Skip to content

Commit

Permalink
[feat] 애플 로그인 탈퇴 구현 (#105)
Browse files Browse the repository at this point in the history
* [feat] apple 탈퇴 구현 #104

* [feat] 개발용 회원탈퇴 구현 #104

* [chore] spotless 적용 #104
  • Loading branch information
wjdtkdgns authored Aug 15, 2023
1 parent 59e5e14 commit 26be511
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ public class AuthController {
@Operation(summary = "회원탈퇴를 합니다.")
@DeleteMapping("/withdrawal")
public void withDrawUser(
@RequestParam(required = false, name = "appleAccessToken", value = "")
String appleAccessToken) {
withdrawUserUseCase.execute(appleAccessToken);
@RequestParam(required = false, name = "appleCode", value = "") String appleCode,
@RequestHeader(value = "referer", required = false) String referer) {
withdrawUserUseCase.execute(appleCode, referer);
}

@Operation(summary = "회원탈퇴를 합니다. (개발용)", deprecated = true)
@DeleteMapping("/withdrawal/dev")
public void withDrawUserDev(
@RequestParam(required = false, name = "appleCode", value = "") String appleCode) {
withdrawUserUseCase.executeDev(appleCode);
}

@Operation(summary = "로그아웃을 합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public OauthSignInResponse oauthUserIdTokenLogin(
public OauthRegisterResponse oauthUserRegister(
@PathVariable("provider") OauthProvider provider,
@RequestParam("idToken") String idToken,
@RequestParam("oauthAccessToken") String accessToken,
@RequestParam(value = "oauthAccessToken", required = false) String accessToken,
@RequestBody RegisterRequest registerRequest) {
return oauthRegisterUseCase.execute(provider, idToken, accessToken, registerRequest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,38 @@ public class WithdrawUserUseCase {
private final UserDomainService userDomainService;

@Transactional
public void execute(String appleAccessToken) {
public void execute(String appleAccessToken, String referer) {
Long userId = SecurityUtil.getCurrentUserId();
User user = userAdaptor.findById(userId);
// oauth쪽 탈퇴
withdrawOauth(user.getOauthInfo().getProvider(), appleAccessToken, user);
withdrawOauth(user.getOauthInfo().getProvider(), appleAccessToken, user, referer);
// 우리쪽 탈퇴
withdrawService(userId, user);
}

private void withdrawOauth(OauthProvider provider, String appleAccessToken, User user) {
@Transactional
public void executeDev(String appleAccessToken) {
Long userId = SecurityUtil.getCurrentUserId();
User user = userAdaptor.findById(userId);
// oauth쪽 탈퇴
withdrawOauthDev(user.getOauthInfo().getProvider(), appleAccessToken, user);
// 우리쪽 탈퇴
withdrawService(userId, user);
}

private void withdrawOauth(
OauthProvider provider, String appleAccessToken, User user, String referer) {
switch (provider) {
case KAKAO -> oauthHelper.withdraw(provider, user.getOauthInfo().getOid(), null, null);
case APPLE -> oauthHelper.withdraw(provider, null, appleAccessToken, referer);
default -> throw InvalidOauthProviderException.EXCEPTION;
}
}

private void withdrawOauthDev(OauthProvider provider, String appleAccessToken, User user) {
switch (provider) {
case KAKAO -> oauthHelper.withdraw(provider, user.getOauthInfo().getOid(), null);
case APPLE -> oauthHelper.withdraw(provider, null, appleAccessToken);
case KAKAO -> oauthHelper.withdrawDev(provider, user.getOauthInfo().getOid(), null);
case APPLE -> oauthHelper.withdrawDev(provider, null, appleAccessToken);
default -> throw InvalidOauthProviderException.EXCEPTION;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import allchive.server.core.annotation.Helper;
import allchive.server.core.dto.OIDCDecodePayload;
import allchive.server.core.error.exception.NoAppleAccessTokenException;
import allchive.server.core.error.exception.NoAppleCodeException;
import allchive.server.core.properties.AppleOAuthProperties;
import allchive.server.domain.domains.user.domain.enums.OauthInfo;
import allchive.server.domain.domains.user.domain.enums.OauthProvider;
Expand Down Expand Up @@ -94,12 +94,26 @@ public OIDCDecodePayload getOIDCDecodePayloadDev(String token) {
}

/** apple측 회원 탈퇴 * */
public void withdrawAppleOauthUser(String appleOAuthAccessToken) {
if (appleOAuthAccessToken == null) {
throw NoAppleAccessTokenException.EXCEPTION;
public void withdrawAppleOauthUser(String code, String referer) {
if (code == null) {
throw NoAppleCodeException.EXCEPTION;
}
AppleTokenResponse appleTokenResponse = getAppleOAuthToken(code, referer);
appleOAuthClient.revoke(
appleOAuthProperties.getClientId(), appleOAuthAccessToken, this.getClientSecret());
appleOAuthProperties.getClientId(),
appleTokenResponse.getAccessToken(),
this.getClientSecret());
}

public void withdrawAppleOauthUserDev(String code) {
if (code == null) {
throw NoAppleCodeException.EXCEPTION;
}
AppleTokenResponse appleTokenResponse = getAppleOAuthTokenDev(code);
appleOAuthClient.revoke(
appleOAuthProperties.getWebClientId(),
appleTokenResponse.getAccessToken(),
this.getClientSecret());
}

/** client secret 가져오기 * */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,94 +18,80 @@ public class OauthHelper {

/** oauth link 가져오기 * */
public OauthLoginLinkResponse getOauthLinkDev(OauthProvider provider) {
switch (provider) {
case KAKAO:
return new OauthLoginLinkResponse(kakaoOauthHelper.getKaKaoOauthLinkDev());
case APPLE:
return new OauthLoginLinkResponse(appleOauthHelper.getAppleOauthLinkDev());
default:
throw InvalidOauthProviderException.EXCEPTION;
}
return switch (provider) {
case KAKAO -> new OauthLoginLinkResponse(kakaoOauthHelper.getKaKaoOauthLinkDev());
case APPLE -> new OauthLoginLinkResponse(appleOauthHelper.getAppleOauthLinkDev());
default -> throw InvalidOauthProviderException.EXCEPTION;
};
}

public OauthLoginLinkResponse getOauthLink(OauthProvider provider, String referer) {
switch (provider) {
case KAKAO:
return new OauthLoginLinkResponse(kakaoOauthHelper.getKaKaoOauthLink(referer));
case APPLE:
return new OauthLoginLinkResponse(appleOauthHelper.getAppleOAuthLink(referer));
default:
throw InvalidOauthProviderException.EXCEPTION;
}
return switch (provider) {
case KAKAO -> new OauthLoginLinkResponse(kakaoOauthHelper.getKaKaoOauthLink(referer));
case APPLE -> new OauthLoginLinkResponse(appleOauthHelper.getAppleOAuthLink(referer));
default -> throw InvalidOauthProviderException.EXCEPTION;
};
}

/** idtoken 가져오기 * */
public OauthTokenResponse getCredential(OauthProvider provider, String code, String referer) {
switch (provider) {
case KAKAO:
return OauthTokenResponse.from(kakaoOauthHelper.getKakaoOauthToken(code, referer));
case APPLE:
return OauthTokenResponse.from(appleOauthHelper.getAppleOAuthToken(code, referer));
default:
throw InvalidOauthProviderException.EXCEPTION;
}
return switch (provider) {
case KAKAO -> OauthTokenResponse.from(
kakaoOauthHelper.getKakaoOauthToken(code, referer));
case APPLE -> OauthTokenResponse.from(
appleOauthHelper.getAppleOAuthToken(code, referer));
default -> throw InvalidOauthProviderException.EXCEPTION;
};
}

public OauthTokenResponse getCredentialDev(OauthProvider provider, String code) {
switch (provider) {
case KAKAO:
return OauthTokenResponse.from(kakaoOauthHelper.getKakaoOauthTokenDev(code));
case APPLE:
return OauthTokenResponse.from(appleOauthHelper.getAppleOAuthTokenDev(code));
default:
throw InvalidOauthProviderException.EXCEPTION;
}
return switch (provider) {
case KAKAO -> OauthTokenResponse.from(kakaoOauthHelper.getKakaoOauthTokenDev(code));
case APPLE -> OauthTokenResponse.from(appleOauthHelper.getAppleOAuthTokenDev(code));
default -> throw InvalidOauthProviderException.EXCEPTION;
};
}

/** idtoken 분석 * */
public OauthInfo getOauthInfo(OauthProvider provider, String idToken) {
switch (provider) {
case KAKAO:
return kakaoOauthHelper.getKakaoOauthInfoByIdToken(idToken);
case APPLE:
return appleOauthHelper.getAppleOAuthInfoByIdToken(idToken);
default:
throw InvalidOauthProviderException.EXCEPTION;
}
return switch (provider) {
case KAKAO -> kakaoOauthHelper.getKakaoOauthInfoByIdToken(idToken);
case APPLE -> appleOauthHelper.getAppleOAuthInfoByIdToken(idToken);
default -> throw InvalidOauthProviderException.EXCEPTION;
};
}

public OauthInfo getOauthInfoDev(OauthProvider provider, String idToken) {
return switch (provider) {
case KAKAO -> kakaoOauthHelper.getKakaoOauthInfoByIdTokenDev(idToken);
case APPLE -> appleOauthHelper.getAppleOAuthInfoByIdTokenDev(idToken);
default -> throw InvalidOauthProviderException.EXCEPTION;
};
}

/** 회원탈퇴 * */
public void withdraw(
OauthProvider provider, String oid, String appleAccessToken, String referer) {
switch (provider) {
case KAKAO:
return kakaoOauthHelper.getKakaoOauthInfoByIdTokenDev(idToken);
case APPLE:
return appleOauthHelper.getAppleOAuthInfoByIdTokenDev(idToken);
default:
throw InvalidOauthProviderException.EXCEPTION;
case KAKAO -> kakaoOauthHelper.withdrawKakaoOauthUser(oid);
case APPLE -> appleOauthHelper.withdrawAppleOauthUser(appleAccessToken, referer);
default -> throw InvalidOauthProviderException.EXCEPTION;
}
}

/** 회원탈퇴 * */
public void withdraw(OauthProvider provider, String oid, String appleAccessToken) {
public void withdrawDev(OauthProvider provider, String oid, String appleAccessToken) {
switch (provider) {
case KAKAO:
kakaoOauthHelper.withdrawKakaoOauthUser(oid);
break;
case APPLE:
appleOauthHelper.withdrawAppleOauthUser(appleAccessToken);
break;
default:
throw InvalidOauthProviderException.EXCEPTION;
case KAKAO -> kakaoOauthHelper.withdrawKakaoOauthUser(oid);
case APPLE -> appleOauthHelper.withdrawAppleOauthUserDev(appleAccessToken);
default -> throw InvalidOauthProviderException.EXCEPTION;
}
}

/** 유저 정보 가져오기 * */
public OauthUserInfoDto getUserInfo(OauthProvider provider, String oauthAccessToken) {
switch (provider) {
case KAKAO:
return kakaoOauthHelper.getUserInfo(oauthAccessToken);
default:
throw InvalidOauthProviderException.EXCEPTION;
}
return switch (provider) {
case KAKAO -> kakaoOauthHelper.getUserInfo(oauthAccessToken);
default -> throw InvalidOauthProviderException.EXCEPTION;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public enum GlobalErrorCode implements BaseErrorCode {

/** Server 오류 * */
HTTP_MESSAGE_NOT_READABLE(BAD_REQUEST, "GLOBAL_400_1", "잘못된 형식의 값을 입력했습니다."),
NO_APPLE_ACCESS_TOKEN(BAD_REQUEST, "GLOBAL_400_2", "애플 엑세스 토큰이 필요합니다."),
NO_APPLE_CODE(BAD_REQUEST, "GLOBAL_400_2", "애플 코드가 필요합니다."),
EMPTY_PARAM_VALUE(BAD_REQUEST, "GLOBAL_400_3", "빈 파람 값을 입력했습니다."),
_INTERNAL_SERVER_ERROR(INTERNAL_SERVER, "GLOBAL_500_1", "서버 오류. 관리자에게 문의 부탁드립니다."),
INVALID_OAUTH_PROVIDER(INTERNAL_SERVER, "GLOBAL_500_2", "지원하지 않는 OAuth Provider 입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import allchive.server.core.error.BaseErrorException;
import allchive.server.core.error.GlobalErrorCode;

public class NoAppleAccessTokenException extends BaseErrorException {
public class NoAppleCodeException extends BaseErrorException {

public static final BaseErrorException EXCEPTION = new NoAppleAccessTokenException();
public static final BaseErrorException EXCEPTION = new NoAppleCodeException();

private NoAppleAccessTokenException() {
super(GlobalErrorCode.NO_APPLE_ACCESS_TOKEN);
private NoAppleCodeException() {
super(GlobalErrorCode.NO_APPLE_CODE);
}
}

0 comments on commit 26be511

Please sign in to comment.