Skip to content

Commit 4a477c4

Browse files
committed
remove keymanager holder dependency. Handle tenant logins with JWTs.
1 parent 0728161 commit 4a477c4

File tree

1 file changed

+24
-41
lines changed

1 file changed

+24
-41
lines changed

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

+24-41
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
import org.wso2.carbon.apimgt.impl.APIConstants;
3636
import org.wso2.carbon.apimgt.impl.APIConstants.JwtTokenConstants;
3737
import org.wso2.carbon.apimgt.impl.RESTAPICacheConfiguration;
38-
import org.wso2.carbon.apimgt.impl.dto.KeyManagerDto;
39-
import org.wso2.carbon.apimgt.impl.factory.KeyManagerHolder;
4038
import org.wso2.carbon.apimgt.impl.jwt.JWTValidator;
4139
import org.wso2.carbon.apimgt.impl.jwt.JWTValidatorImpl;
4240
import org.wso2.carbon.apimgt.impl.jwt.SignedJWTInfo;
@@ -53,6 +51,9 @@
5351
import org.wso2.carbon.identity.application.common.model.IdentityProviderProperty;
5452
import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants;
5553
import org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil;
54+
import org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException;
55+
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
56+
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
5657
import org.wso2.carbon.idp.mgt.IdentityProviderManagementException;
5758
import org.wso2.carbon.idp.mgt.IdentityProviderManager;
5859
import org.wso2.carbon.user.api.UserStoreException;
@@ -238,13 +239,12 @@ private JWTValidationInfo validateJWTToken(SignedJWTInfo signedJWTInfo, String j
238239

239240
JWTValidationInfo jwtValidationInfo;
240241
String issuer = signedJWTInfo.getJwtClaimsSet().getIssuer();
241-
String subject = signedJWTInfo.getJwtClaimsSet().getSubject();
242242

243243
if (StringUtils.isNotEmpty(issuer)) {
244244
//validate Issuer
245245
JWTValidator jwtValidator;
246246
try {
247-
jwtValidator = validateAndGetJWTValidatorForIssuer(subject, issuer, maskedToken);
247+
jwtValidator = validateAndGetJWTValidatorForIssuer(signedJWTInfo.getJwtClaimsSet());
248248
} catch (APIManagementException e) {
249249
log.error(e.getMessage(), e);
250250
return null;
@@ -308,68 +308,51 @@ private JWTValidationInfo validateJWTToken(SignedJWTInfo signedJWTInfo, String j
308308
return jwtValidationInfo;
309309
}
310310

311-
/**
312-
* Get logged-in organization from the sub claim of the token.
313-
*
314-
* @param subject Sub claim value
315-
* @param maskedToken Masked token for logging
316-
* @return Organization
317-
*/
318-
private String getOrganizationFromSubject(String subject, String maskedToken) {
319-
if (subject == null) {
320-
log.error("Subject is not found in the token " + maskedToken);
321-
return null;
322-
}
323-
return MultitenantUtils.getTenantDomain(subject);
324-
}
325-
326311
/**
327312
* Retrieve JWT Validator for the given issuer.
328313
*
329314
* @param issuer Issuer from the token
330-
* @param organization Organization
331-
* @param maskedToken Masked token string for logging
332315
* @return JWTValidator implementation for the given issuer.
333316
*/
334-
private JWTValidator getJWTValidator(String issuer, String organization, String maskedToken) {
335-
336-
JWTValidator jwtValidator = APIMConfigUtil.getJWTValidatorMap().get(issuer);
337-
if (jwtValidator == null) {
338-
if (StringUtils.isNotEmpty(issuer) && StringUtils.isNotEmpty(organization)) {
339-
KeyManagerDto keyManagerDto = KeyManagerHolder.getKeyManagerByIssuer(organization, issuer);
340-
if (keyManagerDto != null && keyManagerDto.getJwtValidator() != null) {
341-
jwtValidator = keyManagerDto.getJwtValidator();
342-
}
343-
}
344-
}
345-
return jwtValidator;
317+
private JWTValidator getJWTValidator(String issuer) {
318+
319+
return APIMConfigUtil.getJWTValidatorMap().get(issuer);
346320
}
347321

348322
/**
349323
* Validate issuer in the token against the registered token issuers/default key manager issuer.
350324
*
351-
* @param subject Subject to derive the logged-in organization
352-
* @param tokenIssuer Token issuer from the token
353-
* @param maskedToken Masked token for logging purposes
325+
* @param jwtClaimsSet JWT Claim set from the token
354326
* @return if issuer validation fails or success
355327
* @throws APIManagementException if an error occurs during validation
356328
*/
357-
private JWTValidator validateAndGetJWTValidatorForIssuer(String subject, String tokenIssuer, String maskedToken)
329+
private JWTValidator validateAndGetJWTValidatorForIssuer(JWTClaimsSet jwtClaimsSet)
358330
throws APIManagementException {
359331

360-
String organization = getOrganizationFromSubject(subject, maskedToken);
332+
String tokenIssuer = jwtClaimsSet.getIssuer();
361333
if (tokenIssuers != null && !tokenIssuers.isEmpty()) {
362334
if (tokenIssuers.containsKey(tokenIssuer)) {
363-
return getJWTValidator(tokenIssuer, organization, maskedToken);
335+
return getJWTValidator(tokenIssuer);
364336
}
365337
throw new APIManagementException("JWT token issuer validation failed. Reason: Issuer present in the JWT ("
366338
+ tokenIssuer + ") does not match with the token issuer (" + tokenIssuers.keySet() + ")");
367339
}
368-
IdentityProvider residentIDP = validateAndGetResidentIDPForIssuer(organization, tokenIssuer);
340+
String residentTenantDomain = APIConstants.SUPER_TENANT_DOMAIN;
341+
String consumerKey = (String) jwtClaimsSet.getClaim("azp");
342+
if (consumerKey != null) {
343+
try {
344+
residentTenantDomain = OAuth2Util.getTenantDomainOfOauthApp(consumerKey);
345+
} catch (IdentityOAuth2Exception | InvalidOAuthClientException e) {
346+
throw new APIManagementException("JWT token issuer validation failed. Reason: Error while retrieving "
347+
+ "organization for the consumer app: " + consumerKey);
348+
}
349+
}
350+
IdentityProvider residentIDP
351+
= validateAndGetResidentIDPForIssuer(residentTenantDomain, tokenIssuer);
369352
if (residentIDP == null) {
370353
//invalid issuer. invalid token
371354
throw new APIManagementException("JWT token issuer validation failed. Reason: Resident Identity Provider "
372-
+ "cannot be found for the organization: " + organization);
355+
+ "cannot be found for the organization: " + residentTenantDomain);
373356
}
374357
JWTValidator jwtValidator = new JWTValidatorImpl();
375358
TokenIssuerDto tokenIssuerDto = new TokenIssuerDto(tokenIssuer);

0 commit comments

Comments
 (0)