From f03034fd6b7458d1d363de2c402f000ae7a9937f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8E=E1=85=AC=E1=84=8C=E1=85=AE=E1=86=AB=E1=84=92?= =?UTF-8?q?=E1=85=A9?= Date: Thu, 11 Jan 2024 21:47:30 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=A6=AC=ED=94=84=EB=A0=88?= =?UTF-8?q?=EC=8B=9C=ED=86=A0=ED=81=B0=20userId=20=EA=B5=AC=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../koin/domain/user/service/UserService.java | 14 ++++++++++++-- .../in/koreatech/koin/acceptance/AuthApiTest.java | 1 - 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/in/koreatech/koin/domain/user/service/UserService.java b/src/main/java/in/koreatech/koin/domain/user/service/UserService.java index be5dda27c..404114ff9 100644 --- a/src/main/java/in/koreatech/koin/domain/user/service/UserService.java +++ b/src/main/java/in/koreatech/koin/domain/user/service/UserService.java @@ -1,6 +1,7 @@ package in.koreatech.koin.domain.user.service; import in.koreatech.koin.domain.auth.JwtProvider; +import in.koreatech.koin.domain.auth.exception.AuthException; import in.koreatech.koin.domain.user.dto.UserLoginRequest; import in.koreatech.koin.domain.user.dto.UserLoginResponse; import in.koreatech.koin.domain.user.dto.UserTokenRefreshRequest; @@ -36,7 +37,7 @@ public UserLoginResponse login(UserLoginRequest request) { } String accessToken = jwtProvider.createToken(user); - String refreshToken = String.format("%s%d", UUID.randomUUID(), user.getId()); + String refreshToken = String.format("%s-%d", UUID.randomUUID(), user.getId()); UserToken savedToken = userTokenRepository.save(UserToken.create(user.getId(), refreshToken)); user.updateLastLoggedTime(LocalDateTime.now()); User saved = userRepository.save(user); @@ -50,7 +51,8 @@ public void logout(User user) { } public UserTokenRefreshResponse refresh(UserTokenRefreshRequest request) { - UserToken userToken = userTokenRepository.findByRefreshToken(request.refreshToken()) + String userId = getUserId(request.refreshToken()); + UserToken userToken = userTokenRepository.findById(Long.parseLong(userId)) .orElseThrow(() -> new IllegalArgumentException("refresh token이 존재하지 않습니다. request: " + request)); if (!Objects.equals(userToken.getRefreshToken(), request.refreshToken())) { throw new IllegalArgumentException("refresh token이 일치하지 않습니다. request: " + request); @@ -60,4 +62,12 @@ public UserTokenRefreshResponse refresh(UserTokenRefreshRequest request) { String accessToken = jwtProvider.createToken(user); return UserTokenRefreshResponse.of(accessToken, userToken.getRefreshToken()); } + + private static String getUserId(String refreshToken) { + String[] split = refreshToken.split("-"); + if (split.length == 0) { + throw new AuthException("올바르지 않은 인증 토큰입니다. refreshToken: " + refreshToken); + } + return split[split.length - 1]; + } } diff --git a/src/test/java/in/koreatech/koin/acceptance/AuthApiTest.java b/src/test/java/in/koreatech/koin/acceptance/AuthApiTest.java index 652e35773..b3a876800 100644 --- a/src/test/java/in/koreatech/koin/acceptance/AuthApiTest.java +++ b/src/test/java/in/koreatech/koin/acceptance/AuthApiTest.java @@ -165,7 +165,6 @@ void userRefreshToken() { RestAssured .given() .log().all() - .header("Authorization", "BEARER " + response.jsonPath().getString("token")) .body( Map.of("refresh_token", response.jsonPath().getString("refresh_token")) )