-
Notifications
You must be signed in to change notification settings - Fork 616
Refresh Auth token if expired during getId call. #809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
18cc029
c0580b1
8558f73
0fb2666
b727b35
60f4023
63da567
c26adc7
4afc7e5
86e3728
4f64c09
62098a7
f7457d3
fe2577c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,18 +179,14 @@ String getName() { | |
*/ | ||
private PersistedFidEntry getPersistedFid() throws FirebaseInstallationsException { | ||
PersistedFidEntry persistedFidEntry = persistedFid.readPersistedFidEntryValue(); | ||
if (persistedFidMissingOrInErrorState(persistedFidEntry)) { | ||
if (persistedFidEntry == null | ||
|| persistedFidEntry.getRegistrationStatus() == RegistrationStatus.REGISTER_ERROR) { | ||
throw new FirebaseInstallationsException( | ||
"Failed to get existing fid.", FirebaseInstallationsException.Status.CLIENT_ERROR); | ||
} | ||
return persistedFidEntry; | ||
} | ||
|
||
private static boolean persistedFidMissingOrInErrorState(PersistedFidEntry persistedFidEntry) { | ||
return persistedFidEntry == null | ||
|| persistedFidEntry.getRegistrationStatus() == RegistrationStatus.REGISTER_ERROR; | ||
} | ||
|
||
@NonNull | ||
private static <F, T> Continuation<F, T> orElse(@NonNull Supplier<T> supplier) { | ||
return t -> { | ||
|
@@ -235,31 +231,50 @@ private void persistFid(String fid) throws FirebaseInstallationsException { | |
} | ||
|
||
/** | ||
* Registers the FID with FIS servers if FID is in UNREGISTERED state. | ||
* Registers the FID with FIS servers if FID is in UNREGISTERED state. Also, refreshes auth token | ||
* if expired. | ||
* | ||
* <p>Updates FID registration status to PENDING to avoid multiple network calls to FIS Servers. | ||
*/ | ||
private Task<String> registerFidIfNecessary( | ||
PersistedFidEntry persistedFidEntry, AwaitListener listener) { | ||
String fid = persistedFidEntry.getFirebaseInstallationId(); | ||
|
||
// Check if the fid is unregistered | ||
if (persistedFidEntry.getRegistrationStatus() == RegistrationStatus.UNREGISTERED) { | ||
updatePersistedFidWithPendingStatus(fid); | ||
// If FID registration is in complete/pending state or FID auth token is valid, update the | ||
// listener if awaiting and return the persisted fid. | ||
if (persistedFidEntry.getRegistrationStatus() != RegistrationStatus.UNREGISTERED | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a helper for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added, as this method could be reused in getId and delete APIs. |
||
&& !isAuthTokenExpired(persistedFidEntry)) { | ||
return updateAwaitListenerIfRegisteredFid(persistedFidEntry, listener); | ||
} | ||
|
||
// Update FID registration status to PENDING to avoid multiple network calls to FIS Servers. | ||
persistedFid.insertOrUpdatePersistedFidEntry( | ||
persistedFidEntry.toBuilder().setRegistrationStatus(RegistrationStatus.PENDING).build()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to keep "reading cache status" and "updating cache status" in one atomic section, to avoid duplicated network calls. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. PTAL. |
||
|
||
// If fid registration for this firebase installation is incomplete without an expired auth | ||
// token, execute Fid registration. | ||
if (!isAuthTokenExpired(persistedFidEntry)) { | ||
executeFidRegistration(persistedFidEntry, listener); | ||
} else { | ||
updateAwaitListenerIfRegisteredFid(persistedFidEntry, listener); | ||
return Tasks.forResult(fid); | ||
} | ||
|
||
// If auth token is expired, refresh FID auth token with FIS servers in a background thread and | ||
// updates the listener on | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit : in one line. |
||
// completion | ||
Task<InstallationTokenResult> task = Tasks.call(executor, () -> refreshAuthTokenIfNecessary(1)); | ||
if (listener != null) { | ||
task.addOnCompleteListener((unused) -> listener.onSuccess()); | ||
} | ||
return Tasks.forResult(fid); | ||
} | ||
|
||
private void updateAwaitListenerIfRegisteredFid( | ||
private Task<String> updateAwaitListenerIfRegisteredFid( | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PersistedFidEntry persistedFidEntry, AwaitListener listener) { | ||
if (listener != null | ||
&& persistedFidEntry.getRegistrationStatus() == RegistrationStatus.REGISTERED) { | ||
listener.onSuccess(); | ||
} | ||
return Tasks.forResult(persistedFidEntry.getFirebaseInstallationId()); | ||
} | ||
|
||
/** | ||
|
@@ -273,14 +288,6 @@ private void executeFidRegistration(PersistedFidEntry persistedFidEntry, AwaitLi | |
} | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private void updatePersistedFidWithPendingStatus(String fid) { | ||
persistedFid.insertOrUpdatePersistedFidEntry( | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(fid) | ||
.setRegistrationStatus(RegistrationStatus.PENDING) | ||
.build()); | ||
} | ||
|
||
/** Registers the created Fid with FIS servers and update the shared prefs. */ | ||
private Void registerAndSaveFid(PersistedFidEntry persistedFidEntry) | ||
throws FirebaseInstallationsException { | ||
|
@@ -355,7 +362,8 @@ private InstallationTokenResult getValidAuthToken(PersistedFidEntry persistedFid | |
|
||
private boolean isPersistedFidRegistered(PersistedFidEntry persistedFidEntry) { | ||
return persistedFidEntry != null | ||
&& persistedFidEntry.getRegistrationStatus() == RegistrationStatus.REGISTERED; | ||
&& (persistedFidEntry.getRegistrationStatus() == RegistrationStatus.REGISTERED | ||
|| persistedFidEntry.getRegistrationStatus() == RegistrationStatus.PENDING); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed now, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. We need this check. With this new flow - both getId and getAuthToken need this. getAuthToken uses it with persistedFid in REGISTERED state.
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** Calls the FIS servers to generate an auth token for this Firebase installation. */ | ||
|
@@ -382,6 +390,12 @@ private InstallationTokenResult fetchAuthTokenFromServer(PersistedFidEntry persi | |
|
||
return tokenResult; | ||
} catch (FirebaseInstallationServiceException exception) { | ||
persistedFid.insertOrUpdatePersistedFidEntry( | ||
persistedFidEntry | ||
.toBuilder() | ||
.setRegistrationStatus(RegistrationStatus.REGISTER_ERROR) | ||
.build()); | ||
|
||
throw new FirebaseInstallationsException( | ||
"Failed to generate auth token for a Firebase Installation.", | ||
FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); | ||
|
Uh oh!
There was an error while loading. Please reload this page.