-
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 1 commit
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 |
---|---|---|
|
@@ -235,31 +235,45 @@ 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); | ||
executeFidRegistration(persistedFidEntry, listener); | ||
} else { | ||
updateAwaitListenerIfRegisteredFid(persistedFidEntry, listener); | ||
if (!isFidUnregisteredOrAuthTokenExpired(persistedFidEntry)) { | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return updateAwaitListenerIfRegisteredFid(persistedFidEntry, listener); | ||
} | ||
|
||
updatePersistedFidWithPendingStatus(persistedFidEntry); | ||
executeFISCalls(persistedFidEntry, listener); | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Tasks.forResult(fid); | ||
} | ||
|
||
private void updateAwaitListenerIfRegisteredFid( | ||
private boolean isFidUnregisteredOrAuthTokenExpired(PersistedFidEntry persistedFidEntry) { | ||
return persistedFidEntry.getRegistrationStatus() == RegistrationStatus.UNREGISTERED | ||
|| isAuthTokenExpired(persistedFidEntry); | ||
} | ||
|
||
private void executeFISCalls(PersistedFidEntry persistedFidEntry, AwaitListener listener) { | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!isAuthTokenExpired(persistedFidEntry)) { | ||
executeFidRegistration(persistedFidEntry, listener); | ||
return; | ||
} | ||
refreshAuthToken(listener); | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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,12 +287,20 @@ private void executeFidRegistration(PersistedFidEntry persistedFidEntry, AwaitLi | |
} | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private void updatePersistedFidWithPendingStatus(String fid) { | ||
/** | ||
* Refreshes FID auth token with FIS servers in a background thread and updates the listener on | ||
* completion. | ||
*/ | ||
private void refreshAuthToken(AwaitListener listener) { | ||
Task<InstallationTokenResult> task = Tasks.call(executor, () -> refreshAuthTokenIfNecessary(1)); | ||
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. never pass the raw value for an (Gradle has a check for this afair, but I don't know if this repo is configured to run them) 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. Thank you so much. Updated. Any examples on how do I configure it? |
||
if (listener != null) { | ||
task.addOnCompleteListener((unused) -> listener.onSuccess()); | ||
} | ||
} | ||
|
||
private void updatePersistedFidWithPendingStatus(PersistedFidEntry persistedFidEntry) { | ||
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. A general comment : I feel like you are doing functionalization too much in this class. Functionalization is useful when you want to hide a chunk of detailed logic, or to avoid duplicated code. But for example for updatePersistedFidWithPendingStatus, this function is only used in one place(doesn't avoid duplicated code), and it's not hiding any detailed logic (use one line to replace another line) A downside of over functionalization is, with too many small method, it's really hard to find the relationship between them. Above all, for these cases, I prefer making the code inline than making as a function. 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. Thanks for the detailed explanation. Updated the code. PTAL. |
||
persistedFid.insertOrUpdatePersistedFidEntry( | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(fid) | ||
.setRegistrationStatus(RegistrationStatus.PENDING) | ||
.build()); | ||
persistedFidEntry.toBuilder().setRegistrationStatus(RegistrationStatus.PENDING).build()); | ||
} | ||
|
||
/** Registers the created Fid with FIS servers and update the shared prefs. */ | ||
|
@@ -355,7 +377,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 +405,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.