Skip to content

Commit

Permalink
refactor: OAuth2Client 인터페이스에서 code로 UserInfo 반환하도록 리팩터링 및 AccessToke…
Browse files Browse the repository at this point in the history
…n 네이밍 OAuth2Token으로 변경
  • Loading branch information
seokjin8678 committed May 27, 2024
1 parent 58b0680 commit ca51042
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

public interface OAuth2Client {

String getAccessToken(String code);

UserInfo getUserInfo(String accessToken);
UserInfo getUserInfo(String code);

SocialType getSocialType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public class MemberAuthFacadeService {

public LoginV1Response oAuth2Login(SocialType socialType, String code) {
OAuth2Client oAuth2Client = oAuth2Clients.getClient(socialType);
String oAuth2AccessToken = oAuth2Client.getAccessToken(code);
UserInfo userInfo = oAuth2Client.getUserInfo(oAuth2AccessToken);
UserInfo userInfo = oAuth2Client.getUserInfo(code);
return login(userInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record KakaoAccessTokenResponse(
public record KakaoOAuth2TokenResponse(
String accessToken,
String idToken
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,28 @@
import com.festago.common.exception.ErrorCode;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("!prod")
public class FestagoOAuth2Client implements OAuth2Client {

private static final String PROFILE_IMAGE = "https://placehold.co/150x150";

private final Map<String, Supplier<UserInfo>> userInfoMap = new HashMap<>();
private final Map<String, UserInfo> userInfoMap = new HashMap<>();

public FestagoOAuth2Client() {
userInfoMap.put("1", () -> new UserInfo("1", getSocialType(), "member1", PROFILE_IMAGE));
userInfoMap.put("2", () -> new UserInfo("2", getSocialType(), "member2", PROFILE_IMAGE));
userInfoMap.put("3", () -> new UserInfo("3", getSocialType(), "member3", PROFILE_IMAGE));
}

@Override
public String getAccessToken(String code) {
return code;
userInfoMap.put("1", new UserInfo("1", getSocialType(), "member1", null));
userInfoMap.put("2", new UserInfo("2", getSocialType(), "member2", null));
userInfoMap.put("3", new UserInfo("3", getSocialType(), "member3", null));
}

@Override
public UserInfo getUserInfo(String accessToken) {
return userInfoMap.getOrDefault(accessToken, () -> {
public UserInfo getUserInfo(String code) {
UserInfo userInfo = userInfoMap.get(code);
if (userInfo == null) {
throw new BadRequestException(ErrorCode.OAUTH2_INVALID_TOKEN);
}).get();
}
return userInfo;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
@RequiredArgsConstructor
public class KakaoOAuth2Client implements OAuth2Client {

private final KakaoOAuth2AccessTokenClient accessTokenClient;
private final KakaoOpenIdClient openIdClient;
private final KakaoOAuth2TokenClient kakaoOAuth2TokenClient;
private final KakaoOpenIdClient kakaoOpenIdClient;

@Override
public String getAccessToken(String code) {
return accessTokenClient.getAccessToken(code);
}

@Override
public UserInfo getUserInfo(String accessToken) {
return openIdClient.getUserInfo(accessToken);
public UserInfo getUserInfo(String code) {
String idToken = kakaoOAuth2TokenClient.getIdToken(code);
return kakaoOpenIdClient.getUserInfo(idToken);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.festago.auth.infrastructure.oauth2;

import com.festago.auth.dto.KakaoAccessTokenResponse;
import com.festago.auth.dto.KakaoOAuth2TokenResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpHeaders;
Expand All @@ -9,7 +9,7 @@
import org.springframework.web.client.RestTemplate;

@Component
public class KakaoOAuth2AccessTokenClient {
public class KakaoOAuth2TokenClient {

private static final String URL = "https://kauth.kakao.com/oauth/token";

Expand All @@ -19,7 +19,7 @@ public class KakaoOAuth2AccessTokenClient {
private final String redirectUri;
private final String clientSecret;

public KakaoOAuth2AccessTokenClient(
public KakaoOAuth2TokenClient(
@Value("${festago.oauth2.kakao.grant-type}") String grantType,
@Value("${festago.oauth2.kakao.rest-api-key}") String clientId,
@Value("${festago.oauth2.kakao.redirect-uri}") String redirectUri,
Expand All @@ -35,12 +35,13 @@ public KakaoOAuth2AccessTokenClient(
.build();
}

public String getAccessToken(String code) {
HttpHeaders headers = getAccessTokenHeaders(code);
return requestAccessToken(headers);
public String getIdToken(String code) {
KakaoOAuth2TokenResponse response = restTemplate.postForEntity(URL, getHeaders(code),
KakaoOAuth2TokenResponse.class).getBody();
return response.idToken();
}

private HttpHeaders getAccessTokenHeaders(String code) {
private HttpHeaders getHeaders(String code) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.set("grant_type", grantType);
Expand All @@ -50,10 +51,4 @@ private HttpHeaders getAccessTokenHeaders(String code) {
headers.set("code", code);
return headers;
}

private String requestAccessToken(HttpHeaders headers) {
KakaoAccessTokenResponse response = restTemplate.postForEntity(URL, headers,
KakaoAccessTokenResponse.class).getBody();
return response.accessToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.festago.auth.dto.KakaoAccessTokenResponse;
import com.festago.auth.infrastructure.oauth2.KakaoOAuth2AccessTokenClient;
import com.festago.auth.dto.KakaoOAuth2TokenResponse;
import com.festago.auth.infrastructure.oauth2.KakaoOAuth2TokenClient;
import com.festago.auth.infrastructure.oauth2.KakaoOAuth2AccessTokenErrorHandler.KakaoOAuth2ErrorResponse;
import com.festago.common.exception.BadRequestException;
import com.festago.common.exception.ErrorCode;
Expand All @@ -24,13 +24,13 @@

@DisplayNameGeneration(ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
@RestClientTest(KakaoOAuth2AccessTokenClient.class)
class KakaoOAuth2AccessTokenClientTest {
@RestClientTest(KakaoOAuth2TokenClient.class)
class KakaoOAuth2TokenClientTest {

private static final String URL = "https://kauth.kakao.com/oauth/token";

@Autowired
KakaoOAuth2AccessTokenClient kakaoOAuth2AccessTokenClient;
KakaoOAuth2TokenClient kakaoOAuth2TokenClient;

@Autowired
MockRestServiceServer mockServer;
Expand All @@ -48,7 +48,7 @@ class KakaoOAuth2AccessTokenClientTest {
.body(objectMapper.writeValueAsString(expected)));

// when & then
assertThatThrownBy(() -> kakaoOAuth2AccessTokenClient.getAccessToken("code"))
assertThatThrownBy(() -> kakaoOAuth2TokenClient.getIdToken("code"))
.isInstanceOf(BadRequestException.class)
.hasMessage(ErrorCode.OAUTH2_INVALID_CODE.getMessage());
}
Expand All @@ -63,7 +63,7 @@ class KakaoOAuth2AccessTokenClientTest {
.body(objectMapper.writeValueAsString(expected)));

// when & then
assertThatThrownBy(() -> kakaoOAuth2AccessTokenClient.getAccessToken("code"))
assertThatThrownBy(() -> kakaoOAuth2TokenClient.getIdToken("code"))
.isInstanceOf(InternalServerException.class)
.hasMessage(ErrorCode.OAUTH2_INVALID_REQUEST.getMessage());
}
Expand All @@ -76,7 +76,7 @@ class KakaoOAuth2AccessTokenClientTest {
.contentType(MediaType.APPLICATION_JSON));

// when & then
assertThatThrownBy(() -> kakaoOAuth2AccessTokenClient.getAccessToken("code"))
assertThatThrownBy(() -> kakaoOAuth2TokenClient.getIdToken("code"))
.isInstanceOf(InternalServerException.class)
.hasMessage(ErrorCode.OAUTH2_INVALID_REQUEST.getMessage());
}
Expand All @@ -89,23 +89,23 @@ class KakaoOAuth2AccessTokenClientTest {
.contentType(MediaType.APPLICATION_JSON));

// when & then
assertThatThrownBy(() -> kakaoOAuth2AccessTokenClient.getAccessToken("code"))
assertThatThrownBy(() -> kakaoOAuth2TokenClient.getIdToken("code"))
.isInstanceOf(InternalServerException.class)
.hasMessage(ErrorCode.OAUTH2_PROVIDER_NOT_RESPONSE.getMessage());
}

@Test
void 성공() throws JsonProcessingException {
// given
KakaoAccessTokenResponse expected = new KakaoAccessTokenResponse("accessToken", "idToken");
KakaoOAuth2TokenResponse expected = new KakaoOAuth2TokenResponse("accessToken", "idToken");
mockServer.expect(requestTo(URL))
.andRespond(MockRestResponseCreators.withSuccess()
.contentType(MediaType.APPLICATION_JSON)
.body(objectMapper.writeValueAsString(expected)));
// when
String actual = kakaoOAuth2AccessTokenClient.getAccessToken("code");
String actual = kakaoOAuth2TokenClient.getIdToken("code");

// then
assertThat(actual).isEqualTo(expected.accessToken());
assertThat(actual).isEqualTo(expected.idToken());
}
}

0 comments on commit ca51042

Please sign in to comment.