Skip to content

Commit

Permalink
Rename error codes for ValidationErrorType
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbalakirev committed Nov 3, 2024
1 parent 15c36aa commit afad0f7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
public enum ValidationErrorType {

/** The invalid token. */
INVALID_TOKEN("Invalid token"),
CODE_INVALID_TOKEN("Invalid token"),

/** The empty session token. */
EMPTY_SESSION_TOKEN("Session token is empty"),
CODE_EMPTY_SESSION_TOKEN("Session token is empty"),

/** The empty issuer. */
EMPTY_ISSUER("Issuer is empty"),
CODE_EMPTY_ISSUER("Issuer is empty"),

JWT_BEFORE("Token is not valid yet"),
CODE_JWT_BEFORE("Token is not valid yet"),

JWT_EXPIRED("Token is expired"),
CODE_JWT_EXPIRED("Token is expired"),

INVALID_PUBLIC_KEY("Public key is invalid"),
CODE_INVALID_PUBLIC_KEY("Public key is invalid"),

JWT_INVALID_SIGNATURE("Token signature is invalid"),
CODE_JWT_INVALID_SIGNATURE("Token signature is invalid"),

/** The issuer missmatch. */
ISSUER_MISSMATCH("Token issuer does not match");
CODE_ISSUER_MISSMATCH("Token issuer does not match");

/** The description. */
private final String description;
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/corbado/services/SessionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public UserEntity validateToken(final String sessionToken) throws TokenValidatio

if (sessionToken == null || sessionToken.isEmpty()) {
throw new TokenValidationException(
ValidationErrorType.EMPTY_SESSION_TOKEN, "Session token is empty");
ValidationErrorType.CODE_EMPTY_SESSION_TOKEN, "Session token is empty");
}
DecodedJWT decodedJwt = null;
try {
Expand All @@ -148,20 +148,20 @@ public UserEntity validateToken(final String sessionToken) throws TokenValidatio
final JWTVerifier verifier = JWT.require(algorithm).build();
decodedJwt = verifier.verify(sessionToken);
} catch (final InvalidPublicKeyException e) {
throw new TokenValidationException(ValidationErrorType.INVALID_PUBLIC_KEY, e.getMessage(), e);
throw new TokenValidationException(ValidationErrorType.CODE_INVALID_PUBLIC_KEY, e.getMessage(), e);
} catch (final TokenExpiredException e) {
throw new TokenValidationException(ValidationErrorType.JWT_EXPIRED, e.getMessage(), e);
throw new TokenValidationException(ValidationErrorType.CODE_JWT_EXPIRED, e.getMessage(), e);

} catch (final SignatureVerificationException e) {
throw new TokenValidationException(
ValidationErrorType.JWT_INVALID_SIGNATURE, e.getMessage(), e);
ValidationErrorType.CODE_JWT_INVALID_SIGNATURE, e.getMessage(), e);

} catch (final JWTVerificationException e) {
ValidationErrorType errorType = null;
if (StringUtils.startsWith(e.getMessage(), "The Token can't be used before")) {
errorType = ValidationErrorType.JWT_BEFORE;
errorType = ValidationErrorType.CODE_JWT_BEFORE;
} else {
errorType = ValidationErrorType.INVALID_TOKEN;
errorType = ValidationErrorType.CODE_INVALID_TOKEN;
}
throw new TokenValidationException(
errorType,
Expand All @@ -172,7 +172,7 @@ public UserEntity validateToken(final String sessionToken) throws TokenValidatio
e);
} catch (final Exception e) {
throw new TokenValidationException(
ValidationErrorType.INVALID_TOKEN,
ValidationErrorType.CODE_INVALID_TOKEN,
"Unexpected exception during token validation: " + sessionToken,
e);
}
Expand All @@ -187,7 +187,7 @@ private void validateIssuer(String tokenIssuer, String sessionToken)
// Check if issuer is empty
if (tokenIssuer == null || StringUtils.isBlank(tokenIssuer)) {
throw new TokenValidationException(
ValidationErrorType.EMPTY_ISSUER, "Issuer is empty. Session token: " + sessionToken);
ValidationErrorType.CODE_EMPTY_ISSUER, "Issuer is empty. Session token: " + sessionToken);
}

// Check for old Frontend API (without .cloud.)
Expand All @@ -205,7 +205,7 @@ private void validateIssuer(String tokenIssuer, String sessionToken)
// Check against the configured issuer (e.g., a custom domain or CNAME)
if (!tokenIssuer.equals(this.issuer)) {
throw new TokenValidationException(
ValidationErrorType.ISSUER_MISSMATCH,
ValidationErrorType.CODE_ISSUER_MISSMATCH,
"Issuer mismatch (configured via FrontendAPI: '"
+ this.issuer
+ "', JWT issuer: '"
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/com/corbado/unit/SessionServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ static List<Object[]> provideJwts() throws InvalidKeySpecException, NoSuchAlgori
+ "pvaG4gRG9lIiwiYWRtaW4iOnRydWV9.dyt0CoTl4WoVjAHI9Q_CwSKhl6d_9rhM3NrXuJttkao";
testData.add(
new Object[] {
jwtWithWrongAlgorithm, TokenValidationException.class, ValidationErrorType.INVALID_TOKEN
jwtWithWrongAlgorithm, TokenValidationException.class, ValidationErrorType.CODE_INVALID_TOKEN
});

// Empty JWT
testData.add(
new Object[] {"", TokenValidationException.class, ValidationErrorType.EMPTY_SESSION_TOKEN});
new Object[] {"", TokenValidationException.class, ValidationErrorType.CODE_EMPTY_SESSION_TOKEN});
// Not before (nbf) in future
testData.add(
new Object[] {
Expand All @@ -267,7 +267,7 @@ static List<Object[]> provideJwts() throws InvalidKeySpecException, NoSuchAlgori
System.currentTimeMillis() / 1000 + 100,
privateKey),
TokenValidationException.class,
ValidationErrorType.JWT_BEFORE
ValidationErrorType.CODE_JWT_BEFORE
});

// Expired (exp)
Expand All @@ -279,7 +279,7 @@ static List<Object[]> provideJwts() throws InvalidKeySpecException, NoSuchAlgori
System.currentTimeMillis() / 1000 - 100,
privateKey),
TokenValidationException.class,
ValidationErrorType.JWT_EXPIRED
ValidationErrorType.CODE_JWT_EXPIRED
});

// Invalid issuer (iss)
Expand All @@ -291,7 +291,7 @@ static List<Object[]> provideJwts() throws InvalidKeySpecException, NoSuchAlgori
System.currentTimeMillis() / 1000 - 100,
privateKey),
TokenValidationException.class,
ValidationErrorType.ISSUER_MISSMATCH
ValidationErrorType.CODE_ISSUER_MISSMATCH
});
// Wrong private key
testData.add(
Expand All @@ -302,7 +302,7 @@ static List<Object[]> provideJwts() throws InvalidKeySpecException, NoSuchAlgori
System.currentTimeMillis() / 1000 - 100,
invalidPrivateKey),
TokenValidationException.class,
ValidationErrorType.JWT_INVALID_SIGNATURE
ValidationErrorType.CODE_JWT_INVALID_SIGNATURE
});
// Success with cname
testData.add(
Expand All @@ -313,7 +313,7 @@ static List<Object[]> provideJwts() throws InvalidKeySpecException, NoSuchAlgori
System.currentTimeMillis() / 1000 - 100,
privateKey),
null,
ValidationErrorType.JWT_INVALID_SIGNATURE
ValidationErrorType.CODE_JWT_INVALID_SIGNATURE
});
// Empty issuer
testData.add(
Expand All @@ -324,7 +324,7 @@ static List<Object[]> provideJwts() throws InvalidKeySpecException, NoSuchAlgori
System.currentTimeMillis() / 1000 - 100,
privateKey),
TokenValidationException.class,
ValidationErrorType.EMPTY_ISSUER
ValidationErrorType.CODE_EMPTY_ISSUER
});
// "Success with new Frontend API URL in JWT",
testData.add(
Expand Down Expand Up @@ -358,7 +358,7 @@ static List<Object[]> provideJwts() throws InvalidKeySpecException, NoSuchAlgori
System.currentTimeMillis() / 1000 - 100,
privateKey),
TokenValidationException.class,
ValidationErrorType.ISSUER_MISSMATCH
ValidationErrorType.CODE_ISSUER_MISSMATCH
});

return testData;
Expand Down

0 comments on commit afad0f7

Please sign in to comment.