From 3988aceb3359d29d87dbfbb481b7a9be0e62e585 Mon Sep 17 00:00:00 2001 From: xb205 <62425964+devxb@users.noreply.github.com> Date: Tue, 12 Mar 2024 12:27:01 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20TokenInfo=EB=A5=BC=20record?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD=ED=95=98=EA=B3=A0=20token-value?= =?UTF-8?q?=20=EB=B6=80=EB=B6=84=EC=9D=84=20=EB=B0=9B=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=ED=95=9C=EB=8B=A4=20(#407)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/JwtLoginedDecryptServiceTest.java | 4 ++-- .../JwtDecryptInterceptorConfigurer.java | 1 + .../auth/mock/config/MockAuthConfigurer.java | 1 + .../user/application/common/dto/TokenInfo.java | 11 ++++------- .../service/LoginedUserGetByTokenService.java | 15 +++------------ .../application/service/UserDeleteService.java | 2 +- .../LoginedUserGetByTokenServiceTest.java | 16 +--------------- .../logined/LoginedUserGetController.java | 5 ++--- 8 files changed, 15 insertions(+), 40 deletions(-) diff --git a/auth/auth-application/src/test/java/me/nalab/auth/application/service/JwtLoginedDecryptServiceTest.java b/auth/auth-application/src/test/java/me/nalab/auth/application/service/JwtLoginedDecryptServiceTest.java index 9a6f5fdf..ac1b32e0 100644 --- a/auth/auth-application/src/test/java/me/nalab/auth/application/service/JwtLoginedDecryptServiceTest.java +++ b/auth/auth-application/src/test/java/me/nalab/auth/application/service/JwtLoginedDecryptServiceTest.java @@ -61,8 +61,8 @@ void THROW_EXCEPTION_WHEN_TOKEN_IS_NULL_OR_EMPTY(String token) { private void assertDecryptedInfo(TokenInfo response, String expectedName, Long expectedUserId, Long expectedTargetId) { - Assertions.assertEquals(response.getUserId(), expectedUserId); - Assertions.assertEquals(response.getTargetId(), expectedTargetId); + Assertions.assertEquals(response.userId(), expectedUserId); + Assertions.assertEquals(response.targetId(), expectedTargetId); } @Test diff --git a/auth/auth-interceptor/src/main/java/me/nalab/auth/interceptor/JwtDecryptInterceptorConfigurer.java b/auth/auth-interceptor/src/main/java/me/nalab/auth/interceptor/JwtDecryptInterceptorConfigurer.java index b57b22da..02c04199 100644 --- a/auth/auth-interceptor/src/main/java/me/nalab/auth/interceptor/JwtDecryptInterceptorConfigurer.java +++ b/auth/auth-interceptor/src/main/java/me/nalab/auth/interceptor/JwtDecryptInterceptorConfigurer.java @@ -33,6 +33,7 @@ public class JwtDecryptInterceptorConfigurer implements WebMvcConfigurer { "/v1/gallerys/logins", "/v1/gallerys", "/v1/surveys/bookmarks*", + "/v1/users/logins", }; @Override diff --git a/auth/auth-mock/src/main/java/me/nalab/auth/mock/config/MockAuthConfigurer.java b/auth/auth-mock/src/main/java/me/nalab/auth/mock/config/MockAuthConfigurer.java index c2bcfda3..5dbe9242 100644 --- a/auth/auth-mock/src/main/java/me/nalab/auth/mock/config/MockAuthConfigurer.java +++ b/auth/auth-mock/src/main/java/me/nalab/auth/mock/config/MockAuthConfigurer.java @@ -23,6 +23,7 @@ public class MockAuthConfigurer implements WebMvcConfigurer { "/v1/reviewers/summary*", "/v2/surveys/*/feedbacks", "/v1/surveys/*/bookmarks", + "/v1/users/logins", }; @Override diff --git a/user/user-application/src/main/java/me/nalab/user/application/common/dto/TokenInfo.java b/user/user-application/src/main/java/me/nalab/user/application/common/dto/TokenInfo.java index 4701d5ae..d60e4686 100644 --- a/user/user-application/src/main/java/me/nalab/user/application/common/dto/TokenInfo.java +++ b/user/user-application/src/main/java/me/nalab/user/application/common/dto/TokenInfo.java @@ -1,11 +1,8 @@ package me.nalab.user.application.common.dto; -import lombok.Data; - -@Data -public class TokenInfo { - - private final Long targetId; - private final Long userId; +public record TokenInfo( + Long targetId, + Long userId +) { } diff --git a/user/user-application/src/main/java/me/nalab/user/application/service/LoginedUserGetByTokenService.java b/user/user-application/src/main/java/me/nalab/user/application/service/LoginedUserGetByTokenService.java index 34fa2e4e..54320fc6 100644 --- a/user/user-application/src/main/java/me/nalab/user/application/service/LoginedUserGetByTokenService.java +++ b/user/user-application/src/main/java/me/nalab/user/application/service/LoginedUserGetByTokenService.java @@ -7,7 +7,6 @@ import org.springframework.stereotype.Service; import lombok.RequiredArgsConstructor; -import me.nalab.user.application.exception.InvalidTokenException; import me.nalab.user.application.port.in.LoginedUserGetByTokenUseCase; import me.nalab.user.application.port.out.persistence.LoginedUserGetByTokenPort; import org.springframework.transaction.annotation.Transactional; @@ -23,17 +22,9 @@ public class LoginedUserGetByTokenService implements LoginedUserGetByTokenUseCas @Transactional(readOnly = true) public LoginedInfo getLoginedInfoByToken(String encryptedToken) { Objects.requireNonNull(encryptedToken, "encryptedToken은 null이 되면 안됩니다."); - String[] split = encryptedToken.split(" "); - throwIfInvalidToken(split); - var tokenInfo = loginedUserGetByTokenPort.decryptToken(split[1]); - var user = userGetPort.getById(tokenInfo.getUserId()); - return LoginedInfo.from(tokenInfo.getTargetId(), user); - } - - private void throwIfInvalidToken(String[] split) { - if(split.length < 2) { - throw new InvalidTokenException(split[0]); - } + var tokenInfo = loginedUserGetByTokenPort.decryptToken(encryptedToken); + var user = userGetPort.getById(tokenInfo.userId()); + return LoginedInfo.from(tokenInfo.targetId(), user); } } diff --git a/user/user-application/src/main/java/me/nalab/user/application/service/UserDeleteService.java b/user/user-application/src/main/java/me/nalab/user/application/service/UserDeleteService.java index 23c20495..16f3fae2 100644 --- a/user/user-application/src/main/java/me/nalab/user/application/service/UserDeleteService.java +++ b/user/user-application/src/main/java/me/nalab/user/application/service/UserDeleteService.java @@ -18,6 +18,6 @@ public class UserDeleteService implements UserDeleteUseCase { @Transactional public void deleteByToken(String token) { var tokenInfo = loginedUserGetByTokenPort.decryptToken(token); - userDeletePort.deleteUserById(tokenInfo.getUserId()); + userDeletePort.deleteUserById(tokenInfo.userId()); } } diff --git a/user/user-application/src/test/java/me/nalab/user/application/service/LoginedUserGetByTokenServiceTest.java b/user/user-application/src/test/java/me/nalab/user/application/service/LoginedUserGetByTokenServiceTest.java index 355e0464..1703b05c 100644 --- a/user/user-application/src/test/java/me/nalab/user/application/service/LoginedUserGetByTokenServiceTest.java +++ b/user/user-application/src/test/java/me/nalab/user/application/service/LoginedUserGetByTokenServiceTest.java @@ -48,7 +48,7 @@ void GET_LOGINED_INFO_BY_TOKEN_SUCCESS() { TokenInfo tokenInfo = new TokenInfo(12345L, DEFAULT_USER.getId()); String token = "hello token"; - Mockito.when(loginedUserGetByTokenPort.decryptToken(token.split(" ")[1])).thenReturn(tokenInfo); + Mockito.when(loginedUserGetByTokenPort.decryptToken(token)).thenReturn(tokenInfo); Mockito.when(userGetPort.getById(54321L)).thenReturn(DEFAULT_USER); // when @@ -67,18 +67,4 @@ void NULL_PARAMETER_TEST(String token) { // then Assertions.assertThat(result).isInstanceOf(NullPointerException.class); } - - @Test - @DisplayName("Invalid token signature 테스트") - void DECRYPT_INVALID_TOKEN() { - // given - String token = "invalid"; - - // when - Throwable result = Assertions.catchThrowable(() -> loginedUserGetByTokenUseCase.getLoginedInfoByToken(token)); - - // then - Assertions.assertThat(result).isInstanceOf(InvalidTokenException.class); - } - } diff --git a/user/user-web-adaptor/src/main/java/me/nalab/user/web/adaptor/logined/LoginedUserGetController.java b/user/user-web-adaptor/src/main/java/me/nalab/user/web/adaptor/logined/LoginedUserGetController.java index 3cad0e7e..17bafe54 100644 --- a/user/user-web-adaptor/src/main/java/me/nalab/user/web/adaptor/logined/LoginedUserGetController.java +++ b/user/user-web-adaptor/src/main/java/me/nalab/user/web/adaptor/logined/LoginedUserGetController.java @@ -1,8 +1,7 @@ package me.nalab.user.web.adaptor.logined; -import org.springframework.http.HttpHeaders; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -18,7 +17,7 @@ public class LoginedUserGetController { private final LoginedUserGetByTokenUseCase loginedUserGetByTokenUseCase; @GetMapping("/users/logins") - public LoginedInfoResponse getLoginedUserByToken(@RequestHeader(HttpHeaders.AUTHORIZATION) String token) { + public LoginedInfoResponse getLoginedUserByToken(@RequestAttribute("tokenValue") String token) { return LoginedInfoResponse.of(loginedUserGetByTokenUseCase.getLoginedInfoByToken(token)); }