diff --git a/src/main/java/com/moneymong/global/security/oauth/handler/KakaoService.java b/src/main/java/com/moneymong/global/security/oauth/handler/KakaoService.java index e889907..9687b9e 100644 --- a/src/main/java/com/moneymong/global/security/oauth/handler/KakaoService.java +++ b/src/main/java/com/moneymong/global/security/oauth/handler/KakaoService.java @@ -1,5 +1,8 @@ package com.moneymong.global.security.oauth.handler; +import com.moneymong.domain.user.entity.User; +import com.moneymong.domain.user.repository.UserRepository; +import com.moneymong.global.exception.custom.NotFoundException; import com.moneymong.global.exception.enums.ErrorCode; import com.moneymong.global.security.oauth.dto.KakaoUserData; import com.moneymong.global.security.oauth.dto.OAuthUserDataRequest; @@ -11,6 +14,8 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.stereotype.Component; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestClientResponseException; import org.springframework.web.client.RestTemplate; @@ -21,10 +26,14 @@ public class KakaoService implements OAuthAuthenticationHandler { private final RestTemplate restTemplate; + private final UserRepository userRepository; @Value("${spring.security.oauth2.kakao.host}") private String host; + @Value("${spring.security.oauth2.kakao.admin-key}") + private String adminKey; + @Override public OAuthProvider getAuthProvider() { return OAuthProvider.KAKAO; @@ -70,6 +79,39 @@ public OAuthUserDataResponse getOAuthUserData(OAuthUserDataRequest request) { @Override public void unlink(Long userId) { + String oauthId = userRepository.findById(userId) + .orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND)) + .getOauthId(); + + String url = host + "/v1/user/unlink"; + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + httpHeaders.add("Authorization", "KakaoAK " + adminKey); + + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("target_id_type", "user_id"); + body.add("target_id", oauthId); + HttpEntity httpRequest = new HttpEntity<>(body, httpHeaders); + + try { + ResponseEntity response = restTemplate.exchange( + url, + HttpMethod.POST, + httpRequest, + KakaoUserData.class + ); + assert response.getBody() != null; + + } catch (RestClientException e) { + log.warn("[KakaoService] failed to unlink User = {}", oauthId); + + if (e instanceof RestClientResponseException) { + throw new HttpClientException(ErrorCode.INVALID_OAUTH_TOKEN); + } + + throw new HttpClientException(ErrorCode.HTTP_CLIENT_REQUEST_FAILED); + } } } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 0659603..7f72306 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -11,6 +11,7 @@ spring: oauth2: kakao: host: https://kapi.kakao.com + admin-key: ${KAKAO_ADMIN_KEY} apple: host: https://appleid.apple.com grant-type: authorization_code diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 0659603..7f72306 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -11,6 +11,7 @@ spring: oauth2: kakao: host: https://kapi.kakao.com + admin-key: ${KAKAO_ADMIN_KEY} apple: host: https://appleid.apple.com grant-type: authorization_code