-
Notifications
You must be signed in to change notification settings - Fork 414
Implement cache clearance for original token scopes during revocation #3071
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
KD23243
wants to merge
7
commits into
wso2-extensions:master
Choose a base branch
from
KD23243:cacheClearRevoke
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cf8b7a8
Implement cache clearance for original token scopes during revocation
KD23243 bad9c76
Add debug logs
KD23243 dfb6e42
Merge branch 'wso2-extensions:master' into cacheClearRevoke
KD23243 5584aaf
Use service method to fetch the access token
KD23243 ebdf741
Fix unit tests
KD23243 1d31749
Add unit tests
KD23243 a1babe2
Address review comments
KD23243 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 |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ | |
| import org.wso2.carbon.identity.oauth.cache.OAuthCache; | ||
| import org.wso2.carbon.identity.oauth.cache.OAuthCacheKey; | ||
| import org.wso2.carbon.identity.oauth.common.OAuthConstants; | ||
| import org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration; | ||
| import org.wso2.carbon.identity.oauth.dao.OAuthAppDO; | ||
| import org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO; | ||
| import org.wso2.carbon.identity.oauth.event.OAuthEventInterceptor; | ||
|
|
@@ -62,6 +63,7 @@ | |
| import org.wso2.carbon.identity.oauth2.IdentityOAuth2ServerException; | ||
| import org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory; | ||
| import org.wso2.carbon.identity.oauth2.dao.SharedAppResolveDAO; | ||
| import org.wso2.carbon.identity.oauth2.dto.OAuthRevocationRequestDTO; | ||
| import org.wso2.carbon.identity.oauth2.model.AccessTokenDO; | ||
| import org.wso2.carbon.identity.oauth2.model.AuthzCodeDO; | ||
| import org.wso2.carbon.identity.oauth2.util.OAuth2Util; | ||
|
|
@@ -1573,4 +1575,58 @@ private static String getUserStoreDomainOfParentUser(String parentUserId, String | |
| + parentUserId + " in tenant domain: " + tenantDomain, e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Triggers a cache clearance for the original scopes associated with a token. | ||
| * This is necessary because the scopes in the provided {@code accessTokenDO} might have been | ||
| * filtered or mutated during the request lifecycle. Fetch the original scopes from the | ||
| * database to ensure the cache is cleared using the correct keys. | ||
| * | ||
| * @param tokenBindingReference The token binding identifier. | ||
| * @param accessTokenDO The current (potentially mutated) access token object. | ||
| * @param revokeRequestDTO The revocation request details containing the consumer key. | ||
| */ | ||
| public static void clearOAuthCacheUsingPersistedScopes(String tokenBindingReference, AccessTokenDO accessTokenDO, | ||
| OAuthRevocationRequestDTO revokeRequestDTO) { | ||
|
|
||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("Clearing OAuth cache for persisted scopes. Consumer key: " + revokeRequestDTO.getConsumerKey()); | ||
| } | ||
|
|
||
| if (OAuthServerConfiguration.getInstance().getAllowedScopes().isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| String accessToken = accessTokenDO.getAccessToken(); | ||
| if (StringUtils.isBlank(accessToken)) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // The in-memory scopes may be mutated during validation. To avoid cache-key | ||
| // mismatches, retrieve the original scopes from the database before clearing | ||
| // the OAuth cache. | ||
| AccessTokenDO dbTokenDO = OAuthTokenPersistenceFactory.getInstance() | ||
|
||
| .getAccessTokenDAO() | ||
| .getAccessToken(accessToken, true); | ||
| if (dbTokenDO == null || dbTokenDO.getScope() == null || dbTokenDO.getScope().length == 0) { | ||
| return; | ||
| } | ||
|
|
||
| String dbTokenScopeString = OAuth2Util.buildScopeString(dbTokenDO.getScope()); | ||
| OAuthUtil.clearOAuthCache(revokeRequestDTO.getConsumerKey(), | ||
| accessTokenDO.getAuthzUser(), dbTokenScopeString, tokenBindingReference); | ||
| OAuthUtil.clearOAuthCache(revokeRequestDTO.getConsumerKey(), | ||
| accessTokenDO.getAuthzUser(), dbTokenScopeString); | ||
KD23243 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (LOG.isDebugEnabled()) { | ||
| LOG.debug("Successfully cleared OAuth cache entries for consumer key: " + | ||
| revokeRequestDTO.getConsumerKey()); | ||
| } | ||
|
|
||
| } catch (IdentityOAuth2Exception e) { | ||
| LOG.error("Error while clearing cache entries for extended scopes. Consumer key: " | ||
| + revokeRequestDTO.getConsumerKey(), e); | ||
| } | ||
| } | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.