Skip to content

Commit

Permalink
refactor: LoginedUserController에서 tokenValue part를 받도록 수정한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Mar 10, 2024
1 parent 1a98784 commit e8687be
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 27 deletions.
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
Expand Up @@ -23,17 +23,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 tokenInfo = loginedUserGetByTokenPort.decryptToken(encryptedToken);
var user = userGetPort.getById(tokenInfo.userId());
return LoginedInfo.from(tokenInfo.targetId(), user);
}

private void throwIfInvalidToken(String[] split) {
if(split.length < 2) {
throw new InvalidTokenException(split[0]);
}
}

}
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 e8687be

Please sign in to comment.