Skip to content

Commit

Permalink
Add TokenValidationException class
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbalakirev committed Oct 27, 2024
1 parent 2245aa0 commit f406a5b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/main/java/com/corbado/enums/exception/ValidationErrorType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.corbado.enums.exception;

import com.corbado.exceptions.TokenValidationException;

/** Enum representing error types for {@link TokenValidationException}. */
public enum ValidationErrorType {

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

/** The signing key error. */
SIGNING_KEY_ERROR("Could not retrieve signing key"),

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

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

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

/** The description. */
private final String description;

/**
* Instantiates a new validation error type.
*
* @param description the description
*/
ValidationErrorType(final String description) {
this.description = description;
}

/**
* To string.
*
* @return the string
*/
@Override
public String toString() {
return description;
}
}
67 changes: 67 additions & 0 deletions src/main/java/com/corbado/exceptions/TokenValidationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.corbado.exceptions;

import com.corbado.enums.exception.ValidationErrorType;
import lombok.Getter;

/**
* TokenValidationException is a custom exception used to indicate issues during token validation.
* This exception is typically thrown when the token's issuer, format, or other key attributes do
* not meet the expected validation criteria.
*
* <p>This exception wraps a specific {@link ValidationErrorType} enum value to categorize the type
* of validation error encountered, and it provides a descriptive message detailing the reason for
* the exception. Additionally, it can wrap an underlying exception that caused the validation
* failure, allowing for more detailed error tracing.
*
* <p>Typical usage includes handling invalid token attributes, such as missing or mismatched
* issuers, empty tokens, or other validation failures.
*
* <p>Example usage:
*
* <pre>{@code
* try {
* validateToken(token);
* } catch (SomeOtherException e) {
* throw new TokenValidationException(ValidationErrorType.ISSUER_MISMATCH,
* "Issuer mismatch (configured issuer: 'expected.com', JWT issuer: 'actual.com')", e);
* }
* }</pre>
*
* @see ValidationErrorType
*/
public class TokenValidationException extends RuntimeException {

/** The Constant serialVersionUID. */
@Getter private static final long serialVersionUID = -2978676337061777870L;

/** The error type. */
@Getter private final ValidationErrorType errorType;

/**
* Constructs a new TokenValidationException with a specified error type, a detailed message, and
* an optional original exception that caused this validation failure.
*
* @param errorType the type of validation error encountered, represented by {@link
* ValidationErrorType}
* @param message a descriptive message providing additional context for the validation error
* @param cause the original exception that caused this validation failure, if any
*/
public TokenValidationException(
final ValidationErrorType errorType, final String message, final Throwable cause) {
super(message, cause);
this.errorType = errorType;
}

/**
* Constructs a new TokenValidationException with a specified error type and detailed message,
* without an underlying cause.
*
* @param errorType the type of validation error encountered, represented by {@link
* ValidationErrorType}
* @param message a descriptive message providing additional context for the validation error
*/
public TokenValidationException(final ValidationErrorType errorType, final String message) {
super(message);
this.errorType = errorType;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/corbado/services/SessionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public SessionValidationResult validateToken(final String sessionToken)

// Verify and decode the JWT using the signing key
final Algorithm algorithm = Algorithm.RSA256(publicKey);
final JWTVerifier verifier = JWT.require(algorithm).withIssuer(this.issuer).build();
final JWTVerifier verifier = JWT.require(algorithm).build();
decodedJwt = verifier.verify(sessionToken);

return SessionValidationResult.builder()
Expand Down

0 comments on commit f406a5b

Please sign in to comment.