Skip to content

Commit

Permalink
fix: JWT decrypt 예외시 AuthException을 던지도록하고, 전역 Advice를 등록 (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Mar 8, 2024
1 parent 2c5eecd commit b7802fe
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.nalab.auth.application.common.exception;

public class AuthException extends RuntimeException {

public AuthException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package me.nalab.auth.application.common.utils;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.IncorrectClaimException;
import com.auth0.jwt.exceptions.MissingClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import me.nalab.auth.application.common.exception.AuthException;
import org.springframework.stereotype.Component;

import com.auth0.jwt.JWT;
Expand Down Expand Up @@ -33,7 +39,16 @@ public DecodedJWT verify(String jwt) {
var algorithm = getAlgorithm();
var verifier = getVerifier(algorithm);

return verifier.verify(jwt);
try {
return verifier.verify(jwt);
} catch (TokenExpiredException tokenExpiredException) {
throw new AuthException("Expired token");
} catch (IncorrectClaimException
| MissingClaimException
| SignatureVerificationException
| AlgorithmMismatchException invalidTokenException) {
throw new AuthException("Invalid token");
}
}

private JWTVerifier getVerifier(Algorithm algorithm) {
Expand Down
1 change: 1 addition & 0 deletions auth/auth-application/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports me.nalab.auth.application.port.in.web;
exports me.nalab.auth.application.port.in;
exports me.nalab.auth.application.port.out;
exports me.nalab.auth.application.common.exception;

requires lombok;
requires com.fasterxml.jackson.annotation;
Expand Down
1 change: 1 addition & 0 deletions auth/auth-web-adaptor/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dependencies {
implementation project(':auth:auth-application')
implementation project(':auth:oauth-application')
implementation project(':core:exception-handler')
testImplementation project(':auth:auth-application')

implementation 'org.springframework.boot:spring-boot-starter-web'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.nalab.auth.web.adaptor.advice;

import me.nalab.auth.application.common.exception.AuthException;
import me.nalab.core.exception.handler.ErrorTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class AuthControllerAdvice {

@ExceptionHandler(AuthException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public ErrorTemplate handleAuthException(AuthException authException) {
return ErrorTemplate.of(authException.getMessage());
}

}
1 change: 1 addition & 0 deletions auth/auth-web-adaptor/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

requires luffy.auth.auth.application.main;
requires luffy.auth.oauth.application.main;
requires luffy.core.exception.handler.main;

requires lombok;
requires java.validation;
Expand Down
2 changes: 2 additions & 0 deletions core/exception-handler/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module luffy.core.exception.handler.main {
exports me.nalab.core.exception.handler;

requires spring.web;
requires lombok;
requires com.fasterxml.jackson.annotation;
Expand Down

0 comments on commit b7802fe

Please sign in to comment.