@@ -242,7 +242,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
242
242
//The assertion is not an encrypted one.
243
243
signedJWT = getSignedJWT (tokReqMsgCtx );
244
244
if (signedJWT == null ) {
245
- handleException ("No Valid Assertion was found for " + JWTConstants .OAUTH_JWT_BEARER_GRANT_TYPE );
245
+ handleClientException ("No Valid Assertion was found for " + JWTConstants .OAUTH_JWT_BEARER_GRANT_TYPE );
246
246
} else {
247
247
claimsSet = getClaimSet (signedJWT );
248
248
}
@@ -293,7 +293,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
293
293
}
294
294
295
295
if (claimsSet == null ) {
296
- handleException ("Claim values are empty in the given JSON Web Token" );
296
+ handleClientException ("Claim values are empty in the given JSON Web Token" );
297
297
}
298
298
299
299
String jwtIssuer = claimsSet .getIssuer ();
@@ -312,7 +312,8 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
312
312
long timeStampSkewMillis = OAuthServerConfiguration .getInstance ().getTimeStampSkewInSeconds () * 1000 ;
313
313
314
314
if (StringUtils .isEmpty (jwtIssuer ) || StringUtils .isEmpty (subject ) || expirationTime == null || audience == null ) {
315
- handleException ("Mandatory fields(Issuer, Subject, Expiration time or Audience) are empty in the given JSON Web Token." );
315
+ handleClientException ("Mandatory fields(Issuer, Subject, Expiration time or Audience) are empty in the " +
316
+ "given JSON Web Token." );
316
317
}
317
318
try {
318
319
identityProvider = IdentityProviderManager .getInstance ().getIdPByMetadataProperty (
@@ -333,13 +334,13 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
333
334
//check whether this jwt was issued by the resident identity provider
334
335
identityProvider = getResidentIDPForIssuer (tenantDomain , jwtIssuer );
335
336
if (identityProvider == null ) {
336
- handleException ("No Registered IDP found for the JWT with issuer name : " + jwtIssuer );
337
+ handleClientException ("No registered identity provider found for the JWT with issuer name : " + jwtIssuer );
337
338
}
338
339
}
339
340
340
341
tokenEndPointAlias = getTokenEndpointAlias (identityProvider );
341
342
} else {
342
- handleException ("No Registered IDP found for the JWT with issuer name : " + jwtIssuer );
343
+ handleClientException ("No registered identity provider found for the JWT with issuer name : " + jwtIssuer );
343
344
}
344
345
if (signedJWT != null ) {
345
346
signatureValid = validateSignature (signedJWT , identityProvider );
@@ -348,7 +349,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
348
349
log .debug ("Signature/MAC validated successfully." );
349
350
}
350
351
} else {
351
- handleException ("Signature or Message Authentication invalid." );
352
+ handleClientException ("Signature or Message Authentication invalid." );
352
353
}
353
354
}
354
355
setAuthorizedUser (tokReqMsgCtx , identityProvider , subject );
@@ -361,7 +362,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
361
362
tokReqMsgCtx .setScope (tokReqMsgCtx .getOauth2AccessTokenReqDTO ().getScope ());
362
363
363
364
if (StringUtils .isEmpty (tokenEndPointAlias )) {
364
- handleException ("Token Endpoint alias of the local Identity Provider has not been " +
365
+ handleClientException ("Token Endpoint alias of the local Identity Provider has not been " +
365
366
"configured for " + identityProvider .getIdentityProviderName ());
366
367
}
367
368
for (String aud : audience ) {
@@ -374,7 +375,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
374
375
}
375
376
}
376
377
if (!audienceFound ) {
377
- handleException ("None of the audience values matched the tokenEndpoint Alias " + tokenEndPointAlias );
378
+ handleClientException ("None of the audience values matched the tokenEndpoint Alias " + tokenEndPointAlias );
378
379
}
379
380
boolean checkedExpirationTime = checkExpirationTime (expirationTime , currentTimeInMillis ,
380
381
timeStampSkewMillis );
@@ -440,7 +441,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
440
441
} else {
441
442
boolean customClaimsValidated = validateCustomClaims (claimsSet .getClaims ());
442
443
if (!customClaimsValidated ) {
443
- handleException ("Custom Claims in the JWT were invalid" );
444
+ handleClientException ("Custom Claims in the JWT were invalid" );
444
445
}
445
446
}
446
447
if (log .isDebugEnabled ()) {
@@ -688,10 +689,13 @@ private boolean checkExpirationTime(Date expirationTime, long currentTimeInMilli
688
689
689
690
long expirationTimeInMillis = expirationTime .getTime ();
690
691
if ((currentTimeInMillis + timeStampSkewMillis ) > expirationTimeInMillis ) {
691
- handleException ("JSON Web Token is expired." +
692
- ", Expiration Time(ms) : " + expirationTimeInMillis +
693
- ", TimeStamp Skew : " + timeStampSkewMillis +
694
- ", Current Time : " + currentTimeInMillis + ". JWT Rejected and validation terminated" );
692
+ if (log .isDebugEnabled ()) {
693
+ log .debug ("JSON Web Token is expired." +
694
+ ", Expiration Time(ms) : " + expirationTimeInMillis +
695
+ ", TimeStamp Skew : " + timeStampSkewMillis +
696
+ ", Current Time : " + currentTimeInMillis + ". JWT Rejected and validation terminated" );
697
+ }
698
+ handleClientException ("JSON Web Token is expired." );
695
699
}
696
700
return true ;
697
701
}
@@ -709,10 +713,13 @@ private boolean checkNotBeforeTime(Date notBeforeTime, long currentTimeInMillis,
709
713
710
714
long notBeforeTimeMillis = notBeforeTime .getTime ();
711
715
if (currentTimeInMillis + timeStampSkewMillis < notBeforeTimeMillis ) {
712
- handleException ("JSON Web Token is used before Not_Before_Time." +
713
- ", Not Before Time(ms) : " + notBeforeTimeMillis +
714
- ", TimeStamp Skew : " + timeStampSkewMillis +
715
- ", Current Time : " + currentTimeInMillis + ". JWT Rejected and validation terminated" );
716
+ if (log .isDebugEnabled ()) {
717
+ log .debug ("JSON Web Token is used before Not_Before_Time." +
718
+ ", Not Before Time(ms) : " + notBeforeTimeMillis +
719
+ ", TimeStamp Skew : " + timeStampSkewMillis +
720
+ ", Current Time : " + currentTimeInMillis + ". JWT Rejected and validation terminated" );
721
+ }
722
+ handleClientException ("JSON Web Token is used before Not_Before_Time." );
716
723
}
717
724
return true ;
718
725
}
@@ -733,11 +740,14 @@ private boolean checkValidityOfTheToken(Date issuedAtTime, long currentTimeInMil
733
740
long rejectBeforeMillis = 1000L * 60 * validityPeriod ;
734
741
if (currentTimeInMillis + timeStampSkewMillis - issuedAtTimeMillis >
735
742
rejectBeforeMillis ) {
736
- handleException ("JSON Web Token is issued before the allowed time." +
737
- ", Issued At Time(ms) : " + issuedAtTimeMillis +
738
- ", Reject before limit(ms) : " + rejectBeforeMillis +
739
- ", TimeStamp Skew : " + timeStampSkewMillis +
740
- ", Current Time : " + currentTimeInMillis + ". JWT Rejected and validation terminated" );
743
+ if (log .isDebugEnabled ()) {
744
+ log .debug ("JSON Web Token is issued before the allowed time." +
745
+ ", Issued At Time(ms) : " + issuedAtTimeMillis +
746
+ ", Reject before limit(ms) : " + rejectBeforeMillis +
747
+ ", TimeStamp Skew : " + timeStampSkewMillis +
748
+ ", Current Time : " + currentTimeInMillis + ". JWT Rejected and validation terminated" );
749
+ }
750
+ handleClientException ("JSON Web Token is issued before the allowed time." );
741
751
}
742
752
return true ;
743
753
}
@@ -770,7 +780,7 @@ private boolean checkCachedJTI(String jti, SignedJWT signedJWT, JWTCacheEntry en
770
780
log .debug ("jti of the JWT has been validated successfully and cache updated" );
771
781
}
772
782
} else {
773
- handleException ("JWT Token \n " + signedJWT .getHeader ().toJSONObject ().toString () + "\n "
783
+ handleClientException ("JWT Token \n " + signedJWT .getHeader ().toJSONObject ().toString () + "\n "
774
784
+ signedJWT .getPayload ().toJSONObject ().toString () + "\n " +
775
785
"Has been replayed before the allowed expiry time : "
776
786
+ cachedJWT .getJWTClaimsSet ().getExpirationTime ());
@@ -844,7 +854,7 @@ private boolean validateSignature(SignedJWT signedJWT, IdentityProvider idp)
844
854
JWSHeader header = signedJWT .getHeader ();
845
855
X509Certificate x509Certificate = resolveSignerCertificate (header , idp );
846
856
if (x509Certificate == null ) {
847
- handleException (
857
+ handleClientException (
848
858
"Unable to locate certificate for Identity Provider " + idp .getDisplayName () + "; JWT " +
849
859
header .toString ());
850
860
}
@@ -853,7 +863,7 @@ private boolean validateSignature(SignedJWT signedJWT, IdentityProvider idp)
853
863
854
864
String alg = signedJWT .getHeader ().getAlgorithm ().getName ();
855
865
if (StringUtils .isEmpty (alg )) {
856
- handleException ("Algorithm must not be null." );
866
+ handleClientException ("Algorithm must not be null." );
857
867
} else {
858
868
if (log .isDebugEnabled ()) {
859
869
log .debug ("Signature Algorithm found in the JWT Header: " + alg );
@@ -864,15 +874,15 @@ private boolean validateSignature(SignedJWT signedJWT, IdentityProvider idp)
864
874
if (publicKey instanceof RSAPublicKey ) {
865
875
verifier = new RSASSAVerifier ((RSAPublicKey ) publicKey );
866
876
} else {
867
- handleException ("Public key is not an RSA public key." );
877
+ handleClientException ("Public key is not an RSA public key." );
868
878
}
869
879
} else {
870
880
if (log .isDebugEnabled ()) {
871
881
log .debug ("Signature Algorithm not supported yet : " + alg );
872
882
}
873
883
}
874
884
if (verifier == null ) {
875
- handleException ("Could not create a signature verifier for algorithm type: " + alg );
885
+ handleClientException ("Could not create a signature verifier for algorithm type: " + alg );
876
886
}
877
887
}
878
888
@@ -959,6 +969,11 @@ private void handleException(String errorMessage) throws IdentityOAuth2Exception
959
969
throw new IdentityOAuth2Exception (errorMessage );
960
970
}
961
971
972
+ private void handleClientException (String errorMessage ) throws IdentityOAuth2Exception {
973
+
974
+ throw new IdentityOAuth2Exception (errorMessage );
975
+ }
976
+
962
977
private EncryptedJWT getEncryptedJWT (OAuthTokenReqMessageContext tokReqMsgCtx ) {
963
978
964
979
RequestParameter [] params = tokReqMsgCtx .getOauth2AccessTokenReqDTO ().getRequestParameters ();
0 commit comments