diff --git a/src/main/java/net/teumteum/auth/service/AuthService.java b/src/main/java/net/teumteum/auth/service/AuthService.java index ee91cce1..a2530fb9 100644 --- a/src/main/java/net/teumteum/auth/service/AuthService.java +++ b/src/main/java/net/teumteum/auth/service/AuthService.java @@ -49,7 +49,6 @@ private void checkRefreshTokenMatch(User user, String refreshToken) { private TokenResponse issueNewToken(User user) { - return new TokenResponse(jwtService.createAccessToken(user.getOauth().getOauthId()), - jwtService.createRefreshToken()); + return jwtService.createServiceToken(user); } } diff --git a/src/main/java/net/teumteum/core/security/service/JwtService.java b/src/main/java/net/teumteum/core/security/service/JwtService.java index fda0e80d..eb939017 100644 --- a/src/main/java/net/teumteum/core/security/service/JwtService.java +++ b/src/main/java/net/teumteum/core/security/service/JwtService.java @@ -18,7 +18,6 @@ import net.teumteum.core.property.JwtProperty; import net.teumteum.user.domain.User; import org.springframework.beans.factory.InitializingBean; -import org.springframework.security.oauth2.jwt.JwtException; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; @@ -60,12 +59,14 @@ public String extractRefreshToken(HttpServletRequest request) { public Long getUserIdFromToken(String token) { try { - return Long.valueOf(getClaims(token).get("id", String.class)); - } catch (Exception exception) { - throw new JwtException("Access Token is not valid"); + Claims claims = getClaims(token); + return claims.get("id", Long.class); + } catch (ExpiredJwtException exception) { + return Long.valueOf(exception.getClaims().get("id").toString()); } } + public TokenResponse createServiceToken(User users) { String accessToken = createAccessToken(users.getId().toString()); String refreshToken = createRefreshToken(); diff --git a/src/main/java/net/teumteum/core/security/service/RedisService.java b/src/main/java/net/teumteum/core/security/service/RedisService.java index 323263ac..5be3a95c 100644 --- a/src/main/java/net/teumteum/core/security/service/RedisService.java +++ b/src/main/java/net/teumteum/core/security/service/RedisService.java @@ -9,10 +9,10 @@ @RequiredArgsConstructor public class RedisService { - private final RedisTemplate redisTemplate; + private final RedisTemplate redisTemplate; public String getData(String key) { - return (String) redisTemplate.opsForValue().get(key); + return redisTemplate.opsForValue().get(key); } public void setData(String key, String value) { diff --git a/src/test/java/net/teumteum/unit/auth/service/AuthServiceTest.java b/src/test/java/net/teumteum/unit/auth/service/AuthServiceTest.java index 5e194698..3163c935 100644 --- a/src/test/java/net/teumteum/unit/auth/service/AuthServiceTest.java +++ b/src/test/java/net/teumteum/unit/auth/service/AuthServiceTest.java @@ -1,6 +1,8 @@ package net.teumteum.unit.auth.service; import static net.teumteum.core.security.Authenticated.네이버; +import static net.teumteum.unit.auth.common.SecurityValue.INVALID_ACCESS_TOKEN; +import static net.teumteum.unit.auth.common.SecurityValue.VALID_REFRESH_TOKEN; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; @@ -55,17 +57,16 @@ void Return_new_jwt_if_access_and_refresh_is_exist() { HttpServletRequest httpServletRequest = mock(HttpServletRequest.class); - given(jwtService.extractAccessToken(any(HttpServletRequest.class))).willReturn("access token"); + given(jwtService.extractAccessToken(any(HttpServletRequest.class))).willReturn(INVALID_ACCESS_TOKEN); - given(jwtService.extractRefreshToken(any(HttpServletRequest.class))).willReturn("refresh token"); + given(jwtService.extractRefreshToken(any(HttpServletRequest.class))).willReturn(VALID_REFRESH_TOKEN); given(jwtService.getUserIdFromToken(anyString())).willReturn(1L); - given(jwtService.createAccessToken(anyString())).willReturn("new access token"); - - given(jwtService.createRefreshToken()).willReturn("new refresh token"); + given(jwtService.createServiceToken(any(User.class))).willReturn( + TokenResponse.builder().accessToken("access token").refreshToken("refresh token").build()); - given(redisService.getData(anyString())).willReturn("refresh token"); + given(redisService.getData(anyString())).willReturn(VALID_REFRESH_TOKEN); given(userConnector.findUserById(anyLong())).willReturn(user); @@ -76,8 +77,8 @@ void Return_new_jwt_if_access_and_refresh_is_exist() { // then assertThat(response).isNotNull(); - assertThat(response.getAccessToken()).isEqualTo("new access token"); - assertThat(response.getRefreshToken()).isEqualTo("new refresh token"); + assertThat(response.getAccessToken()).isEqualTo("access token"); + assertThat(response.getRefreshToken()).isEqualTo("refresh token"); verify(userConnector, times(1)).findUserById(anyLong()); verify(jwtService, times(1)).validateToken(any()); }