20
20
import com .nimbusds .jwt .JWTClaimsSet ;
21
21
import com .nimbusds .jwt .SignedJWT ;
22
22
import com .nimbusds .jwt .util .DateUtils ;
23
- import org .apache .commons .codec .binary .Base64 ;
24
- import org .apache .commons .lang3 .ArrayUtils ;
25
23
import org .apache .commons .lang3 .StringUtils ;
26
24
import org .apache .commons .logging .Log ;
27
25
import org .apache .commons .logging .LogFactory ;
28
26
import org .apache .cxf .message .Message ;
29
27
import org .wso2 .carbon .apimgt .api .APIManagementException ;
30
28
import org .wso2 .carbon .apimgt .api .OAuthTokenInfo ;
31
29
import org .wso2 .carbon .apimgt .common .gateway .constants .JWTConstants ;
32
- import org .wso2 .carbon .apimgt .common .gateway .dto .JWKSConfigurationDTO ;
33
30
import org .wso2 .carbon .apimgt .common .gateway .dto .JWTValidationInfo ;
34
31
import org .wso2 .carbon .apimgt .common .gateway .dto .TokenIssuerDto ;
35
32
import org .wso2 .carbon .apimgt .impl .APIConstants ;
48
45
import org .wso2 .carbon .context .PrivilegedCarbonContext ;
49
46
import org .wso2 .carbon .identity .application .common .model .FederatedAuthenticatorConfig ;
50
47
import org .wso2 .carbon .identity .application .common .model .IdentityProvider ;
51
- import org .wso2 .carbon .identity .application .common .model .IdentityProviderProperty ;
52
48
import org .wso2 .carbon .identity .application .common .util .IdentityApplicationConstants ;
53
49
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 ;
57
50
import org .wso2 .carbon .idp .mgt .IdentityProviderManagementException ;
58
51
import org .wso2 .carbon .idp .mgt .IdentityProviderManager ;
59
52
import org .wso2 .carbon .user .api .UserStoreException ;
63
56
import org .wso2 .carbon .utils .multitenancy .MultitenantUtils ;
64
57
65
58
import javax .security .cert .X509Certificate ;
66
- import java .io .ByteArrayInputStream ;
67
- import java .io .IOException ;
68
- import java .io .InputStream ;
69
59
import java .net .MalformedURLException ;
70
60
import java .net .URL ;
71
61
import java .text .ParseException ;
@@ -338,17 +328,7 @@ private JWTValidator validateAndGetJWTValidatorForIssuer(JWTClaimsSet jwtClaimsS
338
328
+ tokenIssuer + ") does not match with the token issuer (" + tokenIssuers .keySet () + ")" );
339
329
}
340
330
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 );
331
+ IdentityProvider residentIDP = validateAndGetResidentIDPForIssuer (residentTenantDomain , tokenIssuer );
352
332
if (residentIDP == null ) {
353
333
//invalid issuer. invalid token
354
334
throw new APIManagementException ("JWT token issuer validation failed. Reason: Resident Identity Provider "
@@ -357,47 +337,22 @@ private JWTValidator validateAndGetJWTValidatorForIssuer(JWTClaimsSet jwtClaimsS
357
337
JWTValidator jwtValidator = new JWTValidatorImpl ();
358
338
TokenIssuerDto tokenIssuerDto = new TokenIssuerDto (tokenIssuer );
359
339
if (residentIDP .getCertificate () != null ) {
360
- tokenIssuerDto .setCertificate (retrieveCertificateFromContent (residentIDP .getCertificate ()));
340
+ X509Certificate certificate = APIUtil .retrieveCertificateFromContent (residentIDP .getCertificate ());
341
+ if (certificate == null ) {
342
+ throw new APIManagementException ("JWT token issuer validation failed. Reason: Certificate for resident"
343
+ + " identity provider is not in base64 encoded format in organization: "
344
+ + residentTenantDomain );
345
+ }
346
+ tokenIssuerDto .setCertificate (certificate );
347
+
361
348
} else {
362
- JWKSConfigurationDTO jwksConfigurationDTO = new JWKSConfigurationDTO ();
363
- jwksConfigurationDTO .setEnabled (true );
364
- jwksConfigurationDTO .setUrl (getJwksUriForIDP (residentIDP ));
365
- tokenIssuerDto .setJwksConfigurationDTO (jwksConfigurationDTO );
349
+ throw new APIManagementException ("JWT token issuer validation failed. Reason: Certificate for resident" +
350
+ " identity provider cannot be found for the organization: " + residentTenantDomain );
366
351
}
367
352
jwtValidator .loadTokenIssuerConfiguration (tokenIssuerDto );
368
353
return jwtValidator ;
369
354
}
370
355
371
- /**
372
- * Retrieve JWKS URI configured for the resident IDP.
373
- *
374
- * @param idp IdentityProvider
375
- * @return JWKS URI
376
- */
377
- private String getJwksUriForIDP (IdentityProvider idp ) {
378
-
379
- String jwksUri = null ;
380
- IdentityProviderProperty [] identityProviderProperties = idp .getIdpProperties ();
381
- if (!ArrayUtils .isEmpty (identityProviderProperties )) {
382
- for (IdentityProviderProperty identityProviderProperty : identityProviderProperties ) {
383
- if (StringUtils .equals (identityProviderProperty .getName (), JWKS_URI )) {
384
- jwksUri = identityProviderProperty .getValue ();
385
- if (log .isDebugEnabled ()) {
386
- log .debug ("JWKS endpoint set for the identity provider : " + idp .getIdentityProviderName ()
387
- + ", jwks_uri : " + jwksUri );
388
- }
389
- break ;
390
- } else {
391
- if (log .isDebugEnabled ()) {
392
- log .debug ("JWKS endpoint not specified for the identity provider : "
393
- + idp .getIdentityProviderName ());
394
- }
395
- }
396
- }
397
- }
398
- return jwksUri ;
399
- }
400
-
401
356
/**
402
357
* Retrieve token issuer details from deployment.toml file.
403
358
*
@@ -465,28 +420,4 @@ private IdentityProvider validateAndGetResidentIDPForIssuer(String tenantDomain,
465
420
}
466
421
return jwtIssuer .equals (issuer ) ? residentIdentityProvider : null ;
467
422
}
468
-
469
- /**
470
- * Util method to convert base64 encoded certificate content to X509Certificate instance.
471
- *
472
- * @param base64EncodedCertificate Base64 encoded cert string (not URL encoded)
473
- * @return javax.security.cert.X509Certificate
474
- * @throws APIManagementException if an error occurs while retrieving from IDP
475
- */
476
- private X509Certificate retrieveCertificateFromContent (String base64EncodedCertificate )
477
- throws APIManagementException {
478
-
479
- if (base64EncodedCertificate != null ) {
480
- base64EncodedCertificate = APIUtil .getX509certificateContent (base64EncodedCertificate );
481
- byte [] bytes = Base64 .decodeBase64 (base64EncodedCertificate .getBytes ());
482
- try (InputStream inputStream = new ByteArrayInputStream (bytes )) {
483
- return X509Certificate .getInstance (inputStream );
484
- } catch (IOException | javax .security .cert .CertificateException e ) {
485
- String msg = "Error while converting into X509Certificate" ;
486
- log .error (msg , e );
487
- throw new APIManagementException (msg , e );
488
- }
489
- }
490
- return null ;
491
- }
492
423
}
0 commit comments