Skip to content

[MOB-11813] - JWT in background - Rework #931

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Ayyanchira
Copy link
Member

@Ayyanchira Ayyanchira commented Jul 30, 2025

🔹 Jira Ticket(s) if any

✏️ Description

Please provide a brief description of what this pull request does.

@Ayyanchira Ayyanchira force-pushed the MOB-11813-JWTrework-for-null-in-bg branch from 2667920 to f3b1b26 Compare July 30, 2025 19:33
@Ayyanchira
Copy link
Member Author

At current state, I could still see refresh not happening when app is active & jwt in memory is within its refresh period.

@Ayyanchira Ayyanchira requested a review from sumeruchat August 5, 2025 05:00
@Ayyanchira Ayyanchira marked this pull request as ready for review August 5, 2025 05:00
@Ayyanchira Ayyanchira force-pushed the MOB-11813-JWTrework-for-null-in-bg branch from 87e7f7b to 6c0df8a Compare August 5, 2025 05:02
@Ayyanchira Ayyanchira force-pushed the MOB-11813-JWTrework-for-null-in-bg branch from 6c0df8a to 2ce0ee8 Compare August 5, 2025 05:06
@Ayyanchira
Copy link
Member Author

Background/Foreground Auth Token Management

Background Prevention: JWT requests are now prevented when the app goes to background to avoid unnecessary network calls and improve performance.

Foreground Token Refresh Logic:

  • When the app comes to foreground, the SDK checks if the current auth token is within its refresh period
  • If the token is approaching expiration (within the configured expiringAuthTokenRefreshPeriod), a new token request is scheduled immediately
  • Otherwise, the token refresh is scheduled around the calculated expiration time (offset by the refresh period)

Retry Policy Integration: The existing retry policy logic remains unchanged:

  • When the app comes to foreground and the SDK detects an invalid or expired auth token in memory, it immediately schedules a new auth token request from the app (following the configured retry policy)
  • If no auth token is present in memory (null), the SDK waits for the first SDK request before attempting to schedule an auth token request to the app
    This approach ensures efficient token management while maintaining backward compatibility with existing retry mechanisms.

@Ayyanchira Ayyanchira changed the title [MOB-11813] - Null case and background handling [MOB-11813] - JWT in background - Rework Aug 5, 2025
@Ayyanchira Ayyanchira requested a review from davidtruong August 5, 2025 05:31
Comment on lines +108 to +111
if (!isInForeground) {
IterableLogger.w(TAG, "Auth token request skipped - app is in background");
pendingAuth = false;
return;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will now prevent the Auth token form getting requested to the app. This also does not increase the retryCount

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to call the handleAuthTokenSuccess in this scenario?
Is it possible that the client's app is waiting for the sdk to return something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because no token will be requested until this point, we wont have to call any success or failure callback.
The function is void method which only SDK internally calls for scheduling a request. Once we make a request to app, we call either a success or failure callback

@@ -151,7 +165,7 @@ public void queueExpirationRefresh(String encodedJWT) {
if (triggerExpirationRefreshTime > 0) {
scheduleAuthTokenRefresh(triggerExpirationRefreshTime, true, null);
} else {
IterableLogger.w(TAG, "The expiringAuthTokenRefreshPeriod has already passed for the current JWT");
scheduleAuthTokenRefresh(getNextRetryInterval(), true, null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SDK was missing the opportunity to refresh the token when the trigger time had already passed the configured refresh window

String currentAuthToken = api.getAuthToken();
if (currentAuthToken != null) {
// Reuse existing queueExpirationRefresh logic to check and schedule refresh if needed
queueExpirationRefresh(currentAuthToken);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When App comes back to foreground, it continues with its usual scheduling

// Reuse existing queueExpirationRefresh logic to check and schedule refresh if needed
queueExpirationRefresh(currentAuthToken);
} else {
IterableLogger.d(TAG, "No auth token available for expiration check");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For cases when auth token will be null in memory,
Like for Instances:

  1. App's server is overloaded and not responding
  2. App has not configured/ using JWT based API key
    The app will not immediately follow scheduling auth Token request. When SDK makes a request and realizes missing of an auth token from backend is when it will continue to ask for new Auth token and following retry policy

“Akshay added 2 commits August 7, 2025 08:11
1. ActivityMonitor removing callback doesnt need to be done.
2. encodedJWT can be null sometimes. SDK should still attempt to request jwt as per RetryPolicy (only when in foreground)
3. SDK to check email OR userID existence before proceeding to auth token check and retries
@Ayyanchira Ayyanchira force-pushed the MOB-11813-JWTrework-for-null-in-bg branch from 558eb97 to f4aedbf Compare August 8, 2025 07:02
public void onSwitchToBackground() {
IterableLogger.d(TAG, "App switched to background - disabling auth token requests");
isInForeground = false;
clearRefreshTimer();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we wrap in try catch blocks

IterableLogger.d(TAG, "App switched to foreground - enabling auth token requests");
isInForeground = true;

checkAndHandleAuthRefresh();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we wrap in try catch blocks

@Ayyanchira
Copy link
Member Author

Current state, the Test fails are not flaky. local running the tests are making it run properly but not when running the entire suite

@Ayyanchira Ayyanchira force-pushed the MOB-11813-JWTrework-for-null-in-bg branch from e550af7 to b445b0c Compare August 12, 2025 22:31
@Ayyanchira
Copy link
Member Author

@sumeruchat @davidtruong updated with try catch and review and comments on discussions initiated

} catch (Exception e) {
IterableLogger.e(TAG, "Error in onSwitchToBackground", e);
throw new RuntimeException(e);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we throwing the exception

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find! Thanks. No runtime exception needed. Replacing with Error log.

Im also seeing that isTimerScheduled is also checked within `scheduleAuthTokenRefresh` method. But somehow, without checking calling makes the test cases fail. So adding it as added checks are not harmful.
Apart from that, surrounded background and foreground tasks in try catch as suggested in PR Reviews
@Ayyanchira Ayyanchira force-pushed the MOB-11813-JWTrework-for-null-in-bg branch from b445b0c to 50e002a Compare August 14, 2025 16:37
@Ayyanchira Ayyanchira requested a review from sumeruchat August 14, 2025 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants