|
| 1 | +package com.unit.daybook.domain.auth.kakao; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import org.springframework.beans.factory.annotation.Value; |
| 5 | +import org.springframework.http.HttpEntity; |
| 6 | +import org.springframework.http.HttpHeaders; |
| 7 | +import org.springframework.http.HttpMethod; |
| 8 | +import org.springframework.http.MediaType; |
| 9 | +import org.springframework.stereotype.Component; |
| 10 | +import org.springframework.util.LinkedMultiValueMap; |
| 11 | +import org.springframework.util.MultiValueMap; |
| 12 | + |
| 13 | +import com.unit.daybook.domain.auth.common.OAuthApiClient; |
| 14 | +import com.unit.daybook.domain.auth.common.OAuthInfoResponse; |
| 15 | +import com.unit.daybook.domain.auth.common.OAuthLoginParams; |
| 16 | +import com.unit.daybook.domain.auth.dto.request.OauthProvider; |
| 17 | +import com.unit.daybook.global.error.exception.CustomException; |
| 18 | +import com.unit.daybook.global.error.exception.ErrorCode; |
| 19 | +import org.springframework.web.client.RestTemplate; |
| 20 | + |
| 21 | +@Component |
| 22 | +@RequiredArgsConstructor |
| 23 | +public class KakaoApiClient implements OAuthApiClient { |
| 24 | + private static final String GRANT_TYPE = "authorization_code"; |
| 25 | + |
| 26 | + @Value("${oauth.kakao.url.auth}") |
| 27 | + private String authUrl; |
| 28 | + |
| 29 | + @Value("${oauth.kakao.url.api}") |
| 30 | + private String apiUrl; |
| 31 | + |
| 32 | + @Value("${oauth.kakao.client-id}") |
| 33 | + private String clientId; |
| 34 | + |
| 35 | + @Value("${oauth.kakao.client-secret") |
| 36 | + private String clientSecret; |
| 37 | + |
| 38 | + private final RestTemplate restTemplate; |
| 39 | + |
| 40 | + @Override |
| 41 | + public OauthProvider oAuthProvider() { |
| 42 | + return OauthProvider.KAKAO; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String requestAccessToken(OAuthLoginParams params) { |
| 47 | + String url = authUrl + "/oauth/token"; |
| 48 | + |
| 49 | + HttpHeaders httpHeaders = new HttpHeaders(); |
| 50 | + httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| 51 | + |
| 52 | + MultiValueMap<String, String> body = params.makeBody(); |
| 53 | + body.add("grant_type", GRANT_TYPE); |
| 54 | + body.add("client_id", clientId); |
| 55 | + body.add("client_secret", clientSecret); |
| 56 | + |
| 57 | + HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, httpHeaders); |
| 58 | + |
| 59 | + KakaoTokens response = restTemplate.exchange(url, HttpMethod.POST, request, KakaoTokens.class).getBody(); |
| 60 | + |
| 61 | + if (response == null) { |
| 62 | + throw new CustomException(ErrorCode.KAKAO_RESPONSE_NOT_FOUND); |
| 63 | + } |
| 64 | + return response.getAccessToken(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public OAuthInfoResponse requestOAuthInfo(String accessToken) { |
| 69 | + String url = apiUrl + "/v2/user/me"; |
| 70 | + |
| 71 | + HttpHeaders httpHeaders = new HttpHeaders(); |
| 72 | + httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| 73 | + httpHeaders.set("Authorization", "Bearer " + accessToken); |
| 74 | + |
| 75 | + MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); |
| 76 | + body.add("property_keys", "[\"id\", \"kakao_account.\", \"properties.\", \"has_signed_up.\"]"); |
| 77 | + |
| 78 | + HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, httpHeaders); |
| 79 | + |
| 80 | + return restTemplate.exchange(url, HttpMethod.POST, request, KakaoInfoResponse.class).getBody(); |
| 81 | + } |
| 82 | +} |
0 commit comments