Skip to content

Commit

Permalink
โ™ป๏ธ change custom exception
Browse files Browse the repository at this point in the history
  • Loading branch information
HaiSeong committed Jul 24, 2024
1 parent 8d78228 commit cbfaa2c
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.pengcook.authentication.exception;

import org.springframework.http.HttpStatus;

public class AuthorizationHeaderException extends AuthenticationException {

public AuthorizationHeaderException(String message) {
super(HttpStatus.NOT_FOUND, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.pengcook.authentication.exception;

import org.springframework.http.HttpStatus;

public class DuplicationException extends AuthenticationException {

public DuplicationException(String message) {
super(HttpStatus.BAD_REQUEST, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.pengcook.authentication.exception;

import org.springframework.http.HttpStatus;

public class FirebaseTokenException extends AuthenticationException {

public FirebaseTokenException(String message) {
super(HttpStatus.UNAUTHORIZED, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.pengcook.authentication.exception;

import org.springframework.http.HttpStatus;

public class JwtTokenException extends AuthenticationException {

public JwtTokenException(String message) {
super(HttpStatus.UNAUTHORIZED, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import net.pengcook.authentication.dto.GoogleSignUpRequest;
import net.pengcook.authentication.dto.GoogleSignUpResponse;
import net.pengcook.authentication.dto.TokenPayload;
import net.pengcook.authentication.exception.AuthenticationException;
import net.pengcook.authentication.exception.DuplicationException;
import net.pengcook.authentication.exception.FirebaseTokenException;
import net.pengcook.authentication.util.JwtTokenManager;
import net.pengcook.user.domain.User;
import net.pengcook.user.repository.UserRepository;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

@Service
Expand Down Expand Up @@ -41,7 +41,7 @@ public GoogleSignUpResponse signUpWithGoogle(GoogleSignUpRequest googleSignUpReq
User user = createUser(googleSignUpRequest);

if (userRepository.existsByEmail(user.getEmail())) {
throw new AuthenticationException(HttpStatus.BAD_REQUEST, "์ด๋ฏธ ๊ฐ€์ž…๋œ ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค.");
throw new DuplicationException("์ด๋ฏธ ๊ฐ€์ž…๋œ ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค.");
}

User savedUser = userRepository.save(user);
Expand All @@ -67,7 +67,7 @@ private FirebaseToken decodeIdToken(String idToken) {
try {
return firebaseAuth.verifyIdToken(idToken);
} catch (FirebaseAuthException e) {
throw new AuthenticationException(HttpStatus.UNAUTHORIZED, "๊ตฌ๊ธ€ ์ธ์ฆ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
throw new FirebaseTokenException("๊ตฌ๊ธ€ ์ธ์ฆ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Date;
import net.pengcook.authentication.dto.TokenPayload;
import net.pengcook.authentication.exception.AuthenticationException;
import net.pengcook.authentication.exception.JwtTokenException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

@Component
Expand Down Expand Up @@ -46,7 +45,7 @@ public TokenPayload extract(String token) {
DecodedJWT decodedJWT = jwtVerifier.verify(token);
return getTokenPayload(decodedJWT);
} catch (JWTVerificationException e) {
throw new AuthenticationException(HttpStatus.UNAUTHORIZED, "์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ์ž…๋‹ˆ๋‹ค.");
throw new JwtTokenException("์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ์ž…๋‹ˆ๋‹ค.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.pengcook.authentication.util;

import net.pengcook.authentication.exception.AuthenticationException;
import org.springframework.http.HttpStatus;
import net.pengcook.authentication.exception.AuthorizationHeaderException;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -11,10 +10,10 @@ public class TokenExtractor {

public String extractToken(String authorizationHeader) {
if (authorizationHeader == null) {
throw new AuthenticationException(HttpStatus.BAD_REQUEST, "์ธ์ฆ ํ—ค๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
throw new AuthorizationHeaderException("์ธ์ฆ ํ—ค๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
}
if (!authorizationHeader.startsWith(BEARER)) {
throw new AuthenticationException(HttpStatus.BAD_REQUEST, "์ธ์ฆ ํ—ค๋”๋Š” Bearer๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
throw new AuthorizationHeaderException("์ธ์ฆ ํ—ค๋”๋Š” Bearer๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
}
return authorizationHeader.substring(BEARER.length());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import net.pengcook.authentication.dto.GoogleLoginResponse;
import net.pengcook.authentication.dto.GoogleSignUpRequest;
import net.pengcook.authentication.dto.GoogleSignUpResponse;
import net.pengcook.authentication.exception.AuthenticationException;
import net.pengcook.authentication.exception.DuplicationException;
import net.pengcook.authentication.util.JwtTokenManager;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -118,7 +118,7 @@ void signUpWithGoogleWhenEmailAleadyRegistered() throws FirebaseAuthException {
when(firebaseAuth.verifyIdToken(idToken)).thenReturn(firebaseToken);

assertThatThrownBy(() -> loginService.signUpWithGoogle(request))
.isInstanceOf(AuthenticationException.class)
.isInstanceOf(DuplicationException.class)
.hasMessage("์ด๋ฏธ ๊ฐ€์ž…๋œ ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import net.pengcook.authentication.dto.TokenPayload;
import net.pengcook.authentication.exception.AuthenticationException;
import net.pengcook.authentication.exception.JwtTokenException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -41,7 +41,7 @@ void extractWhenInvalidToken() {
String accessToken = "fakefakefakefakefake.accessaccessaccessaccess.tokentokentokentokentoken";

assertThatThrownBy(() -> jwtTokenManager.extract(accessToken))
.isInstanceOf(AuthenticationException.class)
.isInstanceOf(JwtTokenException.class)
.hasMessage("์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ์ž…๋‹ˆ๋‹ค.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import net.pengcook.authentication.exception.AuthenticationException;
import net.pengcook.authentication.exception.AuthorizationHeaderException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -27,7 +27,7 @@ void extractTokenWhenAuthorizationHeaderNull() {
String authorizationHeader = null;

assertThatThrownBy(() -> tokenExtractor.extractToken(authorizationHeader))
.isInstanceOf(AuthenticationException.class)
.isInstanceOf(AuthorizationHeaderException.class)
.hasMessage("์ธ์ฆ ํ—ค๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
}

Expand All @@ -37,7 +37,7 @@ void extractTokenWhenAuthorizationHeaderNotStartWithBearer() {
String authorizationHeader = "Not Bearer token";

assertThatThrownBy(() -> tokenExtractor.extractToken(authorizationHeader))
.isInstanceOf(AuthenticationException.class)
.isInstanceOf(AuthorizationHeaderException.class)
.hasMessage("์ธ์ฆ ํ—ค๋”๋Š” Bearer๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
}
}

0 comments on commit cbfaa2c

Please sign in to comment.