-
Notifications
You must be signed in to change notification settings - Fork 29
[MOB-11508] Fix Android SDK auth token refresh in background #910
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,7 +12,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.util.concurrent.ExecutorService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import java.util.concurrent.Executors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public class IterableAuthManager { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public class IterableAuthManager implements IterableActivityMonitor.AppStateCallback { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private static final String TAG = "IterableAuth"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
private static final String expirationString = "exp"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -37,6 +37,7 @@ public class IterableAuthManager { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.authHandler = authHandler; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.authRetryPolicy = authRetryPolicy; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.expiringAuthTokenRefreshPeriod = expiringAuthTokenRefreshPeriod; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IterableActivityMonitor.getInstance().addCallback(this); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public synchronized void requestNewAuthToken(boolean hasFailedPriorAuth) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -51,6 +52,7 @@ public void pauseAuthRetries(boolean pauseRetry) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void reset() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
clearRefreshTimer(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setIsLastAuthTokenValid(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IterableActivityMonitor.getInstance().removeCallback(this); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
void setIsLastAuthTokenValid(boolean isValid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -251,5 +253,37 @@ void clearRefreshTimer() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
isTimerScheduled = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public void onSwitchToBackground() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IterableLogger.v(TAG, "App switched to background. Clearing auth refresh timer."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
clearRefreshTimer(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (Exception e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IterableLogger.e(TAG, "Error in onSwitchToBackground", e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public void onSwitchToForeground() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IterableLogger.v(TAG, "App switched to foreground. Re-evaluating auth token refresh."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
String authToken = api.getAuthToken(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (authToken != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
queueExpirationRefresh(authToken); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// If queueExpirationRefresh didn't schedule a timer (expired token case), request new token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!isTimerScheduled && !pendingAuth) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IterableLogger.d(TAG, "Token expired, requesting new token on foreground"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
requestNewAuthToken(false, null, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if ((api.getEmail() != null || api.getUserId() != null) && !pendingAuth) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IterableLogger.d(TAG, "App foregrounded, user identified, no auth token present. Requesting new token."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
requestNewAuthToken(false, null, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch (Exception e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
IterableLogger.e(TAG, "Error in onSwitchToForeground", e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+271
to
+287
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. [nitpick] Consider refactoring the lengthy onSwitchToForeground method into smaller helper methods to improve readability and maintainability.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,4 +88,34 @@ public void testCallbacks() { | |
verify(callback, never()).onSwitchToBackground(); | ||
} | ||
|
||
@Test | ||
public void testAuthManagerLifecycleRegistration() { | ||
// Create a mock auth handler and initialize IterableApi with auth | ||
IterableAuthHandler mockAuthHandler = mock(IterableAuthHandler.class); | ||
IterableTestUtils.createIterableApiNew(new IterableTestUtils.ConfigBuilderExtender() { | ||
@Override | ||
public IterableConfig.Builder run(IterableConfig.Builder builder) { | ||
return builder.setAuthHandler(mockAuthHandler); | ||
} | ||
}, null); | ||
|
||
IterableApi.getInstance().setEmail("[email protected]"); | ||
IterableAuthManager authManager = IterableApi.getInstance().getAuthManager(); | ||
|
||
// Verify AuthManager is registered as a callback | ||
ActivityController<Activity> activity = Robolectric.buildActivity(Activity.class).create().start().resume(); | ||
Robolectric.flushForegroundThreadScheduler(); | ||
|
||
// Verify we can trigger lifecycle methods without errors | ||
authManager.onSwitchToBackground(); | ||
authManager.onSwitchToForeground(); | ||
|
||
// Test that reset() unregisters the callback | ||
authManager.reset(); | ||
|
||
// After reset, lifecycle methods should still work (no exceptions) | ||
authManager.onSwitchToBackground(); | ||
authManager.onSwitchToForeground(); | ||
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. Probably a better check would be to trigger parent level onSwitchToBackground to check if authmanager's onSwitchToBackground is actually called or not to check the unregistering of callbacks. |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -480,4 +480,33 @@ public void testRegisterForPushInvokedAfterTokenRefresh() throws InterruptedExce | |
//TODO: Verify if registerForPush is invoked | ||
} | ||
|
||
@Test | ||
public void testAuthTokenRefreshPausesOnBackground() throws Exception { | ||
IterableApi.initialize(getContext(), "apiKey"); | ||
|
||
IterableAuthManager authManager = IterableApi.getInstance().getAuthManager(); | ||
|
||
// Set up a valid token and user to trigger normal expiration refresh | ||
doReturn(validJWT).when(authHandler).onAuthTokenRequested(); | ||
IterableApi.getInstance().setEmail("[email protected]"); | ||
shadowOf(getMainLooper()).runToEndOfTasks(); | ||
|
||
// Request auth token which should set a timer for expiration refresh | ||
authManager.requestNewAuthToken(false); | ||
shadowOf(getMainLooper()).runToEndOfTasks(); | ||
|
||
// The timer might be null if the token is considered expired, so let's test the behavior | ||
// rather than the internal timer state. We'll check that onSwitchToBackground and | ||
// onSwitchToForeground can be called without exceptions | ||
|
||
// Simulate app going to background - should clear any timer | ||
authManager.onSwitchToBackground(); | ||
|
||
// Simulate app coming to foreground - should re-evaluate token state | ||
authManager.onSwitchToForeground(); | ||
shadowOf(getMainLooper()).runToEndOfTasks(); | ||
|
||
// Test passes if no exceptions were thrown and lifecycle methods executed successfully | ||
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. Should we not test anything after onSwitchToForeground? Like if the jwt is being fetched again or not? Or if the timer is no more running. Or probably running as there is new requests SDK is making? |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could lead us to
two source of truth situation
where AuthManager class has a authToken in its own memory and the one in SDK's persistent memory has one.