diff --git a/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/SignInUseCase.java b/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/SignInUseCase.java index 379e67fb..6d626c33 100644 --- a/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/SignInUseCase.java +++ b/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/SignInUseCase.java @@ -5,4 +5,6 @@ public interface SignInUseCase { AuthUserResponse getUser(CodeRequest request); + + AuthUserResponse getDevUser(CodeRequest codeRequest); } diff --git a/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/kakao/KakaoAccessTokenUseCase.java b/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/kakao/KakaoAccessTokenUseCase.java index f0dcf1a9..b8621a77 100644 --- a/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/kakao/KakaoAccessTokenUseCase.java +++ b/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/kakao/KakaoAccessTokenUseCase.java @@ -23,7 +23,9 @@ public class KakaoAccessTokenUseCase { @Value("${app-id.kakao}") private String APP_ID; @Value("${kakao.redirect-uri}") - private String REDRECT_URI; + private String REDIRECT_URI; + @Value("${kakao.dev-redirect-uri}") + private String DEV_REDIRECT_URI; @Value("${kakao.authorization-grant-type}") private String AUTHORIZATION_GRANT_TYPE; private final WebClient webClient; @@ -47,11 +49,27 @@ public KakaoUserInfoResponse getAccessToken (CodeRequest codeRequest) { } } + public KakaoUserInfoResponse getDevAccessToken (CodeRequest codeRequest) { + MultiValueMap requestBody = getDevRequestBody(codeRequest.code()); + try { + KakaoTokenInfoResponse tokenInfoResponse = webClient.post() + .uri(KAKAO_TOKEN_URI) + .headers(h -> h.setContentType(MediaType.APPLICATION_FORM_URLENCODED)) + .bodyValue(requestBody) + .retrieve() + .bodyToMono(KakaoTokenInfoResponse.class) + .block(); + return getUserInfo(tokenInfoResponse.access_token()); + } catch (WebClientResponseException ex) { + throw new KakaoCodeException(); + } + } + private MultiValueMap getRequestBody(String code) { MultiValueMap requestBody = new LinkedMultiValueMap<>(); requestBody.add("grant_type", AUTHORIZATION_GRANT_TYPE); requestBody.add("client_id", APP_ID); - requestBody.add("redirect_uri", REDRECT_URI); + requestBody.add("redirect_uri", REDIRECT_URI); requestBody.add("code", code); return requestBody; } @@ -68,4 +86,13 @@ private KakaoUserInfoResponse getUserInfo(String accessToken) { throw new KakaoException(); } } + + private MultiValueMap getDevRequestBody(String code) { + MultiValueMap requestBody = new LinkedMultiValueMap<>(); + requestBody.add("grant_type", AUTHORIZATION_GRANT_TYPE); + requestBody.add("client_id", APP_ID); + requestBody.add("redirect_uri", DEV_REDIRECT_URI); + requestBody.add("code", code); + return requestBody; + } } \ No newline at end of file diff --git a/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/kakao/KakaoSignInUseCase.java b/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/kakao/KakaoSignInUseCase.java index 1fb2318d..52825d9b 100644 --- a/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/kakao/KakaoSignInUseCase.java +++ b/src/main/java/com/postgraduate/domain/auth/application/usecase/oauth/kakao/KakaoSignInUseCase.java @@ -30,4 +30,16 @@ public AuthUserResponse getUser(CodeRequest codeRequest) { return AuthMapper.mapToAuthUser(socialId); } } + + @Override + public AuthUserResponse getDevUser(CodeRequest codeRequest) { + KakaoUserInfoResponse userInfo = kakaoTokenUseCase.getDevAccessToken(codeRequest); + Long socialId = userInfo.id(); + try { + User user = userGetService.bySocialId(socialId); + return AuthMapper.mapToAuthUser(user, socialId); + } catch (UserNotFoundException e) { + return AuthMapper.mapToAuthUser(socialId); + } + } } \ No newline at end of file diff --git a/src/main/java/com/postgraduate/domain/auth/presentation/AuthController.java b/src/main/java/com/postgraduate/domain/auth/presentation/AuthController.java index 27842d17..3b7ee07c 100644 --- a/src/main/java/com/postgraduate/domain/auth/presentation/AuthController.java +++ b/src/main/java/com/postgraduate/domain/auth/presentation/AuthController.java @@ -44,6 +44,17 @@ public ResponseDto authLogin(@RequestBody @Valid CodeRequest request, @PathVa return ResponseDto.create(AUTH_ALREADY.getCode(), SUCCESS_AUTH.getMessage(), jwtToken); } + @PostMapping("/dev/login/{provider}") + @Operation(summary = "개발용 소셜 로그인", description = "회원인 경우 JWT를, 회원이 아닌 경우 socialId를 반환합니다(회원가입은 진행하지 않습니다).") + public ResponseDto authDevLogin(@RequestBody @Valid CodeRequest request, @PathVariable Provider provider) { + SignInUseCase signInUseCase = selectOauth.selectSignIn(provider); + AuthUserResponse authUser = signInUseCase.getDevUser(request); + if (authUser.user() == null) + return ResponseDto.create(AUTH_NONE.getCode(), NOT_REGISTERED_USER.getMessage(), authUser); + JwtTokenResponse jwtToken = jwtUseCase.signIn(authUser.user()); + return ResponseDto.create(AUTH_ALREADY.getCode(), SUCCESS_AUTH.getMessage(), jwtToken); + } + @PostMapping("/logout") @Operation(summary = "로그아웃", description = "토큰 같이 보내주세요") public ResponseDto logout(@AuthenticationPrincipal User user) {