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

fix: JWT decrypt 예외시 AuthException을 던지도록하고, 전역 Advice를 등록 #403

Merged
merged 1 commit into from
Mar 8, 2024
Merged
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
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
Loading