From 643d8be48cd7f0d56166b0578649a533e9fbadd1 Mon Sep 17 00:00:00 2001 From: heejjinkim <06.hjhj.12@gmail.com> Date: Tue, 3 Sep 2024 21:25:24 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20kakaoService=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 추후에 삭제 예정 related to: #11 --- .../_119/wepro/auth/service/KakaoService.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main/java/com/_119/wepro/auth/service/KakaoService.java diff --git a/src/main/java/com/_119/wepro/auth/service/KakaoService.java b/src/main/java/com/_119/wepro/auth/service/KakaoService.java new file mode 100644 index 0000000..2b47d87 --- /dev/null +++ b/src/main/java/com/_119/wepro/auth/service/KakaoService.java @@ -0,0 +1,73 @@ +package com._119.wepro.auth.service; + +import com._119.wepro.auth.dto.request.AuthRequest.SignInRequest; +import com._119.wepro.auth.dto.response.AuthResponse.SignInResponse; +import com._119.wepro.global.enums.Provider; +import com._119.wepro.auth.client.KakaoOauthClient; +import com._119.wepro.auth.dto.response.KakaoTokenResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +@Service +@RequiredArgsConstructor +public class KakaoService { + + private final KakaoOauthClient kakaoOauthClient; + + // 임시 + private static final String LOGIN_URI = "http://localhost:8080/login"; + + @Value("${kakao.client-id}") + private String CLIENT_ID; + + @Value("${kakao.redirect-uri}") + private String REDIRECT_URI; + + @Value("${kakao.client-secret}") + private String CLIENT_SECRET; + + @Value("${kakao.authorization-uri}") + private String KAKAO_AUTH_URL; + + // 백엔드용 + public String generateKakaoRedirectUrl() { + return UriComponentsBuilder.fromUriString(KAKAO_AUTH_URL) + .queryParam("client_id", CLIENT_ID) + .queryParam("redirect_uri", REDIRECT_URI) + .queryParam("response_type", "code") + .build() + .toUriString(); + } + + // 백엔드용 + public SignInResponse handleKakaoCallback(String code) { + KakaoTokenResponse tokenResponse = kakaoOauthClient.kakaoAuth(CLIENT_ID, REDIRECT_URI, code, + CLIENT_SECRET); + String idToken = tokenResponse.getIdToken(); + ResponseEntity response = callLoginApiWithIdToken(idToken); + + return response.getBody(); + } + + // 백엔드용 + private ResponseEntity callLoginApiWithIdToken(String idToken) { + RestTemplate restTemplate = new RestTemplate(); + SignInRequest signInRequest = new SignInRequest(Provider.KAKAO, idToken); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity requestEntity = new HttpEntity<>(signInRequest, headers); + + ResponseEntity response = restTemplate.postForEntity(LOGIN_URI, requestEntity, + SignInResponse.class); + + return response; + } +}