Skip to content

Commit

Permalink
Add JWT token as default token for portals
Browse files Browse the repository at this point in the history
  • Loading branch information
chamilaadhi committed Feb 28, 2024
1 parent 4900ea0 commit 6d84775
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,8 @@ public static class JwtTokenConstants {
public static final String TOKEN_TYPE = "token_type";
public static final String API_KEY_TOKEN_TYPE = "apiKey";
public static final String DECODING_ALGORITHM_BASE64URL = "base64url";
public static final String APP_DOMAIN = "app_domain";
public static final String USER_DOMAIN = "user_domain";
}

public static final String SIGNATURE_ALGORITHM_RS256 = "RS256";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,6 @@ private OAuthApplicationInfo createApplication(String applicationName, OAuthAppR
logoutConsentProperty.setValue(APIConstants.APP_SKIP_LOGOUT_CONSENT_VALUE);
serviceProviderProperties.add(logoutConsentProperty);

if (APIConstants.JWT.equals(applicationInfo.getTokenType())) {
LocalAndOutboundAuthenticationConfig localAndOutboundConfig =
new LocalAndOutboundAuthenticationConfig();
localAndOutboundConfig.setSkipConsent(true);
localAndOutboundConfig.setSkipLogoutConsent(true);
localAndOutboundConfig.setUseTenantDomainInLocalSubjectIdentifier(true);
serviceProvider.setLocalAndOutBoundAuthenticationConfig(localAndOutboundConfig);
}
String orgId = null;
try {
orgId = RestApiUtil.getValidatedOrganization(securityContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ private boolean handleScopeValidation(Message message, SignedJWTInfo signedJWTIn
String maskedToken = message.get(RestApiConstants.MASKED_TOKEN).toString();
OAuthTokenInfo oauthTokenInfo = new OAuthTokenInfo();
oauthTokenInfo.setAccessToken(accessToken);
oauthTokenInfo.setEndUserName(signedJWTInfo.getJwtClaimsSet().getSubject());
String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;

Check warning on line 146 in components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java#L146

Added line #L146 was not covered by tests
if (signedJWTInfo.getJwtClaimsSet().getClaim(JwtTokenConstants.USER_DOMAIN) != null) {
tenantDomain = (String) signedJWTInfo.getJwtClaimsSet().getClaim(JwtTokenConstants.USER_DOMAIN);

Check warning on line 148 in components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java#L148

Added line #L148 was not covered by tests
}
log.debug("Tenant domain for user " + tenantDomain);
oauthTokenInfo.setEndUserName(signedJWTInfo.getJwtClaimsSet().getSubject() + "@" + tenantDomain);

Check warning on line 151 in components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java#L150-L151

Added lines #L150 - L151 were not covered by tests
oauthTokenInfo.setConsumerKey(signedJWTInfo.getJwtClaimsSet().getStringClaim(JWTConstants.AUTHORIZED_PARTY));
String scopeClaim = signedJWTInfo.getJwtClaimsSet().getStringClaim(JwtTokenConstants.SCOPE);
if (scopeClaim != null) {
Expand All @@ -160,7 +165,7 @@ private boolean handleScopeValidation(Message message, SignedJWTInfo signedJWTIn
//Add the user scopes list extracted from token to the cxf message
message.getExchange().put(RestApiConstants.USER_REST_API_SCOPES, oauthTokenInfo.getScopes());
//If scope validation successful then set tenant name and user name to current context
String tenantDomain = MultitenantUtils.getTenantDomain(oauthTokenInfo.getEndUserName());

int tenantId;
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
Expand Down Expand Up @@ -328,6 +333,10 @@ private JWTValidator validateAndGetJWTValidatorForIssuer(JWTClaimsSet jwtClaimsS
+ tokenIssuer + ") does not match with the token issuer (" + tokenIssuers.keySet() + ")");
}
String residentTenantDomain = APIConstants.SUPER_TENANT_DOMAIN;
if (jwtClaimsSet.getClaim(JwtTokenConstants.APP_DOMAIN) != null) {
residentTenantDomain = (String) jwtClaimsSet.getClaim(JwtTokenConstants.APP_DOMAIN);

Check warning on line 337 in components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java#L337

Added line #L337 was not covered by tests
}
log.debug("Tenant domain for residant IDP " + residentTenantDomain);

Check warning on line 339 in components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.util/src/main/java/org/wso2/carbon/apimgt/rest/api/util/impl/OAuthJwtAuthenticatorImpl.java#L339

Added line #L339 was not covered by tests
IdentityProvider residentIDP = validateAndGetResidentIDPForIssuer(residentTenantDomain, tokenIssuer);
if (residentIDP == null) {
//invalid issuer. invalid token
Expand Down

0 comments on commit 6d84775

Please sign in to comment.