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

[refac] 회원 탈퇴 시 카카오 서버와 연결 끊기 #52

Merged
merged 3 commits into from
Jul 11, 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 @@ -38,7 +38,7 @@ public HankkiResponse<Void> signOut(
public HankkiResponse<Void> withdraw(
@UserId final Long userId,
@Nullable @RequestHeader("X-Apple-Code") final String code){
authService.withdraw(userId,code);
authService.withdraw(userId, code);
return HankkiResponse.success(CommonSuccessCode.NO_CONTENT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.hankki.hankkiserver.api.auth.service.response.UserLoginResponse;
import org.hankki.hankkiserver.api.auth.service.response.UserReissueResponse;
import org.hankki.hankkiserver.external.openfeign.kakao.KakaoOAuthProvider;
import org.hankki.hankkiserver.external.openfeign.kakao.dto.KakaoUnlinkRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -30,6 +32,9 @@
@RequiredArgsConstructor
public class AuthService {

@Value("${oauth.kakao.key}")
private String adminKey;

private final UserFinder userFinder;
private final UserSaver userSaver;
private final UserInfoFinder userInfoFinder;
Expand All @@ -40,9 +45,7 @@ public class AuthService {
private final KakaoOAuthProvider kakaoOAuthProvider;
private final AppleOAuthProvider appleOAuthProvider;

public UserLoginResponse login(
final String token,
final UserLoginRequest request) {
public UserLoginResponse login(final String token, final UserLoginRequest request) {
Platform platform = Platform.getEnumPlatformFromStringPlatform(request.platform());
SocialInfoDto socialInfo = getSocialInfo(token, platform, request.name());
boolean isRegistered = userFinder.isRegisteredUser(platform, socialInfo);
Expand All @@ -60,12 +63,15 @@ public void withdraw(final Long userId, final String code) {
User user = userFinder.getUser(userId);
if (APPLE == user.getPlatform()){
try {
String refreshToken = appleOAuthProvider.getAppleToken(code);
String refreshToken = appleOAuthProvider.getAppleRefreshToken(code);
appleOAuthProvider.requestRevoke(refreshToken);
} catch (Exception e) {
throw new BadRequestException(AuthErrorCode.APPLE_REVOKE_FAILED);
}
}
if (KAKAO == user.getPlatform()) {
kakaoOAuthProvider.unlinkKakaoServer(adminKey, KakaoUnlinkRequest.of(user.getSerialId()));
}
user.softDelete();
userInfoDeleter.softDelete(userId);
}
Expand All @@ -88,14 +94,11 @@ private Token generateTokens(final Long userId) {
return issuedTokens;
}

private String getUserRole(Long userId) {
private String getUserRole(final Long userId) {
return userFinder.getUser(userId).getUserRole().getValue();
}

private SocialInfoDto getSocialInfo(
final String providerToken,
final Platform platform,
final String name) {
private SocialInfoDto getSocialInfo(final String providerToken, final Platform platform, final String name) {
if (KAKAO == platform){
return kakaoOAuthProvider.getKakaoUserInfo(providerToken);
}
Expand All @@ -116,9 +119,9 @@ private User loadOrCreateUser(final Platform platform, final SocialInfoDto socia
});
}


private User updateUserInfo(final User user) {
user.updateStatus(ACTIVE);
user.updateDeletedAt(null);
userInfoFinder.getUserInfo(user.getId()).updateNickname(user.getName());
return user;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ public void softDelete() {
public void updateStatus(MemberStatus memberStatus) {
this.memberStatus = memberStatus;
}

public void updateDeletedAt(LocalDateTime deletedAt) {
this.deletedAt = deletedAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public SocialInfoDto getAppleUserInfo(final String identityToken, final String n
claims.get("email").toString());
}

public String getAppleToken(final String code) {
public String getAppleRefreshToken(final String code) {
try {
String clientSecret = appleClientSecretGenerator.generateClientSecret();
AppleTokenResponse appleTokenResponse = appleFeignClient.getAppleTokens(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package org.hankki.hankkiserver.external.openfeign.kakao;

import org.hankki.hankkiserver.external.openfeign.kakao.dto.KakaoUnlinkRequest;
import org.hankki.hankkiserver.external.openfeign.kakao.dto.KakaoUserInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.*;

@FeignClient(name = "kakaoClient", url = "https://kapi.kakao.com/v2/user/me")
@FeignClient(name = "kakaoClient", url = "https://kapi.kakao.com")
public interface KakaoFeignClient {

@GetMapping
@GetMapping("/v2/user/me")
KakaoUserInfo getUserInfo(
@RequestHeader(HttpHeaders.AUTHORIZATION) String accessToken
);

@PostMapping("/v1/user/unlink")
void unlinkKakaoServer(
@RequestHeader("Authorization") String adminKey,
@RequestParam(name = "target_id_type") String targetIdType,
@RequestParam("target_id") Long targetId
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto;
import org.hankki.hankkiserver.external.openfeign.kakao.dto.KakaoUnlinkRequest;
import org.hankki.hankkiserver.external.openfeign.kakao.dto.KakaoUserInfo;
import org.springframework.stereotype.Component;

Expand All @@ -13,6 +14,8 @@ public class KakaoOAuthProvider {

private final KakaoFeignClient kakaoFeignClient;

private static final String GRANT_TYPE = "KakaoAK ";

public SocialInfoDto getKakaoUserInfo(final String providerToken) {
KakaoAccessToken kakaoAccessToken = createKakaoAccessToken(providerToken);
String accessTokenWithTokenType = kakaoAccessToken.getAccessTokenWithTokenType();
Expand All @@ -22,4 +25,8 @@ public SocialInfoDto getKakaoUserInfo(final String providerToken) {
kakaoUserInfo.kakaoAccount().kakaoUserProfile().nickname(),
kakaoUserInfo.kakaoAccount().email());
}

public void unlinkKakaoServer(final String adminKey, KakaoUnlinkRequest kakaoUnlinkRequest) {
kakaoFeignClient.unlinkKakaoServer(GRANT_TYPE + adminKey, kakaoUnlinkRequest.targetIdType(), kakaoUnlinkRequest.targetId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hankki.hankkiserver.external.openfeign.kakao.dto;

public record KakaoUnlinkRequest(
String targetIdType,
Long targetId
) {
private static final String TARGET_ID_TYPE = "user_id";

public static KakaoUnlinkRequest of(final String targetId) {
return new KakaoUnlinkRequest(TARGET_ID_TYPE, Long.parseLong(targetId));
}
}
Loading