Skip to content

Commit

Permalink
refactor: #459 Token 발급 시 반환하는 형태 통일 (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
apptie authored and swonny committed Oct 6, 2023
1 parent 739deee commit b7f6f12
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@RequiredArgsConstructor
public class JwtEncoder implements TokenEncoder {

public static final String TOKEN_PREFIX = "Bearer ";

private final JwtConfigurationProperties jwtConfigurationProperties;

@Override
Expand All @@ -31,7 +33,7 @@ public String encode(
final String key = jwtConfigurationProperties.findTokenKey(tokenType);
final Long expiredHours = jwtConfigurationProperties.findExpiredHours(tokenType);

return Jwts.builder()
return TOKEN_PREFIX + Jwts.builder()
.setIssuedAt(targetDate)
.setExpiration(new Date(targetDate.getTime() + expiredHours * 60 * 60 * 1000L))
.addClaims(privateClaims)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.ddang.ddang.authentication.application;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;

import com.ddang.ddang.authentication.application.dto.TokenDto;
import com.ddang.ddang.authentication.application.exception.InvalidWithdrawalException;
import com.ddang.ddang.authentication.domain.Oauth2UserInformationProviderComposite;
Expand All @@ -22,6 +31,11 @@
import com.ddang.ddang.image.infrastructure.persistence.JpaProfileImageRepository;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.infrastructure.persistence.JpaUserRepository;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Map;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand All @@ -31,21 +45,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;

@IsolateDatabase
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
Expand Down Expand Up @@ -233,7 +232,7 @@ void setUp(
void refreshToken을_전달하면_새로운_accessToken을_반환한다() {
// given
final Map<String, Object> privateClaims = Map.of("userId", 1L);
final String refreshToken = "Bearer " + tokenEncoder.encode(
final String refreshToken = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.REFRESH,
privateClaims
Expand Down Expand Up @@ -288,7 +287,7 @@ void setUp(
void 유효한_accessToken을_검증하면_참을_반환한다() {
// given
final Map<String, Object> privateClaims = Map.of("userId", 1L);
final String accessToken = "Bearer " + tokenEncoder.encode(
final String accessToken = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.ACCESS,
privateClaims
Expand All @@ -308,7 +307,7 @@ void setUp(
final LocalDateTime expiredPublishTime = instant.atZone(ZoneId.of("UTC")).toLocalDateTime();

final Map<String, Object> privateClaims = Map.of("userId", 1L);
final String accessToken = "Bearer " + tokenEncoder.encode(
final String accessToken = tokenEncoder.encode(
expiredPublishTime,
TokenType.ACCESS,
privateClaims
Expand Down Expand Up @@ -337,12 +336,12 @@ void setUp(
deviceTokenRepository.save(deviceToken);

final Map<String, Object> privateClaims = Map.of("userId", 1L);
final String accessToken = "Bearer " + tokenEncoder.encode(
final String accessToken = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.ACCESS,
privateClaims
);
final String refreshToken = "Bearer " + tokenEncoder.encode(
final String refreshToken = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.REFRESH,
privateClaims
Expand Down Expand Up @@ -375,12 +374,12 @@ void setUp(
user.withdrawal();

final Map<String, Object> privateClaims = Map.of("userId", 1L);
final String accessToken = "Bearer " + tokenEncoder.encode(
final String accessToken = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.ACCESS,
privateClaims
);
final String refreshToken = "Bearer " + tokenEncoder.encode(
final String refreshToken = tokenEncoder.encode(
LocalDateTime.now(),
TokenType.REFRESH,
privateClaims
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ class BlackListTokenServiceTest {
void 블랙리스트로_등록된_토큰인지_확인할때_이미_블랙리스트로_등록된_토큰을_전달하면_참을_반환한다() {
// given
final Map<String, Object> privateClaims = Map.of("userId", 1L);
final String accessToken =
"Bearer " + tokenEncoder.encode(LocalDateTime.now(), TokenType.ACCESS, privateClaims);
final String refreshToken =
"Bearer " + tokenEncoder.encode(LocalDateTime.now(), TokenType.REFRESH, privateClaims);
final String accessToken = tokenEncoder.encode(LocalDateTime.now(), TokenType.ACCESS, privateClaims);
final String refreshToken = tokenEncoder.encode(LocalDateTime.now(), TokenType.REFRESH, privateClaims);

blackListTokenService.registerBlackListToken(accessToken, refreshToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void setUp() {
void 유효한_토큰이면_해당_토큰의_본문을_반환한다() {
// given
final Map<String, Object> privateClaims = Map.of("userId", 1L);
final String accessToken = "Bearer " + jwtEncoder.encode(LocalDateTime.now(), TokenType.ACCESS, privateClaims);
final String accessToken = jwtEncoder.encode(LocalDateTime.now(), TokenType.ACCESS, privateClaims);

// when
final Optional<PrivateClaims> actual = jwtDecoder.decode(TokenType.ACCESS, accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void setUp() {
final String actual = jwtEncoder.encode(LocalDateTime.now(), TokenType.ACCESS, privateClaims);

// then
assertThat(actual).isNotBlank();
assertThat(actual).isNotBlank()
.contains("Bearer ");
}
}

0 comments on commit b7f6f12

Please sign in to comment.