Clear OAuth cache using persisted token scopes on revoke#3193
Clear OAuth cache using persisted token scopes on revoke#3193KD23243 wants to merge 1 commit intowso2-extensions:masterfrom
Conversation
Adds a helper that fetches the original scopes from the access token DAO and clears the OAuth cache using those scope keys, so cache entries are removed even when the in-memory accessTokenDO scopes have been mutated during request processing. Wired into OAuth2Service.revokeTokenByOAuthClient and covered with unit tests for empty allowed-scopes, blank token, null/empty DB scopes, success path, and DAO failure path.
📝 WalkthroughWalkthroughThe changes introduce a new utility method Changes
Sequence DiagramsequenceDiagram
participant Client as Token Revocation<br/>Request Handler
participant OAuthUtil as OAuthUtil
participant TokenDAO as AccessTokenDAO
participant Cache as OAuth Cache
Client->>OAuthUtil: clearOAuthCacheUsingPersistedScopes(tokenBindingRef,<br/>accessTokenDO, revokeRequest)
OAuthUtil->>OAuthUtil: Validate conditions<br/>(scopes, token, data)
OAuthUtil->>TokenDAO: getAccessToken(tokenString,<br/>fromPersistence=true)
TokenDAO-->>OAuthUtil: Returns persisted<br/>AccessTokenDO
OAuthUtil->>OAuthUtil: Convert persisted scopes<br/>to string format
OAuthUtil->>Cache: clearOAuthCache(consumerKey,<br/>scope)
Cache-->>OAuthUtil: Cache cleared
OAuthUtil->>Cache: clearOAuthCache(consumerKey,<br/>scope, tokenBindingRef)
Cache-->>OAuthUtil: Cache cleared
OAuthUtil-->>Client: Method completes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
...s/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth/OAuthUtil.java
Show resolved
Hide resolved
...s/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth/OAuthUtil.java
Show resolved
Hide resolved
....wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/OAuth2Service.java
Show resolved
Hide resolved
...g.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth/OAuthUtilTest.java
Show resolved
Hide resolved
...g.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth/OAuthUtilTest.java
Show resolved
Hide resolved
There was a problem hiding this comment.
AI Agent Log Improvement Checklist
- The log-related comments and suggestions in this review were generated by an AI tool to assist with identifying potential improvements. Purpose of reviewing the code for log improvements is to improve the troubleshooting capabilities of our products.
- Please make sure to manually review and validate all suggestions before applying any changes. Not every code suggestion would make sense or add value to our purpose. Therefore, you have the freedom to decide which of the suggestions are helpful.
✅ Before merging this pull request:
- Review all AI-generated comments for accuracy and relevance.
- Complete and verify the table below. We need your feedback to measure the accuracy of these suggestions and the value they add. If you are rejecting a certain code suggestion, please mention the reason briefly in the suggestion for us to capture it.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In
`@components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth/OAuthUtilTest.java`:
- Around line 823-835: The test creates mockFactory and mockAccessTokenDAO but
never wires mockFactory.getAccessTokenDAO() to return mockAccessTokenDAO, so the
verify(mockAccessTokenDAO, never()) assertions are disconnected; update the test
to stub OAuthTokenPersistenceFactory.getInstance() to return mockFactory and
then stub mockFactory.getAccessTokenDAO() to return mockAccessTokenDAO (for the
same test blocks that call OAuthUtil.clearOAuthCacheUsingPersistedScopes),
ensuring verify(mockAccessTokenDAO, never()).getAccessToken(...) checks the
actual DAO returned in the call path.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: a6bc28ed-6581-405d-9620-5c4b7e259714
📒 Files selected for processing (3)
components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth/OAuthUtil.javacomponents/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/OAuth2Service.javacomponents/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth/OAuthUtilTest.java
...g.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth/OAuthUtilTest.java
Show resolved
Hide resolved
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3193 +/- ##
============================================
+ Coverage 59.18% 59.67% +0.49%
+ Complexity 10337 10253 -84
============================================
Files 711 711
Lines 56756 55930 -826
Branches 13685 13460 -225
============================================
- Hits 33593 33379 -214
+ Misses 18674 18163 -511
+ Partials 4489 4388 -101
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
PR builder started |
|
PR builder completed |
jenkins-is-staging
left a comment
There was a problem hiding this comment.
Approving the pull request based on the successful pr build https://github.com/wso2/product-is/actions/runs/24225608320
| // mismatches, retrieve the original scopes from the database before clearing | ||
| // the OAuth cache. | ||
| AccessTokenDO dbTokenDO = OAuthTokenPersistenceFactory.getInstance() | ||
| .getAccessTokenDAO() |
There was a problem hiding this comment.
use token provider if exists
Purpose
During token revocation, the in-memory
accessTokenDOmay have had its scopes mutated or filtered earlier in the request lifecycle. The existing cache-clearing path uses those (potentially mutated) scopes as part of the cache key, so entries originally cached under the persisted scopes can be left behind.Related Issue
N/A
Implementation
OAuthUtil.clearOAuthCacheUsingPersistedScopes(...)which:OAuthServerConfiguration#getAllowedScopesis empty or the access token is blank.OAuthTokenPersistenceFactory.getAccessTokenDAO().getAccessToken(token, true)to obtain the original persisted scopes.clearOAuthCachefor both the(consumerKey, authzUser, scope, tokenBindingReference)and(consumerKey, authzUser, scope)key variants.IdentityOAuth2Exceptionfrom the DAO and logs it without propagating.OAuth2Service#revokeTokenByOAuthClient, called right after the existingclearOAuthCacheinvocations.OAuthUtilTestcovering:allowedScopesshort-circuit.null.nullscope array.IdentityOAuth2Exceptionis swallowed and no cache clear is invoked.Summary by CodeRabbit
New Features
Tests