-
Notifications
You must be signed in to change notification settings - Fork 37
[Accelerator 4] Add FSJWTAccessTokenClaimProvider to set consent-id as an access token claim #888
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
imesh94
wants to merge
4
commits into
wso2:main
Choose a base branch
from
imesh94:fix/public/fs-jwt-claim-provider
base: main
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 all commits
Commits
Show all changes
4 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
Some comments aren't visible on the classic Files Changed page.
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
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
133 changes: 133 additions & 0 deletions
133
...ancial/services/accelerator/identity/extensions/claims/FSJWTAccessTokenClaimProvider.java
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 |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /** | ||
| * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). | ||
| * <p> | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.wso2.financial.services.accelerator.identity.extensions.claims; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception; | ||
| import org.wso2.carbon.identity.oauth2.authz.OAuthAuthzReqMessageContext; | ||
| import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext; | ||
| import org.wso2.carbon.identity.oauth2.token.handlers.claims.JWTAccessTokenClaimProvider; | ||
| import org.wso2.financial.services.accelerator.common.constant.FinancialServicesConstants; | ||
| import org.wso2.financial.services.accelerator.common.exception.FinancialServicesException; | ||
| import org.wso2.financial.services.accelerator.identity.extensions.util.IdentityCommonConstants; | ||
| import org.wso2.financial.services.accelerator.identity.extensions.util.IdentityCommonUtils; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * This claim provider adds FS specific additional claims to the JWT access token. | ||
| * It extracts the consent ID from scopes, adds it as a claim, and removes internal scopes. | ||
| */ | ||
| public class FSJWTAccessTokenClaimProvider implements JWTAccessTokenClaimProvider { | ||
|
|
||
| private static final Log log = LogFactory.getLog(FSJWTAccessTokenClaimProvider.class); | ||
| private static final String CONSENT_ID_CLAIM_NAME = IdentityCommonUtils.getConfiguredConsentIdClaimName(); | ||
|
|
||
| @Override | ||
| public Map<String, Object> getAdditionalClaims(OAuthAuthzReqMessageContext context) throws IdentityOAuth2Exception { | ||
| // No need to add claims in the authorization flow. | ||
| return new HashMap<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> getAdditionalClaims(OAuthTokenReqMessageContext context) throws IdentityOAuth2Exception { | ||
|
|
||
| Map<String, Object> additionalClaims = new HashMap<>(); | ||
|
|
||
| try { | ||
| if (context != null && context.getScope() != null && context.getOauth2AccessTokenReqDTO() != null && | ||
| IdentityCommonUtils.isRegulatoryApp(context.getOauth2AccessTokenReqDTO().getClientId())) { | ||
| if (log.isDebugEnabled()) { | ||
| log.debug("Processing JWT access token claims. Scopes: " + | ||
| Arrays.toString(context.getScope()).replaceAll("[\r\n]", "")); | ||
| } | ||
| addConsentIDClaim(context.getScope(), additionalClaims); | ||
| removeConsentIdScope(context.getScope(), additionalClaims); | ||
| } | ||
| } catch (FinancialServicesException e) { | ||
| log.error("Error while adding custom claims to the jwt token", e); | ||
| throw new IdentityOAuth2Exception(e.getMessage(), e); | ||
| } | ||
|
|
||
| return additionalClaims; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * This method adds the consent ID claim to the JWT token. | ||
| * | ||
| * @param scopes The scopes array | ||
| * @param claims Claims map to add the consent ID to | ||
| */ | ||
| void addConsentIDClaim(String[] scopes, Map<String, Object> claims) { | ||
|
|
||
| String consentID = extractConsentIdFromScopes(scopes); | ||
| if (StringUtils.isNotEmpty(consentID)) { | ||
| claims.put(CONSENT_ID_CLAIM_NAME, consentID); | ||
|
|
||
| if (log.isDebugEnabled()) { | ||
| log.debug("Added consent ID claim: " + CONSENT_ID_CLAIM_NAME.replaceAll("[\r\n]", "") + | ||
| " : " + consentID.replaceAll("[\r\n]", "")); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Removes the consent ID scope from the JWT token's scopes claim. | ||
| * The consent ID scope was added during the authorization process for internal use. | ||
| * | ||
| * @param scopes The scopes array | ||
| * @param claims Claims map to update the scope claim in | ||
| */ | ||
| void removeConsentIdScope(String[] scopes, Map<String, Object> claims) { | ||
|
|
||
| String[] nonInternalScopes = IdentityCommonUtils.removeInternalScopes(scopes); | ||
| String scopeString = StringUtils.join(nonInternalScopes, FinancialServicesConstants.SPACE_SEPARATOR); | ||
| claims.put(FinancialServicesConstants.SCOPE, scopeString); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the consent ID from the given scopes. | ||
| * Consent ID scope can be in one of the following formats: | ||
| * FS_{consentId} | ||
| * {CONSENT_ID_CLAIM_NAME}{consentId} | ||
| * | ||
| * @param scopes OAuth2 scopes | ||
| * @return extracted consent ID, or empty string if not found | ||
| */ | ||
| private String extractConsentIdFromScopes(String[] scopes) { | ||
|
|
||
| if (scopes == null || scopes.length == 0) { | ||
| return StringUtils.EMPTY; | ||
| } | ||
| for (String scope : scopes) { | ||
| if (scope.startsWith(IdentityCommonConstants.FS_PREFIX)) { | ||
| return scope.substring(IdentityCommonConstants.FS_PREFIX.length()); | ||
| } | ||
| if (scope.startsWith(CONSENT_ID_CLAIM_NAME)) { | ||
| return scope.substring(CONSENT_ID_CLAIM_NAME.length()); | ||
| } | ||
| } | ||
| return StringUtils.EMPTY; | ||
| } | ||
| } |
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
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.