Skip to content

Commit

Permalink
Merge branch 'develop' into iss-#124
Browse files Browse the repository at this point in the history
  • Loading branch information
choidongkuen authored Jan 22, 2024
2 parents 88dda97 + 05dcd4c commit 82ad017
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/main/java/net/teumteum/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@RequiredArgsConstructor
public class RedisService {


private static final String HASH_KEY = "userLocation";
private final RedisTemplate<String, String> redisTemplate;
private final ObjectMapper objectMapper;
Expand All @@ -29,7 +30,6 @@ void init() {
valueOperations = redisTemplate.opsForValue();
}


public String getData(String key) {
return valueOperations.get(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public record UserGetResponse(
String status,
String goal,
Job job,
List<String> interests
List<String> interests,
int friends
) {

public static UserGetResponse of(User user) {
Expand All @@ -33,7 +34,8 @@ public static UserGetResponse of(User user) {
user.getStatus().name(),
user.getGoal(),
Job.of(user),
user.getInterests()
user.getInterests(),
user.getFriends().size()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public record UserMeGetResponse(
String status,
String goal,
Job job,
String oauthType,
List<String> interests
List<String> interests,
int friends
) {

public static UserMeGetResponse of(User user) {
Expand All @@ -35,11 +35,12 @@ public static UserMeGetResponse of(User user) {
user.getStatus().name(),
user.getGoal(),
Job.of(user),
user.getOauth().getAuthenticated().name(),
user.getInterests()
user.getInterests(),
user.getFriends().size()
);
}


public record Job(String name, boolean certificated, @JsonProperty("class") String jobClass, String detailClass) {

public static Job of(User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public record UserGetResponse(
String status,
String goal,
Job job,
List<String> interests
List<String> interests,
int friends
) {

public static UserGetResponse of(User user) {
Expand All @@ -44,7 +45,8 @@ public static UserGetResponse of(User user) {
user.getStatus().name(),
user.getGoal(),
Job.of(user),
user.getInterests()
user.getInterests(),
user.getFriends().size()
);
}

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

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

0 comments on commit 82ad017

Please sign in to comment.