From 58b068097419f6c7853dd9ce605265dedf2bd10e Mon Sep 17 00:00:00 2001 From: seokjin8678 Date: Mon, 27 May 2024 15:27:43 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20KakaoOAuth2Client=20UserInfo=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=20KakaoOpenIdClient=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/dto/KakaoAccessTokenResponse.java | 5 +- .../com/festago/auth/dto/KakaoUserInfo.java | 33 ------ .../oauth2/KakaoOAuth2Client.java | 5 +- .../oauth2/KakaoOAuth2UserInfoClient.java | 43 ------- .../KakaoOAuth2UserInfoErrorHandler.java | 36 ------ .../openid/KakaoOpenIdClient.java | 2 - .../auth/domain/OAuth2ClientsTest.java | 7 +- .../KakaoOAuth2AccessTokenClientTest.java | 3 +- .../KakaoOAuth2UserInfoClientTest.java | 105 ------------------ 9 files changed, 6 insertions(+), 233 deletions(-) delete mode 100644 backend/src/main/java/com/festago/auth/dto/KakaoUserInfo.java delete mode 100644 backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoClient.java delete mode 100644 backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoErrorHandler.java delete mode 100644 backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2UserInfoClientTest.java diff --git a/backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java b/backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java index 3fbfe7585..462da6ec0 100644 --- a/backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java +++ b/backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java @@ -5,11 +5,8 @@ @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) public record KakaoAccessTokenResponse( - String tokenType, String accessToken, - Integer expiresIn, - String refreshToken, - Integer refreshTokenExpiresIn + String idToken ) { } diff --git a/backend/src/main/java/com/festago/auth/dto/KakaoUserInfo.java b/backend/src/main/java/com/festago/auth/dto/KakaoUserInfo.java deleted file mode 100644 index e82122847..000000000 --- a/backend/src/main/java/com/festago/auth/dto/KakaoUserInfo.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.festago.auth.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import com.festago.auth.domain.SocialType; -import com.festago.auth.domain.UserInfo; - -public record KakaoUserInfo( - String id, - @JsonProperty("kakao_account") KakaoAccount kakaoAccount -) { - - public UserInfo toUserInfo() { - return UserInfo.builder() - .socialId(id) - .socialType(SocialType.KAKAO) - .nickname(kakaoAccount.profile.nickname) - .profileImage(kakaoAccount.profile.thumbnailImageUrl) - .build(); - } - - public record KakaoAccount( - Profile profile - ) { - - public record Profile( - String nickname, - @JsonProperty("thumbnail_image_url") - String thumbnailImageUrl - ) { - - } - } -} diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java index 36f64c87a..7aee387da 100644 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java +++ b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java @@ -3,6 +3,7 @@ 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; @@ -11,7 +12,7 @@ public class KakaoOAuth2Client implements OAuth2Client { private final KakaoOAuth2AccessTokenClient accessTokenClient; - private final KakaoOAuth2UserInfoClient userInfoClient; + private final KakaoOpenIdClient openIdClient; @Override public String getAccessToken(String code) { @@ -20,7 +21,7 @@ public String getAccessToken(String code) { @Override public UserInfo getUserInfo(String accessToken) { - return userInfoClient.getUserInfo(accessToken); + return openIdClient.getUserInfo(accessToken); } @Override diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoClient.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoClient.java deleted file mode 100644 index a725e5d92..000000000 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoClient.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.festago.auth.infrastructure.oauth2; - -import com.festago.auth.domain.UserInfo; -import com.festago.auth.dto.KakaoUserInfo; -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -@Component -public class KakaoOAuth2UserInfoClient { - - private static final String URL = "https://kapi.kakao.com/v2/user/me"; - - private final RestTemplate restTemplate; - - public KakaoOAuth2UserInfoClient(RestTemplateBuilder restTemplateBuilder) { - this.restTemplate = restTemplateBuilder - .errorHandler(new KakaoOAuth2UserInfoErrorHandler()) - .build(); - } - - public UserInfo getUserInfo(String accessToken) { - HttpHeaders headers = getUserInfoHeaders(accessToken); - return requestUserInfo(headers); - } - - private HttpHeaders getUserInfoHeaders(String accessToken) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - headers.setBearerAuth(accessToken); - return headers; - } - - private UserInfo requestUserInfo(HttpHeaders headers) { - KakaoUserInfo kakaoUserInfo = restTemplate.postForEntity(URL, new HttpEntity<>(headers), - KakaoUserInfo.class) - .getBody(); - return kakaoUserInfo.toUserInfo(); - } -} diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoErrorHandler.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoErrorHandler.java deleted file mode 100644 index 74e139bf6..000000000 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2UserInfoErrorHandler.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.festago.auth.infrastructure.oauth2; - -import com.festago.common.exception.BadRequestException; -import com.festago.common.exception.ErrorCode; -import com.festago.common.exception.InternalServerException; -import java.io.IOException; -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatusCode; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.web.client.DefaultResponseErrorHandler; - -@Slf4j -public class KakaoOAuth2UserInfoErrorHandler extends DefaultResponseErrorHandler { - - @Override - public void handleError(ClientHttpResponse response) throws IOException { - HttpStatusCode statusCode = response.getStatusCode(); - handle4xxError(statusCode); - handle5xxError(statusCode); - log.error("카카오 OAuth2 요청 중 알 수 없는 문제가 발생했습니다."); - throw new InternalServerException(ErrorCode.INTERNAL_SERVER_ERROR); - } - - private void handle4xxError(HttpStatusCode statusCode) { - if (statusCode.is4xxClientError()) { - throw new BadRequestException(ErrorCode.OAUTH2_INVALID_TOKEN); - } - } - - private void handle5xxError(HttpStatusCode statusCode) { - if (statusCode.is5xxServerError()) { - log.warn("카카오 OAuth2 요청에 500 에러가 발생했습니다."); - throw new InternalServerException(ErrorCode.OAUTH2_PROVIDER_NOT_RESPONSE); - } - } -} diff --git a/backend/src/main/java/com/festago/auth/infrastructure/openid/KakaoOpenIdClient.java b/backend/src/main/java/com/festago/auth/infrastructure/openid/KakaoOpenIdClient.java index 09e1c4cd1..dffcb064c 100644 --- a/backend/src/main/java/com/festago/auth/infrastructure/openid/KakaoOpenIdClient.java +++ b/backend/src/main/java/com/festago/auth/infrastructure/openid/KakaoOpenIdClient.java @@ -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(); } diff --git a/backend/src/test/java/com/festago/auth/domain/OAuth2ClientsTest.java b/backend/src/test/java/com/festago/auth/domain/OAuth2ClientsTest.java index dea7fe991..7390080d5 100644 --- a/backend/src/test/java/com/festago/auth/domain/OAuth2ClientsTest.java +++ b/backend/src/test/java/com/festago/auth/domain/OAuth2ClientsTest.java @@ -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; @@ -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) diff --git a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java b/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java index fd9b99077..7d2a6d288 100644 --- a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java +++ b/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java @@ -97,8 +97,7 @@ class KakaoOAuth2AccessTokenClientTest { @Test void 성공() throws JsonProcessingException { // given - KakaoAccessTokenResponse expected = new KakaoAccessTokenResponse("tokenType", "accessToken", - 100, "refreshToken", 50); + KakaoAccessTokenResponse expected = new KakaoAccessTokenResponse("accessToken", "idToken"); mockServer.expect(requestTo(URL)) .andRespond(MockRestResponseCreators.withSuccess() .contentType(MediaType.APPLICATION_JSON) diff --git a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2UserInfoClientTest.java b/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2UserInfoClientTest.java deleted file mode 100644 index 73b459a5e..000000000 --- a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2UserInfoClientTest.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.festago.auth.infrastructure; - -import static com.festago.common.exception.ErrorCode.OAUTH2_INVALID_TOKEN; -import static com.festago.common.exception.ErrorCode.OAUTH2_PROVIDER_NOT_RESPONSE; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.assertj.core.api.SoftAssertions.assertSoftly; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.festago.auth.domain.SocialType; -import com.festago.auth.domain.UserInfo; -import com.festago.auth.dto.KakaoUserInfo; -import com.festago.auth.dto.KakaoUserInfo.KakaoAccount; -import com.festago.auth.dto.KakaoUserInfo.KakaoAccount.Profile; -import com.festago.auth.infrastructure.oauth2.KakaoOAuth2UserInfoClient; -import com.festago.common.exception.BadRequestException; -import com.festago.common.exception.InternalServerException; -import org.junit.jupiter.api.DisplayNameGeneration; -import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.test.web.client.MockRestServiceServer; -import org.springframework.test.web.client.response.MockRestResponseCreators; - -@DisplayNameGeneration(ReplaceUnderscores.class) -@SuppressWarnings("NonAsciiCharacters") -@RestClientTest(KakaoOAuth2UserInfoClient.class) -class KakaoOAuth2UserInfoClientTest { - - private static final String URL = "https://kapi.kakao.com/v2/user/me"; - - @Autowired - KakaoOAuth2UserInfoClient kakaoOAuth2UserInfoClient; - - @Autowired - MockRestServiceServer mockServer; - - @Autowired - ObjectMapper objectMapper; - - @Test - void 상태코드가_400이면_InternalServer_예외() { - // given - mockServer.expect(requestTo(URL)) - .andRespond(MockRestResponseCreators.withBadRequest() - .contentType(MediaType.APPLICATION_JSON)); - - // when & then - assertThatThrownBy(() -> kakaoOAuth2UserInfoClient.getUserInfo("accessToken")) - .isInstanceOf(BadRequestException.class) - .hasMessage(OAUTH2_INVALID_TOKEN.getMessage()); - } - - @Test - void 상태코드가_401이면_InternalServer_예외() { - // given - mockServer.expect(requestTo(URL)) - .andRespond(MockRestResponseCreators.withUnauthorizedRequest() - .contentType(MediaType.APPLICATION_JSON)); - - // when & then - assertThatThrownBy(() -> kakaoOAuth2UserInfoClient.getUserInfo("accessToken")) - .isInstanceOf(BadRequestException.class) - .hasMessage(OAUTH2_INVALID_TOKEN.getMessage()); - } - - @Test - void 상태코드가_5xx이면_InternalServer_예외() { - // given - mockServer.expect(requestTo(URL)) - .andRespond(MockRestResponseCreators.withStatus(HttpStatus.INTERNAL_SERVER_ERROR) - .contentType(MediaType.APPLICATION_JSON)); - - // when & then - assertThatThrownBy(() -> kakaoOAuth2UserInfoClient.getUserInfo("accessToken")) - .isInstanceOf(InternalServerException.class) - .hasMessage(OAUTH2_PROVIDER_NOT_RESPONSE.getMessage()); - } - - @Test - void 성공() throws JsonProcessingException { - // given - KakaoUserInfo expected = new KakaoUserInfo("id", new KakaoAccount(new Profile("nickname", "imageUrl"))); - mockServer.expect(requestTo(URL)) - .andRespond(MockRestResponseCreators.withSuccess() - .body(objectMapper.writeValueAsString(expected)) - .contentType(MediaType.APPLICATION_JSON)); - - // when - UserInfo actual = kakaoOAuth2UserInfoClient.getUserInfo("accessToken"); - - // then - assertSoftly(softly -> { - softly.assertThat(actual.socialType()).isEqualTo(SocialType.KAKAO); - softly.assertThat(actual.socialId()).isEqualTo(expected.id()); - softly.assertThat(actual.nickname()).isEqualTo(expected.kakaoAccount().profile().nickname()); - softly.assertThat(actual.profileImage()) - .isEqualTo(expected.kakaoAccount().profile().thumbnailImageUrl()); - }); - } -} From ca51042fd64aed2b1757740c2993c5ea4ebc4ccc Mon Sep 17 00:00:00 2001 From: seokjin8678 Date: Mon, 27 May 2024 16:05:16 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20OAuth2Client=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=EC=97=90=EC=84=9C=20code?= =?UTF-8?q?=EB=A1=9C=20UserInfo=20=EB=B0=98=ED=99=98=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=A6=AC=ED=8C=A9=ED=84=B0=EB=A7=81=20=EB=B0=8F=20?= =?UTF-8?q?AccessToken=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20OAuth2Token?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/application/OAuth2Client.java | 4 +--- .../command/MemberAuthFacadeService.java | 3 +-- ...nse.java => KakaoOAuth2TokenResponse.java} | 2 +- .../oauth2/FestagoOAuth2Client.java | 24 +++++++------------ .../oauth2/KakaoOAuth2Client.java | 14 ++++------- ...lient.java => KakaoOAuth2TokenClient.java} | 21 +++++++--------- ...t.java => KakaoOAuth2TokenClientTest.java} | 24 +++++++++---------- 7 files changed, 37 insertions(+), 55 deletions(-) rename backend/src/main/java/com/festago/auth/dto/{KakaoAccessTokenResponse.java => KakaoOAuth2TokenResponse.java} (87%) rename backend/src/main/java/com/festago/auth/infrastructure/oauth2/{KakaoOAuth2AccessTokenClient.java => KakaoOAuth2TokenClient.java} (73%) rename backend/src/test/java/com/festago/auth/infrastructure/{KakaoOAuth2AccessTokenClientTest.java => KakaoOAuth2TokenClientTest.java} (82%) diff --git a/backend/src/main/java/com/festago/auth/application/OAuth2Client.java b/backend/src/main/java/com/festago/auth/application/OAuth2Client.java index 597cba378..ad2d3fad4 100644 --- a/backend/src/main/java/com/festago/auth/application/OAuth2Client.java +++ b/backend/src/main/java/com/festago/auth/application/OAuth2Client.java @@ -5,9 +5,7 @@ public interface OAuth2Client { - String getAccessToken(String code); - - UserInfo getUserInfo(String accessToken); + UserInfo getUserInfo(String code); SocialType getSocialType(); } diff --git a/backend/src/main/java/com/festago/auth/application/command/MemberAuthFacadeService.java b/backend/src/main/java/com/festago/auth/application/command/MemberAuthFacadeService.java index 3980691f3..f028d3b99 100644 --- a/backend/src/main/java/com/festago/auth/application/command/MemberAuthFacadeService.java +++ b/backend/src/main/java/com/festago/auth/application/command/MemberAuthFacadeService.java @@ -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); } diff --git a/backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java b/backend/src/main/java/com/festago/auth/dto/KakaoOAuth2TokenResponse.java similarity index 87% rename from backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java rename to backend/src/main/java/com/festago/auth/dto/KakaoOAuth2TokenResponse.java index 462da6ec0..e1a44b028 100644 --- a/backend/src/main/java/com/festago/auth/dto/KakaoAccessTokenResponse.java +++ b/backend/src/main/java/com/festago/auth/dto/KakaoOAuth2TokenResponse.java @@ -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 ) { diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/FestagoOAuth2Client.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/FestagoOAuth2Client.java index 1470b330e..8010da157 100644 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/FestagoOAuth2Client.java +++ b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/FestagoOAuth2Client.java @@ -7,7 +7,6 @@ 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; @@ -15,26 +14,21 @@ @Profile("!prod") public class FestagoOAuth2Client implements OAuth2Client { - private static final String PROFILE_IMAGE = "https://placehold.co/150x150"; - - private final Map> userInfoMap = new HashMap<>(); + private final Map 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 diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java index 7aee387da..3421faf25 100644 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java +++ b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2Client.java @@ -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 diff --git a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2AccessTokenClient.java b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2TokenClient.java similarity index 73% rename from backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2AccessTokenClient.java rename to backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2TokenClient.java index 66c074b54..59ea6a145 100644 --- a/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2AccessTokenClient.java +++ b/backend/src/main/java/com/festago/auth/infrastructure/oauth2/KakaoOAuth2TokenClient.java @@ -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; @@ -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"; @@ -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, @@ -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); @@ -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(); - } } diff --git a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java b/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2TokenClientTest.java similarity index 82% rename from backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java rename to backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2TokenClientTest.java index 7d2a6d288..05b1125b7 100644 --- a/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2AccessTokenClientTest.java +++ b/backend/src/test/java/com/festago/auth/infrastructure/KakaoOAuth2TokenClientTest.java @@ -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; @@ -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; @@ -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()); } @@ -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()); } @@ -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()); } @@ -89,7 +89,7 @@ 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()); } @@ -97,15 +97,15 @@ class KakaoOAuth2AccessTokenClientTest { @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()); } }