Skip to content

Commit

Permalink
refactor: TokenInfo를 record로 변경하고 token-value 부분을 받도록 수정한다 (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Mar 12, 2024
1 parent 38990cb commit 3988ace
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class JwtDecryptInterceptorConfigurer implements WebMvcConfigurer {
"/v1/gallerys/logins",
"/v1/gallerys",
"/v1/surveys/bookmarks*",
"/v1/users/logins",
};

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class MockAuthConfigurer implements WebMvcConfigurer {
"/v1/reviewers/summary*",
"/v2/surveys/*/feedbacks",
"/v1/surveys/*/bookmarks",
"/v1/users/logins",
};

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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));
}

Expand Down

0 comments on commit 3988ace

Please sign in to comment.