Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] refactor: KakaoOAuth2Client UserInfo 반환 KakaoOpenIdClient 사용하도록 변경 (#994) #995

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,12 +4,9 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record KakaoAccessTokenResponse(
String tokenType,
public record KakaoOAuth2TokenResponse(
String accessToken,
Integer expiresIn,
String refreshToken,
Integer refreshTokenExpiresIn
String idToken
) {

}
33 changes: 0 additions & 33 deletions backend/src/main/java/com/festago/auth/dto/KakaoUserInfo.java

This file was deleted.

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 @@ -3,24 +3,21 @@
import com.festago.auth.application.OAuth2Client;
import com.festago.auth.domain.SocialType;
import com.festago.auth.domain.UserInfo;
import com.festago.auth.infrastructure.openid.KakaoOpenIdClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class KakaoOAuth2Client implements OAuth2Client {

private final KakaoOAuth2AccessTokenClient accessTokenClient;
private final KakaoOAuth2UserInfoClient userInfoClient;
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 userInfoClient.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();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public UserInfo getUserInfo(String idToken) {
return UserInfo.builder()
.socialType(SocialType.KAKAO)
.socialId(payload.getSubject())
.nickname(payload.get("nickname", String.class))
.profileImage(payload.get("picture", String.class))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import com.festago.auth.application.OAuth2Clients;
import com.festago.auth.application.OAuth2Clients.OAuth2ClientsBuilder;
import com.festago.auth.infrastructure.oauth2.FestagoOAuth2Client;
import com.festago.auth.infrastructure.oauth2.KakaoOAuth2AccessTokenClient;
import com.festago.auth.infrastructure.oauth2.KakaoOAuth2Client;
import com.festago.auth.infrastructure.oauth2.KakaoOAuth2UserInfoClient;
import com.festago.common.exception.BadRequestException;
import com.festago.common.exception.UnexpectedException;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand Down Expand Up @@ -53,10 +51,7 @@ class OAuth2ClientsTest {
void 여러_타입의_클라이언트가_주어졌을때_타입으로_찾기_성공() {
// given
FestagoOAuth2Client festagoOAuth2Client = new FestagoOAuth2Client();
KakaoOAuth2Client kakaoOAuth2Client = new KakaoOAuth2Client(
mock(KakaoOAuth2AccessTokenClient.class),
mock(KakaoOAuth2UserInfoClient.class)
);
KakaoOAuth2Client kakaoOAuth2Client = new KakaoOAuth2Client(mock(), mock());

OAuth2Clients oAuth2Clients = OAuth2Clients.builder()
.add(festagoOAuth2Client)
Expand Down
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,24 +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("tokenType", "accessToken",
100, "refreshToken", 50);
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());
}
}
Loading
Loading