-
Notifications
You must be signed in to change notification settings - Fork 393
Fix use of non-current account in client manager and possible logout of current user. #2766
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
Merged
brandonpage
merged 2 commits into
forcedotcom:dev
from
brandonpage:client-manager-logout
Sep 23, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -379,8 +379,20 @@ public AccMgrAuthTokenProvider(ClientManager clientManager, String instanceUrl, | |
| @Override | ||
| public String getNewAuthToken() { | ||
| SalesforceSDKLogger.i(TAG, "Need new access token"); | ||
| final Account acc = clientManager.getAccount(); | ||
| if (acc == null) { | ||
| UserAccountManager userAccountManager = SalesforceSDKManager.getInstance().getUserAccountManager(); | ||
| Account[] accounts = clientManager.getAccounts(); | ||
| Account matchingAccount = null; | ||
|
|
||
| // Find the account for this client. | ||
| for (Account account : accounts) { | ||
| UserAccount user = userAccountManager.buildUserAccount(account); | ||
| if (user != null && lastNewAuthToken.equals(user.getAuthToken())) { | ||
| matchingAccount = account; | ||
| } | ||
| } | ||
|
|
||
| // Fail early to ensure we don't logout the current user below by sending null. | ||
| if (matchingAccount == null) { | ||
| return null; | ||
| } | ||
|
|
||
|
|
@@ -401,9 +413,8 @@ public String getNewAuthToken() { | |
| try { | ||
|
|
||
| // Invalidate current auth token. | ||
| final String cachedAuthToken = clientManager.peekRestClient(acc).getAuthToken(); | ||
| clientManager.invalidateToken(cachedAuthToken); | ||
| final UserAccount userAccount = refreshStaleToken(acc); | ||
| clientManager.invalidateToken(lastNewAuthToken); | ||
| final UserAccount userAccount = refreshStaleToken(matchingAccount); | ||
|
|
||
| // NB: userAccount will be null if refresh token is no longer valid | ||
| newAuthToken = userAccount != null ? userAccount.getAuthToken() : null; | ||
|
|
@@ -417,11 +428,12 @@ public String getNewAuthToken() { | |
| if (Looper.myLooper() == null) { | ||
| Looper.prepare(); | ||
| } | ||
| boolean showLoginPage = accounts.length > 1; | ||
| // Note: As of writing (2024) this call will never succeed because revoke API is an | ||
| // authenticated endpoint. However, there is no harm in attempting and the debug logs | ||
| // produced may help developers better understand the state of their app. | ||
| SalesforceSDKManager.getInstance() | ||
| .logout(null, null, false, OAuth2.LogoutReason.REFRESH_TOKEN_EXPIRED); | ||
| .logout(matchingAccount, null, showLoginPage, OAuth2.LogoutReason.REFRESH_TOKEN_EXPIRED); | ||
|
Contributor
Author
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.
|
||
| } | ||
|
|
||
| // Broadcasts an intent that the refresh token has been revoked. | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wish there was a better way to do this. Perhaps we should associate each ClientManager to an account?
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.
But don't we have an account associated already with ClientManager:
final Account acc = clientManager.getAccount();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.
That returns the current account, which is (part of) the problem. If the user we are trying to get a new token for not the current user we will invalidate and refresh for the wrong account.
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.
Tying the ClientManager to account would make sense, but it's quite a change in behavior so probably not a 13.1 change. Maybe we could add a convenience method on UserAccountManager to find a matching account by auth token?