From 0195ecf6c6220ef525be12eda332fd6902a287fe Mon Sep 17 00:00:00 2001 From: Blaubeeree Date: Thu, 16 Jan 2025 17:59:49 +0100 Subject: [PATCH 1/2] add claimName to InvalidClaimException --- .../jwt/exceptions/IncorrectClaimException.java | 13 +------------ .../jwt/exceptions/InvalidClaimException.java | 16 +++++++++++++++- .../jwt/exceptions/MissingClaimException.java | 13 +------------ 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/src/main/java/com/auth0/jwt/exceptions/IncorrectClaimException.java b/lib/src/main/java/com/auth0/jwt/exceptions/IncorrectClaimException.java index 712e937b..da62dbaa 100644 --- a/lib/src/main/java/com/auth0/jwt/exceptions/IncorrectClaimException.java +++ b/lib/src/main/java/com/auth0/jwt/exceptions/IncorrectClaimException.java @@ -6,7 +6,6 @@ * This exception is thrown when the expected value is not found while verifying the Claims. */ public class IncorrectClaimException extends InvalidClaimException { - private final String claimName; private final Claim claimValue; @@ -19,20 +18,10 @@ public class IncorrectClaimException extends InvalidClaimException { * @param claim The Claim value for which verification failed */ public IncorrectClaimException(String message, String claimName, Claim claim) { - super(message); - this.claimName = claimName; + super(message, claimName); this.claimValue = claim; } - /** - * This method can be used to fetch the name for which the Claim verification failed. - * - * @return The claim name for which the verification failed. - */ - public String getClaimName() { - return claimName; - } - /** * This method can be used to fetch the value for which the Claim verification failed. * diff --git a/lib/src/main/java/com/auth0/jwt/exceptions/InvalidClaimException.java b/lib/src/main/java/com/auth0/jwt/exceptions/InvalidClaimException.java index c5b8eb64..6284d9b3 100644 --- a/lib/src/main/java/com/auth0/jwt/exceptions/InvalidClaimException.java +++ b/lib/src/main/java/com/auth0/jwt/exceptions/InvalidClaimException.java @@ -4,7 +4,21 @@ * The exception that will be thrown while verifying Claims of a JWT. */ public class InvalidClaimException extends JWTVerificationException { - public InvalidClaimException(String message) { + + private final String claimName; + + public InvalidClaimException(String message, String claimName) { super(message); + this.claimName = claimName; } + + /** + * This method can be used to fetch the name for which the Claim verification failed. + * + * @return The claim name for which the verification failed. + */ + public String getClaimName() { + return claimName; + } + } \ No newline at end of file diff --git a/lib/src/main/java/com/auth0/jwt/exceptions/MissingClaimException.java b/lib/src/main/java/com/auth0/jwt/exceptions/MissingClaimException.java index 3bcc2121..90a798b4 100644 --- a/lib/src/main/java/com/auth0/jwt/exceptions/MissingClaimException.java +++ b/lib/src/main/java/com/auth0/jwt/exceptions/MissingClaimException.java @@ -5,19 +5,8 @@ */ public class MissingClaimException extends InvalidClaimException { - private final String claimName; - public MissingClaimException(String claimName) { - super(String.format("The Claim '%s' is not present in the JWT.", claimName)); - this.claimName = claimName; + super(String.format("The Claim '%s' is not present in the JWT.", claimName), claimName); } - /** - * This method can be used to fetch the name for which the Claim is missing during the verification. - * - * @return The name of the Claim that doesn't exist. - */ - public String getClaimName() { - return claimName; - } } From 69e70f504653b8798b427819b5bd5aa6278e2bcc Mon Sep 17 00:00:00 2001 From: Blaubeeree Date: Thu, 16 Jan 2025 18:01:55 +0100 Subject: [PATCH 2/2] change TokenExpiredException to extend JWTVerificationException --- lib/src/main/java/com/auth0/jwt/JWTVerifier.java | 2 +- .../auth0/jwt/exceptions/TokenExpiredException.java | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/src/main/java/com/auth0/jwt/JWTVerifier.java b/lib/src/main/java/com/auth0/jwt/JWTVerifier.java index bf180300..d4b3d637 100644 --- a/lib/src/main/java/com/auth0/jwt/JWTVerifier.java +++ b/lib/src/main/java/com/auth0/jwt/JWTVerifier.java @@ -343,7 +343,7 @@ private boolean assertValidInstantClaim(String claimName, Claim claim, long leew if (shouldBeFuture) { isValid = assertInstantIsFuture(claimVal, leeway, now); if (!isValid) { - throw new TokenExpiredException(String.format("The Token has expired on %s.", claimVal), claimVal); + throw new TokenExpiredException(String.format("The Token has expired on %s.", claimVal), claim); } } else { isValid = assertInstantIsLessThanOrEqualToNow(claimVal, leeway, now); diff --git a/lib/src/main/java/com/auth0/jwt/exceptions/TokenExpiredException.java b/lib/src/main/java/com/auth0/jwt/exceptions/TokenExpiredException.java index 42ab090d..00d5bd0e 100644 --- a/lib/src/main/java/com/auth0/jwt/exceptions/TokenExpiredException.java +++ b/lib/src/main/java/com/auth0/jwt/exceptions/TokenExpiredException.java @@ -1,19 +1,22 @@ package com.auth0.jwt.exceptions; +import com.auth0.jwt.RegisteredClaims; +import com.auth0.jwt.interfaces.Claim; + import java.time.Instant; /** * The exception that is thrown if the token is expired. */ -public class TokenExpiredException extends JWTVerificationException { +public class TokenExpiredException extends IncorrectClaimException { private static final long serialVersionUID = -7076928975713577708L; private final Instant expiredOn; - public TokenExpiredException(String message, Instant expiredOn) { - super(message); - this.expiredOn = expiredOn; + public TokenExpiredException(String message, Claim claim) { + super(message, RegisteredClaims.EXPIRES_AT, claim); + this.expiredOn = claim.asInstant(); } public Instant getExpiredOn() {