From 1b07b5dffcbfb58cc2280fc4a0116d58f4c12345 Mon Sep 17 00:00:00 2001 From: BYEONGRYEOL Date: Mon, 29 Jul 2024 20:17:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Fix:=20oauth=20kakao=20token=20login=20/=20?= =?UTF-8?q?web=20login=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../genti/auth/controller/AuthController.java | 7 +++--- ...estDto.java => SocialAppLoginRequest.java} | 2 +- .../gt/genti/auth/service/AuthService.java | 6 +++-- .../service/social/AppleOauthStrategy.java | 12 ++++++++-- .../service/social/GoogleOauthStrategy.java | 8 ++++++- .../service/social/KakaoOauthStrategy.java | 22 +++++++++++++++---- .../service/social/SocialLoginStrategy.java | 4 +++- .../service/social/SocialOauthContext.java | 11 +++++++--- 8 files changed, 54 insertions(+), 18 deletions(-) rename genti-api/src/main/java/com/gt/genti/auth/dto/request/{OauthSignRequestDto.java => SocialAppLoginRequest.java} (94%) diff --git a/genti-api/src/main/java/com/gt/genti/auth/controller/AuthController.java b/genti-api/src/main/java/com/gt/genti/auth/controller/AuthController.java index f0cbfed6..a7bf3d41 100644 --- a/genti-api/src/main/java/com/gt/genti/auth/controller/AuthController.java +++ b/genti-api/src/main/java/com/gt/genti/auth/controller/AuthController.java @@ -16,7 +16,7 @@ import com.gt.genti.auth.dto.request.AppleLoginRequest; import com.gt.genti.auth.dto.request.AppleLoginRequestDto; -import com.gt.genti.auth.dto.request.OauthSignRequestDto; +import com.gt.genti.auth.dto.request.SocialAppLoginRequest; import com.gt.genti.auth.dto.request.SocialLoginRequestImpl; import com.gt.genti.auth.dto.request.TokenRefreshRequestDto; import com.gt.genti.auth.dto.response.OauthJwtResponse; @@ -153,9 +153,8 @@ public ResponseEntity> logout(@AuthUser Long userId) { @PostMapping("/login/oauth2/token") @Logging(item = LogItem.OAUTH_APP, action = LogAction.LOGIN, requester = LogRequester.ANONYMOUS) public ResponseEntity> loginOrSignUpWithOAuthToken( - @RequestBody @Valid OauthSignRequestDto oauthSignRequestDto) { - return success(authService.appLogin(SocialLoginRequestImpl.of(oauthSignRequestDto.getOauthPlatform(), - oauthSignRequestDto.getToken()))); + @RequestBody @Valid SocialAppLoginRequest socialAppLoginRequest) { + return success(authService.appLogin(socialAppLoginRequest)); } @PostMapping("/reissue") diff --git a/genti-api/src/main/java/com/gt/genti/auth/dto/request/OauthSignRequestDto.java b/genti-api/src/main/java/com/gt/genti/auth/dto/request/SocialAppLoginRequest.java similarity index 94% rename from genti-api/src/main/java/com/gt/genti/auth/dto/request/OauthSignRequestDto.java rename to genti-api/src/main/java/com/gt/genti/auth/dto/request/SocialAppLoginRequest.java index 81b53bd8..f6d5859e 100644 --- a/genti-api/src/main/java/com/gt/genti/auth/dto/request/OauthSignRequestDto.java +++ b/genti-api/src/main/java/com/gt/genti/auth/dto/request/SocialAppLoginRequest.java @@ -12,7 +12,7 @@ @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Schema(name = "[Auth][Anonymous] oauth 토큰으로 로그인or회원가입 처리 요청 dto", description = "Oauth 토큰 및 플랫폼") -public class OauthSignRequestDto { +public class SocialAppLoginRequest { @NotBlank @Schema(example = "rhtodaksgdkdyekemf") String token; diff --git a/genti-api/src/main/java/com/gt/genti/auth/service/AuthService.java b/genti-api/src/main/java/com/gt/genti/auth/service/AuthService.java index 9868fb58..edc8c74b 100644 --- a/genti-api/src/main/java/com/gt/genti/auth/service/AuthService.java +++ b/genti-api/src/main/java/com/gt/genti/auth/service/AuthService.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.gt.genti.auth.dto.request.SocialAppLoginRequest; import com.gt.genti.auth.dto.request.SocialLoginRequest; import com.gt.genti.auth.dto.request.TokenRefreshRequestDto; import com.gt.genti.auth.dto.response.OauthJwtResponse; @@ -20,6 +21,7 @@ import com.gt.genti.user.service.social.SocialOauthContext; import com.gt.genti.util.HttpRequestUtil; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -36,8 +38,8 @@ public SocialLoginResponse webLogin(final SocialLoginRequest request) { return socialOauthContext.doLogin(request); } - public OauthJwtResponse appLogin(final SocialLoginRequest request) { - return socialOauthContext.doLogin(request).getToken(); + public OauthJwtResponse appLogin(final @Valid SocialAppLoginRequest request) { + return socialOauthContext.doAppLogin(request).getToken(); } public HttpHeaders getOauthRedirect(OauthPlatform oauthPlatform) { diff --git a/genti-api/src/main/java/com/gt/genti/user/service/social/AppleOauthStrategy.java b/genti-api/src/main/java/com/gt/genti/user/service/social/AppleOauthStrategy.java index b61f9d6f..c60b161f 100644 --- a/genti-api/src/main/java/com/gt/genti/user/service/social/AppleOauthStrategy.java +++ b/genti-api/src/main/java/com/gt/genti/user/service/social/AppleOauthStrategy.java @@ -9,7 +9,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.gt.genti.auth.dto.request.SocialAppLoginRequest; import com.gt.genti.auth.dto.request.SocialLoginRequest; +import com.gt.genti.auth.dto.request.SocialLoginRequestImpl; import com.gt.genti.auth.dto.response.OauthJwtResponse; import com.gt.genti.auth.dto.response.SocialLoginResponse; import com.gt.genti.error.ExpectedException; @@ -45,7 +47,7 @@ public class AppleOauthStrategy implements SocialLoginStrategy { @Override @Transactional - public SocialLoginResponse login(SocialLoginRequest request) { + public SocialLoginResponse webLogin(SocialLoginRequest request) { AppleUserResponse userResponse = getApplePlatformMember(request.getCode()); Optional findUser = userRepository.findUserBySocialId(userResponse.getPlatformId()); User user; @@ -70,11 +72,17 @@ public SocialLoginResponse login(SocialLoginRequest request) { .userId(user.getId().toString()) .role(user.getUserRole().getRoles()) .build(); - OauthJwtResponse oauthJwtResponse = new OauthJwtResponse(jwtTokenProvider.generateAccessToken(tokenGenerateCommand), + OauthJwtResponse oauthJwtResponse = new OauthJwtResponse( + jwtTokenProvider.generateAccessToken(tokenGenerateCommand), jwtTokenProvider.generateRefreshToken(tokenGenerateCommand), user.getUserRole()); return SocialLoginResponse.of(user.getId(), user.getUsername(), user.getEmail(), isNewUser, oauthJwtResponse); } + @Override + public SocialLoginResponse tokenLogin(SocialAppLoginRequest request) { + return webLogin(SocialLoginRequestImpl.of(request.getOauthPlatform(), request.getToken())); + } + @Override public boolean support(String provider) { return provider.equals(OauthPlatform.APPLE.getStringValue()); diff --git a/genti-api/src/main/java/com/gt/genti/user/service/social/GoogleOauthStrategy.java b/genti-api/src/main/java/com/gt/genti/user/service/social/GoogleOauthStrategy.java index a80ed513..af980dda 100644 --- a/genti-api/src/main/java/com/gt/genti/user/service/social/GoogleOauthStrategy.java +++ b/genti-api/src/main/java/com/gt/genti/user/service/social/GoogleOauthStrategy.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.gt.genti.auth.dto.request.SocialAppLoginRequest; import com.gt.genti.auth.dto.request.SocialLoginRequest; import com.gt.genti.auth.dto.response.OauthJwtResponse; import com.gt.genti.auth.dto.response.SocialLoginResponse; @@ -69,7 +70,7 @@ public String getAuthUri() { @Override @Transactional - public SocialLoginResponse login(SocialLoginRequest request) { + public SocialLoginResponse webLogin(SocialLoginRequest request) { GoogleTokenResponse tokenResponse = googleAuthApiClient.googleAuth( request.getCode(), googleClientId, @@ -105,6 +106,11 @@ public SocialLoginResponse login(SocialLoginRequest request) { return SocialLoginResponse.of(user.getId(), user.getUsername(), user.getEmail(), isNewUser, oauthJwtResponse); } + @Override + public SocialLoginResponse tokenLogin(SocialAppLoginRequest request) { + return null; + } + @Override public boolean support(String provider) { return false; diff --git a/genti-api/src/main/java/com/gt/genti/user/service/social/KakaoOauthStrategy.java b/genti-api/src/main/java/com/gt/genti/user/service/social/KakaoOauthStrategy.java index a73d7e7d..0a14936d 100644 --- a/genti-api/src/main/java/com/gt/genti/user/service/social/KakaoOauthStrategy.java +++ b/genti-api/src/main/java/com/gt/genti/user/service/social/KakaoOauthStrategy.java @@ -11,6 +11,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.gt.genti.auth.dto.request.SocialAppLoginRequest; import com.gt.genti.auth.dto.request.SocialLoginRequest; import com.gt.genti.auth.dto.response.OauthJwtResponse; import com.gt.genti.auth.dto.response.SocialLoginResponse; @@ -65,7 +66,7 @@ public String getAuthUri() { @Override @Transactional - public SocialLoginResponse login(SocialLoginRequest request) { + public SocialLoginResponse webLogin(SocialLoginRequest request) { KakaoTokenResponse tokenResponse = kakaoAuthApiClient.getOAuth2AccessToken( "authorization_code", kakaoClientId, @@ -73,8 +74,20 @@ public SocialLoginResponse login(SocialLoginRequest request) { serverBaseUri + ":" + serverPort + kakaoRedirectUri, request.getCode() ); + + OauthPlatform oauthPlatform = request.getOauthPlatform(); + String accessToken = tokenResponse.accessToken(); + return getUserInfo(oauthPlatform, accessToken); + } + + @Override + public SocialLoginResponse tokenLogin(final SocialAppLoginRequest request) { + return getUserInfo(request.getOauthPlatform(), request.getToken()); + } + + private SocialLoginResponse getUserInfo(OauthPlatform oauthPlatform, String accessToken) { KakaoUserResponse userResponse = kakaoApiClient.getUserInformation( - "Bearer " + tokenResponse.accessToken()); + "Bearer " + accessToken); Optional findUser = userRepository.findUserBySocialId(userResponse.id()); User user; boolean isNewUser = false; @@ -82,7 +95,7 @@ public SocialLoginResponse login(SocialLoginRequest request) { User newUser = userRepository.save(User.builderWithSignIn() .socialId(userResponse.id()) .birthDate(getBirthDateStringFrom(userResponse)) - .oauthPlatform(request.getOauthPlatform()) + .oauthPlatform(oauthPlatform) .username(userResponse.kakaoAccount().name()) .nickname(RandomUtil.generateRandomNickname()) .email(userResponse.kakaoAccount().email()) @@ -99,7 +112,8 @@ public SocialLoginResponse login(SocialLoginRequest request) { .userId(user.getId().toString()) .role(user.getUserRole().getRoles()) .build(); - OauthJwtResponse oauthJwtResponse = new OauthJwtResponse(jwtTokenProvider.generateAccessToken(tokenGenerateCommand), + OauthJwtResponse oauthJwtResponse = new OauthJwtResponse( + jwtTokenProvider.generateAccessToken(tokenGenerateCommand), jwtTokenProvider.generateRefreshToken(tokenGenerateCommand), user.getUserRole()); return SocialLoginResponse.of(user.getId(), user.getUsername(), user.getEmail(), isNewUser, oauthJwtResponse); } diff --git a/genti-api/src/main/java/com/gt/genti/user/service/social/SocialLoginStrategy.java b/genti-api/src/main/java/com/gt/genti/user/service/social/SocialLoginStrategy.java index 7e920b7e..b4e70163 100644 --- a/genti-api/src/main/java/com/gt/genti/user/service/social/SocialLoginStrategy.java +++ b/genti-api/src/main/java/com/gt/genti/user/service/social/SocialLoginStrategy.java @@ -1,11 +1,13 @@ package com.gt.genti.user.service.social; +import com.gt.genti.auth.dto.request.SocialAppLoginRequest; import com.gt.genti.auth.dto.request.SocialLoginRequest; import com.gt.genti.auth.dto.response.SocialLoginResponse; public interface SocialLoginStrategy { - SocialLoginResponse login(final SocialLoginRequest request); + SocialLoginResponse webLogin(final SocialLoginRequest request); + SocialLoginResponse tokenLogin(final SocialAppLoginRequest request); boolean support(String provider); } diff --git a/genti-api/src/main/java/com/gt/genti/user/service/social/SocialOauthContext.java b/genti-api/src/main/java/com/gt/genti/user/service/social/SocialOauthContext.java index 9fc6952a..e1da581e 100644 --- a/genti-api/src/main/java/com/gt/genti/user/service/social/SocialOauthContext.java +++ b/genti-api/src/main/java/com/gt/genti/user/service/social/SocialOauthContext.java @@ -5,13 +5,15 @@ import org.springframework.stereotype.Component; -import com.gt.genti.error.ExpectedException; -import com.gt.genti.error.ResponseCode; +import com.gt.genti.auth.dto.request.SocialAppLoginRequest; import com.gt.genti.auth.dto.request.SocialLoginRequest; import com.gt.genti.auth.dto.response.SocialLoginResponse; +import com.gt.genti.error.ExpectedException; +import com.gt.genti.error.ResponseCode; import com.gt.genti.user.model.OauthPlatform; import jakarta.annotation.PostConstruct; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; @Component @@ -48,11 +50,14 @@ private SocialAuthStrategy authStrategyOf(OauthPlatform oauthPlatform) { } public SocialLoginResponse doLogin(final SocialLoginRequest request) { - return loginStrategyOf(request.getOauthPlatform()).login(request); + return loginStrategyOf(request.getOauthPlatform()).webLogin(request); } public String getAuthUri(OauthPlatform oauthPlatform) { return authStrategyOf(oauthPlatform).getAuthUri(); } + public SocialLoginResponse doAppLogin(@Valid SocialAppLoginRequest request) { + return loginStrategyOf(request.getOauthPlatform()).tokenLogin(request); + } } From a32f5e7e67d9b7b5d7c19510d390c8c32fbe24fb Mon Sep 17 00:00:00 2001 From: BYEONGRYEOL Date: Mon, 29 Jul 2024 21:11:40 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Fix:=20Service=20Layer=EC=97=90=EC=84=9C=20?= =?UTF-8?q?@Valid=20=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/gt/genti/auth/service/AuthService.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/genti-api/src/main/java/com/gt/genti/auth/service/AuthService.java b/genti-api/src/main/java/com/gt/genti/auth/service/AuthService.java index edc8c74b..8921008b 100644 --- a/genti-api/src/main/java/com/gt/genti/auth/service/AuthService.java +++ b/genti-api/src/main/java/com/gt/genti/auth/service/AuthService.java @@ -21,7 +21,6 @@ import com.gt.genti.user.service.social.SocialOauthContext; import com.gt.genti.util.HttpRequestUtil; -import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -38,7 +37,7 @@ public SocialLoginResponse webLogin(final SocialLoginRequest request) { return socialOauthContext.doLogin(request); } - public OauthJwtResponse appLogin(final @Valid SocialAppLoginRequest request) { + public OauthJwtResponse appLogin(final SocialAppLoginRequest request) { return socialOauthContext.doAppLogin(request).getToken(); }