Skip to content

Commit d1f3f55

Browse files
authored
Merge pull request #68 from Yoshani/log-client-error
Handle client exceptions without logging
2 parents 6a6a7bf + 1ecfe9e commit d1f3f55

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

component/grant-type/src/main/java/org/wso2/carbon/identity/oauth2/grant/jwt/JWTBearerGrantHandler.java

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
242242
//The assertion is not an encrypted one.
243243
signedJWT = getSignedJWT(tokReqMsgCtx);
244244
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);
246246
} else {
247247
claimsSet = getClaimSet(signedJWT);
248248
}
@@ -293,7 +293,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
293293
}
294294

295295
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");
297297
}
298298

299299
String jwtIssuer = claimsSet.getIssuer();
@@ -312,7 +312,8 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
312312
long timeStampSkewMillis = OAuthServerConfiguration.getInstance().getTimeStampSkewInSeconds() * 1000;
313313

314314
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.");
316317
}
317318
try {
318319
identityProvider = IdentityProviderManager.getInstance().getIdPByMetadataProperty(
@@ -333,13 +334,13 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
333334
//check whether this jwt was issued by the resident identity provider
334335
identityProvider = getResidentIDPForIssuer(tenantDomain, jwtIssuer);
335336
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);
337338
}
338339
}
339340

340341
tokenEndPointAlias = getTokenEndpointAlias(identityProvider);
341342
} 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);
343344
}
344345
if (signedJWT != null) {
345346
signatureValid = validateSignature(signedJWT, identityProvider);
@@ -348,7 +349,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
348349
log.debug("Signature/MAC validated successfully.");
349350
}
350351
} else {
351-
handleException("Signature or Message Authentication invalid.");
352+
handleClientException("Signature or Message Authentication invalid.");
352353
}
353354
}
354355
setAuthorizedUser(tokReqMsgCtx, identityProvider, subject);
@@ -361,7 +362,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
361362
tokReqMsgCtx.setScope(tokReqMsgCtx.getOauth2AccessTokenReqDTO().getScope());
362363

363364
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 " +
365366
"configured for " + identityProvider.getIdentityProviderName());
366367
}
367368
for (String aud : audience) {
@@ -374,7 +375,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
374375
}
375376
}
376377
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);
378379
}
379380
boolean checkedExpirationTime = checkExpirationTime(expirationTime, currentTimeInMillis,
380381
timeStampSkewMillis);
@@ -440,7 +441,7 @@ public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws Id
440441
} else {
441442
boolean customClaimsValidated = validateCustomClaims(claimsSet.getClaims());
442443
if (!customClaimsValidated) {
443-
handleException("Custom Claims in the JWT were invalid");
444+
handleClientException("Custom Claims in the JWT were invalid");
444445
}
445446
}
446447
if (log.isDebugEnabled()) {
@@ -688,10 +689,13 @@ private boolean checkExpirationTime(Date expirationTime, long currentTimeInMilli
688689

689690
long expirationTimeInMillis = expirationTime.getTime();
690691
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.");
695699
}
696700
return true;
697701
}
@@ -709,10 +713,13 @@ private boolean checkNotBeforeTime(Date notBeforeTime, long currentTimeInMillis,
709713

710714
long notBeforeTimeMillis = notBeforeTime.getTime();
711715
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.");
716723
}
717724
return true;
718725
}
@@ -733,11 +740,14 @@ private boolean checkValidityOfTheToken(Date issuedAtTime, long currentTimeInMil
733740
long rejectBeforeMillis = 1000L * 60 * validityPeriod;
734741
if (currentTimeInMillis + timeStampSkewMillis - issuedAtTimeMillis >
735742
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.");
741751
}
742752
return true;
743753
}
@@ -770,7 +780,7 @@ private boolean checkCachedJTI(String jti, SignedJWT signedJWT, JWTCacheEntry en
770780
log.debug("jti of the JWT has been validated successfully and cache updated");
771781
}
772782
} else {
773-
handleException("JWT Token \n" + signedJWT.getHeader().toJSONObject().toString() + "\n"
783+
handleClientException("JWT Token \n" + signedJWT.getHeader().toJSONObject().toString() + "\n"
774784
+ signedJWT.getPayload().toJSONObject().toString() + "\n" +
775785
"Has been replayed before the allowed expiry time : "
776786
+ cachedJWT.getJWTClaimsSet().getExpirationTime());
@@ -844,7 +854,7 @@ private boolean validateSignature(SignedJWT signedJWT, IdentityProvider idp)
844854
JWSHeader header = signedJWT.getHeader();
845855
X509Certificate x509Certificate = resolveSignerCertificate(header, idp);
846856
if (x509Certificate == null) {
847-
handleException(
857+
handleClientException(
848858
"Unable to locate certificate for Identity Provider " + idp.getDisplayName() + "; JWT " +
849859
header.toString());
850860
}
@@ -853,7 +863,7 @@ private boolean validateSignature(SignedJWT signedJWT, IdentityProvider idp)
853863

854864
String alg = signedJWT.getHeader().getAlgorithm().getName();
855865
if (StringUtils.isEmpty(alg)) {
856-
handleException("Algorithm must not be null.");
866+
handleClientException("Algorithm must not be null.");
857867
} else {
858868
if (log.isDebugEnabled()) {
859869
log.debug("Signature Algorithm found in the JWT Header: " + alg);
@@ -864,15 +874,15 @@ private boolean validateSignature(SignedJWT signedJWT, IdentityProvider idp)
864874
if (publicKey instanceof RSAPublicKey) {
865875
verifier = new RSASSAVerifier((RSAPublicKey) publicKey);
866876
} else {
867-
handleException("Public key is not an RSA public key.");
877+
handleClientException("Public key is not an RSA public key.");
868878
}
869879
} else {
870880
if (log.isDebugEnabled()) {
871881
log.debug("Signature Algorithm not supported yet : " + alg);
872882
}
873883
}
874884
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);
876886
}
877887
}
878888

@@ -959,6 +969,11 @@ private void handleException(String errorMessage) throws IdentityOAuth2Exception
959969
throw new IdentityOAuth2Exception(errorMessage);
960970
}
961971

972+
private void handleClientException(String errorMessage) throws IdentityOAuth2Exception {
973+
974+
throw new IdentityOAuth2Exception(errorMessage);
975+
}
976+
962977
private EncryptedJWT getEncryptedJWT(OAuthTokenReqMessageContext tokReqMsgCtx) {
963978

964979
RequestParameter[] params = tokReqMsgCtx.getOauth2AccessTokenReqDTO().getRequestParameters();

0 commit comments

Comments
 (0)