Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small exception improvements #704

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/main/java/com/auth0/jwt/JWTVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down