Skip to content

Commit

Permalink
[refac] auth 부분 리팩토링 및 response 변경 (#44)
Browse files Browse the repository at this point in the history
* [refac] oauthHelper 분리 및 회원가입 response 변경 #43

* [feat] 회원 탈퇴 로직 미완 #43

* [refac] url 처리 로직 추가 #43

* [refac] module 의존성 정리 #40

* [refac] 회원 탈퇴 로직 추가 #43
  • Loading branch information
wjdtkdgns authored Jul 19, 2023
1 parent 4c149cb commit 49766a8
Show file tree
Hide file tree
Showing 50 changed files with 338 additions and 137 deletions.
3 changes: 0 additions & 3 deletions Api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-security'

// swagger
// implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4'
// implementation 'org.springdoc:springdoc-openapi-starter-webmvc-api:2.0.4'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.12'

implementation project(':Domain')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void execute(Long archivingId, Boolean cancel) {
archivingValidator.validateExistById(archivingId);
Long userId = SecurityUtil.getCurrentUserId();
archivingValidator.validateDeleteStatus(archivingId, userId);
User user = userAdaptor.queryUserById(userId);
User user = userAdaptor.findUserById(userId);
if (cancel) {
scrapDomainService.deleteScrapByUserAndArchivingId(user, archivingId);
archivingDomainService.updateScrapCount(archivingId, -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ public class AuthController {

@Operation(summary = "회원탈퇴를 합니다.")
@DeleteMapping("/withdrawal/{provider}")
public void withDrawUser(@PathVariable OauthProvider provider) {
withdrawUserUseCase.execute(provider);
public void withDrawUser(
@PathVariable OauthProvider provider,
@RequestParam(required = false, name = "appleAccessToken", value = "")
String appleAccessToken) {
withdrawUserUseCase.execute(provider, appleAccessToken);
}

@Operation(summary = "로그아웃을 합니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package allchive.server.api.auth.model.dto.request;


import allchive.server.core.annotation.ValidEnum;
import allchive.server.domain.domains.archiving.domain.enums.Category;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import javax.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,12 +14,16 @@
@NoArgsConstructor
public class RegisterRequest {
@Schema(
defaultValue = "staging/archiving/1/d241218a-a64c-4443-8aa4-ce98017a3d12",
defaultValue =
"https://asset.allchive.co.kr/staging/archiving/1/d241218a-a64c-4443-8aa4-ce98017a3d12",
description = "프로필 이미지 url")
@NotBlank(message = "프로필 이미지 key를 입력하세요")
private String profileImgKey;
@NotBlank(message = "프로필 이미지 url을 입력하세요")
private String profileImgUrl;

@Schema(defaultValue = "닉네임", description = "닉네임")
@NotBlank(message = "닉네임을 입력하세요")
private String nickname;

@ArraySchema(schema = @Schema(description = "관심 주제", defaultValue = "FOOD"))
private List<@ValidEnum(target = Category.class) Category> categories;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,21 @@


import allchive.server.api.auth.model.dto.response.OauthLoginLinkResponse;
import allchive.server.api.auth.service.helper.AppleOAuthHelper;
import allchive.server.api.auth.service.helper.KakaoOauthHelper;
import allchive.server.api.auth.service.helper.OauthHelper;
import allchive.server.core.annotation.UseCase;
import allchive.server.core.error.exception.InvalidOauthProviderException;
import allchive.server.domain.domains.user.domain.enums.OauthProvider;
import lombok.RequiredArgsConstructor;

@UseCase
@RequiredArgsConstructor
public class OauthLinkUseCase {
private final KakaoOauthHelper kakaoOauthHelper;
private final AppleOAuthHelper appleOAuthHelper;
private final OauthHelper oauthHelper;

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 oauthHelper.getOauthLinkDev(provider);
}

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 oauthHelper.getOauthLink(provider, referer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

import allchive.server.api.auth.model.dto.response.OauthSignInResponse;
import allchive.server.api.auth.model.dto.response.OauthTokenResponse;
import allchive.server.api.auth.service.helper.AppleOAuthHelper;
import allchive.server.api.auth.service.helper.KakaoOauthHelper;
import allchive.server.api.auth.service.helper.OauthHelper;
import allchive.server.api.auth.service.helper.TokenGenerateHelper;
import allchive.server.core.annotation.UseCase;
import allchive.server.core.error.exception.InvalidOauthProviderException;
import allchive.server.domain.domains.user.domain.User;
import allchive.server.domain.domains.user.domain.enums.OauthInfo;
import allchive.server.domain.domains.user.domain.enums.OauthProvider;
Expand All @@ -17,13 +15,13 @@
@UseCase
@RequiredArgsConstructor
public class OauthLoginUseCase {
private final KakaoOauthHelper kakaoOauthHelper;
private final AppleOAuthHelper appleOAuthHelper;
private final OauthHelper oauthHelper;
private final UserDomainService userDomainService;
private final TokenGenerateHelper tokenGenerateHelper;

public OauthSignInResponse loginWithCode(OauthProvider provider, String code, String referer) {
final OauthTokenResponse oauthTokenResponse = getCredential(provider, code, referer);
final OauthTokenResponse oauthTokenResponse =
oauthHelper.getCredential(provider, code, referer);
return processLoginWithIdToken(provider, oauthTokenResponse.getIdToken());
}

Expand All @@ -32,52 +30,17 @@ public OauthSignInResponse loginWithIdToken(OauthProvider provider, String idTok
}

public OauthSignInResponse devLogin(OauthProvider provider, String code) {
final OauthTokenResponse oauthTokenResponse = getCredentialDev(provider, code);
final OauthTokenResponse oauthTokenResponse = oauthHelper.getCredentialDev(provider, code);
return processLoginWithIdToken(provider, oauthTokenResponse.getIdToken());
}

private OauthSignInResponse processLoginWithIdToken(OauthProvider provider, String idToken) {
final OauthInfo oauthInfo = getOauthInfo(provider, idToken);
final OauthInfo oauthInfo = oauthHelper.getOauthInfo(provider, idToken);
if (userDomainService.checkUserCanLogin(oauthInfo)) {
User user = userDomainService.loginUser(oauthInfo);
return tokenGenerateHelper.execute(user);
} else {
return OauthSignInResponse.cannotLogin(idToken);
}
}

/** idtoken 가져오기 * */
private 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;
}
}

private 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;
}
}

/** idtoken 분석 * */
private OauthInfo getOauthInfo(OauthProvider provider, String idToken) {
switch (provider) {
case KAKAO:
return kakaoOauthHelper.getKakaoOauthInfoByIdToken(idToken);
case APPLE:
return appleOAuthHelper.getAppleOAuthInfoByIdToken(idToken);
default:
throw InvalidOauthProviderException.EXCEPTION;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

import allchive.server.api.auth.model.dto.request.RegisterRequest;
import allchive.server.api.auth.model.dto.response.OauthRegisterResponse;
import allchive.server.api.auth.service.helper.AppleOAuthHelper;
import allchive.server.api.auth.service.helper.KakaoOauthHelper;
import allchive.server.api.auth.service.helper.OauthHelper;
import allchive.server.api.auth.service.helper.TokenGenerateHelper;
import allchive.server.api.common.util.UrlUtil;
import allchive.server.core.annotation.UseCase;
import allchive.server.core.error.exception.InvalidOauthProviderException;
import allchive.server.domain.domains.user.domain.User;
import allchive.server.domain.domains.user.domain.enums.OauthInfo;
import allchive.server.domain.domains.user.domain.enums.OauthProvider;
Expand All @@ -17,31 +16,19 @@
@UseCase
@RequiredArgsConstructor
public class OauthRegisterUseCase {
private final KakaoOauthHelper kakaoOauthHelper;
private final AppleOAuthHelper appleOAuthHelper;
private final OauthHelper oauthHelper;
private final UserDomainService userDomainService;
private final TokenGenerateHelper tokenGenerateHelper;

public OauthRegisterResponse execute(
OauthProvider provider, String idToken, RegisterRequest registerRequest) {
final OauthInfo oauthInfo = getOauthInfo(provider, idToken);
final OauthInfo oauthInfo = oauthHelper.getOauthInfo(provider, idToken);
final User user =
userDomainService.registerUser(
registerRequest.getNickname(),
registerRequest.getProfileImgKey(),
UrlUtil.convertUrlToKey(registerRequest.getProfileImgUrl()),
registerRequest.getCategories(),
oauthInfo);
return OauthRegisterResponse.from(tokenGenerateHelper.execute(user));
}

/** idtoken 분석 * */
private OauthInfo getOauthInfo(OauthProvider provider, String idToken) {
switch (provider) {
case KAKAO:
return kakaoOauthHelper.getKakaoOauthInfoByIdToken(idToken);
case APPLE:
return appleOAuthHelper.getAppleOAuthInfoByIdToken(idToken);
default:
throw InvalidOauthProviderException.EXCEPTION;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TokenRefreshUseCase {
public OauthRegisterResponse execute(String refreshToken) {
RefreshTokenEntity oldToken = refreshTokenAdaptor.findTokenByRefreshToken(refreshToken);
Long userId = jwtTokenProvider.parseRefreshToken(oldToken.getRefreshToken());
User user = userAdaptor.queryUserById(userId);
User user = userAdaptor.findUserById(userId);
return OauthRegisterResponse.from(tokenGenerateHelper.execute(user));
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,81 @@
package allchive.server.api.auth.service;


import allchive.server.api.auth.service.helper.AppleOAuthHelper;
import allchive.server.api.auth.service.helper.KakaoOauthHelper;
import allchive.server.api.auth.service.helper.OauthHelper;
import allchive.server.api.config.security.SecurityUtil;
import allchive.server.core.annotation.UseCase;
import allchive.server.core.error.exception.InvalidOauthProviderException;
import allchive.server.domain.domains.archiving.adaptor.ArchivingAdaptor;
import allchive.server.domain.domains.archiving.domain.Archiving;
import allchive.server.domain.domains.block.service.BlockDomainService;
import allchive.server.domain.domains.content.adaptor.ContentAdaptor;
import allchive.server.domain.domains.content.adaptor.TagAdaptor;
import allchive.server.domain.domains.content.domain.Tag;
import allchive.server.domain.domains.content.service.ContentDomainService;
import allchive.server.domain.domains.content.service.ContentTagGroupDomainService;
import allchive.server.domain.domains.content.service.TagDomainService;
import allchive.server.domain.domains.recycle.service.RecycleDomainService;
import allchive.server.domain.domains.report.service.ReportDomainService;
import allchive.server.domain.domains.search.service.LatestSearchDomainService;
import allchive.server.domain.domains.user.adaptor.RefreshTokenAdaptor;
import allchive.server.domain.domains.user.adaptor.UserAdaptor;
import allchive.server.domain.domains.user.domain.User;
import allchive.server.domain.domains.user.domain.enums.OauthProvider;
import allchive.server.domain.domains.user.service.ScrapDomainService;
import allchive.server.domain.domains.user.service.UserDomainService;
import lombok.RequiredArgsConstructor;

import java.util.List;

@UseCase
@RequiredArgsConstructor
public class WithdrawUserUseCase {
private final KakaoOauthHelper kakaoOauthHelper;
private final AppleOAuthHelper appleOAuthHelper;
private final UserAdaptor userAdaptor;
private final OauthHelper oauthHelper;
private final RefreshTokenAdaptor refreshTokenAdaptor;
private final LatestSearchDomainService latestSearchDomainService;
private final ScrapDomainService scrapDomainService;
private final BlockDomainService blockDomainService;
private final ArchivingAdaptor archivingAdaptor;
private final TagAdaptor tagAdaptor;
private final ContentTagGroupDomainService contentTagGroupDomainService;
private final ContentDomainService contentDomainService;
private final TagDomainService tagDomainService;
private final RecycleDomainService recycleDomainService;
private final ReportDomainService reportDomainService;
private final UserDomainService userDomainService;

public void execute(OauthProvider provider) {
public void execute(OauthProvider provider, String appleAccessToken) {
Long userId = SecurityUtil.getCurrentUserId();
User user = userAdaptor.queryUserById(userId);
// 우리쪽 탈퇴
refreshTokenAdaptor.deleteTokenByUserId(userId);
User user = userAdaptor.findUserById(userId);
// oauth쪽 탈퇴
withdrawOauth(provider, appleAccessToken, user);
// 우리쪽 탈퇴
withdrawService(userId, user);
}

private void withdrawOauth(OauthProvider provider, String appleAccessToken, User user) {
switch (provider) {
case KAKAO:
kakaoOauthHelper.withdrawKakaoOauthUser(user.getOauthInfo().getOid());
break;
// case APPLE:
// appleOAuthHelper.withdrawAppleOauthUser();
default:
throw InvalidOauthProviderException.EXCEPTION;
case KAKAO -> oauthHelper.withdraw(provider, user.getOauthInfo().getOid(), null);
case APPLE -> oauthHelper.withdraw(provider, null, appleAccessToken);
default -> throw InvalidOauthProviderException.EXCEPTION;
}

}

private void withdrawService(Long userId, User user) {
refreshTokenAdaptor.deleteTokenByUserId(userId);
latestSearchDomainService.deleteAllByUserId(userId);
scrapDomainService.deleteAllByUser(user);
blockDomainService.queryDeleteBlockByBlockFromOrBlockUser(userId);
List<Archiving> archivingList = archivingAdaptor.findAllByUserId(userId);
List<Long> archivingId = archivingList.stream().map(Archiving::getId).toList();
List<Tag> tagList = tagAdaptor.findAllByUserId(userId);
contentTagGroupDomainService.deleteAllByTagIn(tagList);
tagDomainService.deleteAll(tagList);
contentDomainService.deleteAllByArchivingIdIn(archivingId);
recycleDomainService.deleteAllByUserId(userId);
reportDomainService.deleteAllByReportedUserId(userId);
userDomainService.deleteUserById(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@Helper
@RequiredArgsConstructor
public class AppleOAuthHelper {
public class AppleOauthHelper {
private final AppleOAuthProperties appleOAuthProperties;
private final AppleOAuthClient appleOAuthClient;
private final AppleOIDCClient appleOIDCClient;
Expand Down Expand Up @@ -76,11 +76,10 @@ public OIDCDecodePayload getOIDCDecodePayload(String token) {
}

/** apple측 회원 탈퇴 * */
// public void withdrawAppleOauthUser(String appleOAuthAccessToken) {
// appleOAuthClient.revoke(
// appleOAuthProperties.getClientId(), appleOAuthAccessToken,
// this.getClientSecret());
// }
public void withdrawAppleOauthUser(String appleOAuthAccessToken) {
appleOAuthClient.revoke(
appleOAuthProperties.getClientId(), appleOAuthAccessToken, this.getClientSecret());
}

/** client secret 가져오기 * */
private String getClientSecret() {
Expand Down
Loading

0 comments on commit 49766a8

Please sign in to comment.