|
35 | 35 | import org.wso2.carbon.apimgt.impl.APIConstants;
|
36 | 36 | import org.wso2.carbon.apimgt.impl.APIConstants.JwtTokenConstants;
|
37 | 37 | 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; |
40 | 38 | import org.wso2.carbon.apimgt.impl.jwt.JWTValidator;
|
41 | 39 | import org.wso2.carbon.apimgt.impl.jwt.JWTValidatorImpl;
|
42 | 40 | import org.wso2.carbon.apimgt.impl.jwt.SignedJWTInfo;
|
|
53 | 51 | import org.wso2.carbon.identity.application.common.model.IdentityProviderProperty;
|
54 | 52 | import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants;
|
55 | 53 | 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; |
56 | 57 | import org.wso2.carbon.idp.mgt.IdentityProviderManagementException;
|
57 | 58 | import org.wso2.carbon.idp.mgt.IdentityProviderManager;
|
58 | 59 | import org.wso2.carbon.user.api.UserStoreException;
|
@@ -238,13 +239,12 @@ private JWTValidationInfo validateJWTToken(SignedJWTInfo signedJWTInfo, String j
|
238 | 239 |
|
239 | 240 | JWTValidationInfo jwtValidationInfo;
|
240 | 241 | String issuer = signedJWTInfo.getJwtClaimsSet().getIssuer();
|
241 |
| - String subject = signedJWTInfo.getJwtClaimsSet().getSubject(); |
242 | 242 |
|
243 | 243 | if (StringUtils.isNotEmpty(issuer)) {
|
244 | 244 | //validate Issuer
|
245 | 245 | JWTValidator jwtValidator;
|
246 | 246 | try {
|
247 |
| - jwtValidator = validateAndGetJWTValidatorForIssuer(subject, issuer, maskedToken); |
| 247 | + jwtValidator = validateAndGetJWTValidatorForIssuer(signedJWTInfo.getJwtClaimsSet()); |
248 | 248 | } catch (APIManagementException e) {
|
249 | 249 | log.error(e.getMessage(), e);
|
250 | 250 | return null;
|
@@ -308,68 +308,51 @@ private JWTValidationInfo validateJWTToken(SignedJWTInfo signedJWTInfo, String j
|
308 | 308 | return jwtValidationInfo;
|
309 | 309 | }
|
310 | 310 |
|
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 |
| - |
326 | 311 | /**
|
327 | 312 | * Retrieve JWT Validator for the given issuer.
|
328 | 313 | *
|
329 | 314 | * @param issuer Issuer from the token
|
330 |
| - * @param organization Organization |
331 |
| - * @param maskedToken Masked token string for logging |
332 | 315 | * @return JWTValidator implementation for the given issuer.
|
333 | 316 | */
|
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); |
346 | 320 | }
|
347 | 321 |
|
348 | 322 | /**
|
349 | 323 | * Validate issuer in the token against the registered token issuers/default key manager issuer.
|
350 | 324 | *
|
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 |
354 | 326 | * @return if issuer validation fails or success
|
355 | 327 | * @throws APIManagementException if an error occurs during validation
|
356 | 328 | */
|
357 |
| - private JWTValidator validateAndGetJWTValidatorForIssuer(String subject, String tokenIssuer, String maskedToken) |
| 329 | + private JWTValidator validateAndGetJWTValidatorForIssuer(JWTClaimsSet jwtClaimsSet) |
358 | 330 | throws APIManagementException {
|
359 | 331 |
|
360 |
| - String organization = getOrganizationFromSubject(subject, maskedToken); |
| 332 | + String tokenIssuer = jwtClaimsSet.getIssuer(); |
361 | 333 | if (tokenIssuers != null && !tokenIssuers.isEmpty()) {
|
362 | 334 | if (tokenIssuers.containsKey(tokenIssuer)) {
|
363 |
| - return getJWTValidator(tokenIssuer, organization, maskedToken); |
| 335 | + return getJWTValidator(tokenIssuer); |
364 | 336 | }
|
365 | 337 | throw new APIManagementException("JWT token issuer validation failed. Reason: Issuer present in the JWT ("
|
366 | 338 | + tokenIssuer + ") does not match with the token issuer (" + tokenIssuers.keySet() + ")");
|
367 | 339 | }
|
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); |
369 | 352 | if (residentIDP == null) {
|
370 | 353 | //invalid issuer. invalid token
|
371 | 354 | 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); |
373 | 356 | }
|
374 | 357 | JWTValidator jwtValidator = new JWTValidatorImpl();
|
375 | 358 | TokenIssuerDto tokenIssuerDto = new TokenIssuerDto(tokenIssuer);
|
|
0 commit comments