Skip to content

Commit

Permalink
โ™ป๏ธ change to appropriate exception
Browse files Browse the repository at this point in the history
  • Loading branch information
HaiSeong committed Jul 23, 2024
1 parent 478b7d0 commit 3571e3f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +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.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 @@ -39,7 +41,7 @@ public GoogleSignUpResponse signUpWithGoogle(GoogleSignUpRequest googleSignUpReq
User user = createUser(googleSignUpRequest);

if (userRepository.existsByEmail(user.getEmail())) {
throw new IllegalArgumentException("Email already exists.");
throw new AuthenticationException(HttpStatus.BAD_REQUEST, "์ค‘๋ณต ์ด๋ฉ”์ผ ๊ฐ€์ž… ์‹œ๋„", "์ด๋ฏธ ๊ฐ€์ž…๋œ ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค.");
}

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

@Component
Expand Down Expand Up @@ -42,10 +44,9 @@ public TokenPayload extract(String token) {
JWTVerifier jwtVerifier = JWT.require(secretAlgorithm).build();
try {
DecodedJWT decodedJWT = jwtVerifier.verify(token);

return getTokenPayload(decodedJWT);
} catch (JWTVerificationException e) {
throw new IllegalArgumentException(e);
throw new AuthenticationException(HttpStatus.UNAUTHORIZED, "ํ† ํฐ ๊ฒ€์ฆ ์‹คํŒจ", "์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ์ž…๋‹ˆ๋‹ค.");
}
}

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

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

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

public String extractToken(String authorizationHeader) {
if (authorizationHeader == null) {
throw new IllegalArgumentException("Authorization header is required.");
throw new AuthenticationException(HttpStatus.BAD_REQUEST, "์ธ์ฆ ํ—ค๋” ์—†์Œ", "์ธ์ฆ ํ—ค๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
}
if (!authorizationHeader.startsWith(BEARER)) {
throw new IllegalArgumentException("Invalid Authorization header.");
throw new AuthenticationException(HttpStatus.BAD_REQUEST, "์ธ์ฆ ํ—ค๋” ์˜ค๋ฅ˜", "์ธ์ฆ ํ—ค๋”๋Š” Bearer๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
}
return authorizationHeader.substring(BEARER.length());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,6 @@ void signUpWithGoogleWhenEmailAleadyRegistered() throws FirebaseAuthException {
.body(request)
.when().post("/api/oauth/google/sign-up")
.then().log().all()
.statusCode(500); // TODO : Exception Handler ์ƒ์„ฑํ›„ ์ ์ ˆํ•œ ์ƒํƒœ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ
.statusCode(400);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +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.util.JwtTokenManager;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -117,7 +118,7 @@ void signUpWithGoogleWhenEmailAleadyRegistered() throws FirebaseAuthException {
when(firebaseAuth.verifyIdToken(idToken)).thenReturn(firebaseToken);

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

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

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

assertThatThrownBy(() -> jwtTokenManager.extract(accessToken))
.isInstanceOf(IllegalArgumentException.class);
.isInstanceOf(AuthenticationException.class)
.hasMessage("์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ์ž…๋‹ˆ๋‹ค.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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 org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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

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

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

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

0 comments on commit 3571e3f

Please sign in to comment.