Skip to content

Commit

Permalink
fix: redirect url 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yonghwankim-dev committed Oct 1, 2023
1 parent a84bdce commit 6351f23
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,23 @@ public class OauthRestController {
public ApiResponse<OauthSignUpResponse> signUp(
@PathVariable String provider,
@RequestParam String code,
@RequestParam String redirectUrl,
@RequestPart(value = "profile", required = false) MultipartFile profile,
@Valid @RequestPart(value = "signupData") OauthSignUpRequest request) {
log.info("provider : {}, code : {}, profile : {}, {}", provider, code, profile, request);
log.info("provider : {}, code : {}, requestUrl : {}, profile : {}, request : {}", provider, code, redirectUrl,
profile, request);

oauthService.signUp(profile, request, provider, code);
oauthService.signUp(profile, request, provider, code, redirectUrl);
return ApiResponse.created("회원가입에 성공하였습니다.", null);
}

@PostMapping(value = "/{provider}/login")
public ApiResponse<OauthLoginResponse> login(
@PathVariable String provider,
@RequestParam String code,
@RequestParam String redirectUrl,
@Validated(ValidationSequence.class) @RequestBody OauthLoginRequest request) {
OauthLoginResponse response = oauthService.login(request, provider, code, LocalDateTime.now());
OauthLoginResponse response = oauthService.login(request, provider, code, LocalDateTime.now(), redirectUrl);
return ApiResponse.of(OK, "로그인에 성공하였습니다.", response);
}

Expand Down
15 changes: 9 additions & 6 deletions backend/src/main/java/codesquard/app/api/oauth/OauthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ public class OauthService {
private final OauthRedisService redisService;

public OauthSignUpResponse signUp(MultipartFile profile, OauthSignUpRequest request, String provider,
String authorizationCode) {
String authorizationCode, String redirectUrl) {
log.info("{}, provider : {}, authorizationCode : {}", request, provider,
authorizationCode);
validateDuplicateLoginId(request.getLoginId());

OauthUserProfileResponse userProfileResponse = getOauthUserProfileResponse(provider, authorizationCode);
OauthUserProfileResponse userProfileResponse = getOauthUserProfileResponse(provider, authorizationCode,
redirectUrl);
validateMultipleSignUp(userProfileResponse.getEmail());

Optional<MultipartFile> optionalProfile = Optional.ofNullable(profile);
Expand Down Expand Up @@ -105,11 +106,12 @@ private void validateMultipleSignUp(String email) {
}
}

private OauthUserProfileResponse getOauthUserProfileResponse(String provider, String authorizationCode) {
private OauthUserProfileResponse getOauthUserProfileResponse(String provider, String authorizationCode,
String redirectUrl) {
OauthClient oauthClient = oauthClientRepository.findOneBy(provider);

OauthAccessTokenResponse accessTokenResponse =
oauthClient.exchangeAccessTokenByAuthorizationCode(authorizationCode);
oauthClient.exchangeAccessTokenByAuthorizationCode(authorizationCode, redirectUrl);
log.debug("{}", accessTokenResponse);

OauthUserProfileResponse userProfileResponse =
Expand All @@ -118,10 +120,11 @@ private OauthUserProfileResponse getOauthUserProfileResponse(String provider, St
return userProfileResponse;
}

public OauthLoginResponse login(OauthLoginRequest request, String provider, String code, LocalDateTime now) {
public OauthLoginResponse login(OauthLoginRequest request, String provider, String code, LocalDateTime now,
String redirectUrl) {
log.info("{}, provider : {}, code : {}", request, provider, code);

OauthUserProfileResponse userProfileResponse = getOauthUserProfileResponse(provider, code);
OauthUserProfileResponse userProfileResponse = getOauthUserProfileResponse(provider, code, redirectUrl);

Member member = getLoginMember(request, userProfileResponse);
log.debug("로그인 서비스 요청 중 회원 객체 생성 : {}", member);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public KakaoOauthClient(OauthProperties.Kakao kakao) {
}

@Override
public OauthAccessTokenResponse exchangeAccessTokenByAuthorizationCode(String authorizationCode) {
MultiValueMap<String, String> formData = createFormData(authorizationCode);
public OauthAccessTokenResponse exchangeAccessTokenByAuthorizationCode(String authorizationCode,
String redirectUrl) {
MultiValueMap<String, String> formData = createFormData(authorizationCode, redirectUrl);

log.info("formData : {}", formData);

Expand Down Expand Up @@ -70,12 +71,12 @@ public OauthAccessTokenResponse exchangeAccessTokenByAuthorizationCode(String au
}

@Override
public MultiValueMap<String, String> createFormData(String authorizationCode) {
public MultiValueMap<String, String> createFormData(String authorizationCode, String redirectUrl) {
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("code", authorizationCode);
formData.add("client_id", getClientId());
formData.add("client_secret", getClientSecret());
formData.add("redirect_uri", getRedirectUri());
formData.add("redirect_uri", redirectUrl);
formData.add("grant_type", "authorization_code");
return formData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public NaverOauthClient(OauthProperties.Naver naver) {
}

@Override
public OauthAccessTokenResponse exchangeAccessTokenByAuthorizationCode(String authorizationCode) {
MultiValueMap<String, String> formData = createFormData(authorizationCode);
public OauthAccessTokenResponse exchangeAccessTokenByAuthorizationCode(String authorizationCode,
String redirectUrl) {
MultiValueMap<String, String> formData = createFormData(authorizationCode, redirectUrl);

OauthAccessTokenResponse response = WebClient.create()
.post()
Expand All @@ -54,10 +55,10 @@ public OauthAccessTokenResponse exchangeAccessTokenByAuthorizationCode(String au
}

@Override
public MultiValueMap<String, String> createFormData(String authorizationCode) {
public MultiValueMap<String, String> createFormData(String authorizationCode, String redirectUrl) {
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("code", authorizationCode);
formData.add("redirect_uri", getRedirectUri());
formData.add("redirect_uri", redirectUrl);
formData.add("grant_type", "authorization_code");
return formData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ public OauthClient(String clientId, String clientSecret, String tokenUri, String
this.redirectUri = redirectUri;
}

public abstract OauthAccessTokenResponse exchangeAccessTokenByAuthorizationCode(String authorizationCode);
public abstract OauthAccessTokenResponse exchangeAccessTokenByAuthorizationCode(String authorizationCode,
String redirectUrl);

public abstract MultiValueMap<String, String> createFormData(String authorizationCode);
public abstract MultiValueMap<String, String> createFormData(String authorizationCode, String redirectUrl);

public Map<String, Object> getUserAttributes(String userInfoUri, OauthAccessTokenResponse accessTokenResponse) {
return WebClient.create()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void signup() throws Exception {
responseBody.put("loginId", "23Yong");
OauthSignUpResponse response = objectMapper.readValue(objectMapper.writeValueAsString(responseBody),
OauthSignUpResponse.class);
given(oauthService.signUp(any(), any(OauthSignUpRequest.class), anyString(), anyString()))
given(oauthService.signUp(any(), any(OauthSignUpRequest.class), anyString(), anyString(), anyString()))
.willReturn(response);

Map<String, Object> requestBody = new HashMap<>();
Expand All @@ -91,7 +91,8 @@ public void signup() throws Exception {
mockMvc.perform(multipart("/api/auth/naver/signup")
.file(createMultipartFile("cat.png"))
.file(mockSignupData)
.param("code", "1234"))
.param("code", "1234")
.param("redirectUrl", "http://localhost:5173/my-account/oauth"))
.andExpect(status().isCreated())
.andExpect(jsonPath("statusCode").value(Matchers.equalTo(201)));
}
Expand All @@ -112,7 +113,8 @@ public void signupWhenInvalidLoginId(String loginId) throws Exception {
mockMvc.perform(multipart("/api/auth/naver/signup")
.file(createMultipartFile("cat.png"))
.file(mockSignupData)
.param("code", "1234"))
.param("code", "1234")
.param("redirectUrl", "http://localhost:5173/my-account/oauth"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("statusCode").value(Matchers.equalTo(400)))
.andExpect(jsonPath("message").value(Matchers.equalTo("유효하지 않은 입력형식입니다.")))
Expand All @@ -137,7 +139,8 @@ public void signupWhenInvalidAddrName(List<Long> addressIds) throws Exception {
mockMvc.perform(multipart("/api/auth/naver/signup")
.file(createMultipartFile("cat.png"))
.file(mockSignupData)
.param("code", "1234"))
.param("code", "1234")
.param("redirectUrl", "http://localhost:5173/my-account/oauth"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("statusCode").value(Matchers.equalTo(400)))
.andExpect(jsonPath("message").value(Matchers.equalTo("유효하지 않은 입력형식입니다.")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void signUp() throws IOException {
objectMapper.writeValueAsString(userProfileResponseBody), OauthUserProfileResponse.class);

given(oauthClientRepository.findOneBy(anyString())).willReturn(oauthClient);
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString()))
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString(), anyString()))
.willReturn(mockAccessTokenResponse);
given(oauthClient.getUserProfileByAccessToken(any(OauthAccessTokenResponse.class)))
.willReturn(mockUserProfileResponse);
Expand All @@ -128,7 +128,8 @@ public void signUp() throws IOException {
String provider = "naver";
String code = "1234";
// when
OauthSignUpResponse response = oauthService.signUp(createMultipartFile("cat.png"), request, provider, code);
OauthSignUpResponse response = oauthService.signUp(createMultipartFile("cat.png"), request, provider, code,
"http://localhost:5173/my-account/oauth");

// then
Member findMember = memberRepository.findMemberByLoginId("23Yong")
Expand Down Expand Up @@ -163,7 +164,7 @@ public void signupWithDuplicateLoginId() throws IOException {
String code = "1234";
// when
Throwable throwable = catchThrowable(
() -> oauthService.signUp(createMultipartFile("cat.png"), request, provider, code));
() -> oauthService.signUp(createMultipartFile("cat.png"), request, provider, code, null));
// then

assertThat(throwable)
Expand Down Expand Up @@ -200,7 +201,7 @@ public void signupWithMultipleLoginId() throws IOException {
objectMapper.writeValueAsString(userProfileResponseBody), OauthUserProfileResponse.class);

given(oauthClientRepository.findOneBy(anyString())).willReturn(oauthClient);
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString()))
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString(), anyString()))
.willReturn(mockAccessTokenResponse);
given(oauthClient.getUserProfileByAccessToken(any(OauthAccessTokenResponse.class)))
.willReturn(mockUserProfileResponse);
Expand All @@ -209,7 +210,8 @@ public void signupWithMultipleLoginId() throws IOException {
String code = "1234";
// when
Throwable throwable = catchThrowable(
() -> oauthService.signUp(createMultipartFile("cat.png"), request, provider, code));
() -> oauthService.signUp(createMultipartFile("cat.png"), request, provider, code,
"http://localhost:5173/my-account/oauth"));

// then

Expand Down Expand Up @@ -238,7 +240,7 @@ public void signUpWithInvalidProvider() throws IOException {
String code = "1234";
// when
Throwable throwable = catchThrowable(
() -> oauthService.signUp(createMultipartFile("cat.png"), request, provider, code));
() -> oauthService.signUp(createMultipartFile("cat.png"), request, provider, code, null));

// then
assertThat(throwable)
Expand Down Expand Up @@ -267,7 +269,7 @@ public void signUpWithInvalidCode() throws IOException {
objectMapper.readValue(objectMapper.writeValueAsString(responseBody), OauthAccessTokenResponse.class);

given(oauthClientRepository.findOneBy(anyString())).willReturn(oauthClient);
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString()))
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString(), anyString()))
.willReturn(mockAccessTokenResponse);
given(oauthClient.getUserProfileByAccessToken(any(OauthAccessTokenResponse.class)))
.willThrow(new RestApiException(OauthErrorCode.WRONG_AUTHORIZATION_CODE));
Expand All @@ -276,7 +278,8 @@ public void signUpWithInvalidCode() throws IOException {
String code = "1234";
// when
Throwable throwable = catchThrowable(
() -> oauthService.signUp(createMultipartFile("cat.png"), request, provider, code));
() -> oauthService.signUp(createMultipartFile("cat.png"), request, provider, code,
"http://localhost:5173/my-account/oauth"));

// then
assertThat(throwable)
Expand Down Expand Up @@ -324,13 +327,13 @@ public void signUpWhenDuplicateLoginId() throws IOException {

given(oauthClientRepository.findOneBy(anyString()))
.willReturn(oauthClient);
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString()))
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString(), anyString()))
.willReturn(mockAccessTokenResponse);
given(oauthClient.getUserProfileByAccessToken(any(OauthAccessTokenResponse.class)))
.willReturn(mockUserProfileResponse);

// when
Throwable throwable = catchThrowable(() -> oauthService.signUp(profile, request, provider, code));
Throwable throwable = catchThrowable(() -> oauthService.signUp(profile, request, provider, code, null));

// then
assertThat(throwable)
Expand Down Expand Up @@ -369,13 +372,14 @@ public void login() throws JsonProcessingException {
LocalDateTime now = createNow();
// mocking
given(oauthClientRepository.findOneBy(anyString())).willReturn(oauthClient);
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString()))
given(oauthClient.exchangeAccessTokenByAuthorizationCode(anyString(), anyString()))
.willReturn(mockAccessTokenResponse);
given(oauthClient.getUserProfileByAccessToken(any(OauthAccessTokenResponse.class)))
.willReturn(mockUserProfileResponse);

// when
OauthLoginResponse response = oauthService.login(request, provider, code, now);
OauthLoginResponse response = oauthService.login(request, provider, code, now,
"http://localhost:5173/my-account/oauth");

// then
assertThat(response)
Expand Down

0 comments on commit 6351f23

Please sign in to comment.