Skip to content

Commit

Permalink
Merge pull request #47 from GOAT-WAAAM/dev/encJ/oauth-naver
Browse files Browse the repository at this point in the history
[FEAT] 네이버 소셜로그인 추가
  • Loading branch information
encoreJeong authored Jul 22, 2024
2 parents cc2cc11 + ad00399 commit 00c3637
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.goat.server.auth.application;

import com.goat.server.auth.dto.request.KakaoLoginParams;
import com.goat.server.auth.dto.request.NaverLoginParams;
import com.goat.server.auth.dto.request.OAuthLoginParams;
import com.goat.server.auth.dto.response.OAuthInfoResponse;
import com.goat.server.global.util.jwt.JwtUserDetails;
Expand Down Expand Up @@ -50,7 +51,7 @@ private OAuthLoginParams generateOAuthLoginParams(String provider, String access
}

if (provider.equals("NAVER")) {
// return new NaverLoginParams(accessToken);
return new NaverLoginParams(accessToken);
}

throw new IllegalArgumentException("Invalid provider: " + provider);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.goat.server.auth.dto.request;

import com.goat.server.auth.domain.type.OAuthProvider;
import org.springframework.util.MultiValueMap;

public record KakaoLoginParams(
String kakaoAccessToken
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.goat.server.auth.dto.request;

import com.goat.server.auth.domain.type.OAuthProvider;

public record NaverLoginParams(
String naverAccessToken
) implements OAuthLoginParams {

@Override
public OAuthProvider oAuthProvider() {
return OAuthProvider.NAVER;
}

@Override
public String accessToken() {
return naverAccessToken;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.goat.server.auth.dto.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.goat.server.auth.domain.type.OAuthProvider;
import lombok.Builder;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public record NaverInfoResponse(

Response response

) implements OAuthInfoResponse {

@Override
public Long getId() {
return response.id();
}

@Override
public String getNickname() {
return response.nickname();
}

@Override
public OAuthProvider getOAuthProvider() {
return OAuthProvider.NAVER;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public record Response(
Long id,
String nickname
) {
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/goat/server/auth/util/NaverApiClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.goat.server.auth.util;

import com.goat.server.auth.domain.type.OAuthProvider;
import com.goat.server.auth.dto.response.NaverInfoResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

@FeignClient(name = "naverApiClient", url = "${oauth.naver.service.api_url}")
@Component
public interface NaverApiClient extends OAuthApiClient {

@Override
@GetMapping(value = "/v1/nid/me")
NaverInfoResponse requestOauthInfo(@RequestHeader(HttpHeaders.AUTHORIZATION) String accessToken);

@Override
default OAuthProvider oAuthProvider() {
return OAuthProvider.NAVER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
@SpringBootTest
@AutoConfigureWireMock(port = 0)
@TestPropertySource(properties = {
"oauth.kakao.service.api_url=http://localhost:${wiremock.server.port}"
"oauth.kakao.service.api_url=http://localhost:${wiremock.server.port}",
"oauth.naver.service.api_url=http://localhost:${wiremock.server.port}"
})
@DisplayName("OAuth 로그인 테스트")
class OAuthLoginServiceTest {
Expand Down Expand Up @@ -64,7 +65,7 @@ void afterEach() {

@Test
@DisplayName("카카오_소셜로그인_테스트")
void socialLoginTest() throws JsonProcessingException {
void KakaoSocialLoginTest() throws JsonProcessingException {
//given
final String oauthProvider = "KAKAO";
final String kakaoAccessToken = "thisIsmockAccessToken";
Expand Down Expand Up @@ -98,4 +99,38 @@ void socialLoginTest() throws JsonProcessingException {
assertThat(directoryRepository.findStorageDirectoryByUser(1L).get().getTitle()).isEqualTo("storage_directory");
}

@Test
@DisplayName("네이버_소셜로그인_테스트")
void naverSocialLoginTest() throws JsonProcessingException {
//given
final String oauthProvider = "NAVER";
final String naverAccessToken = "thisIsmockAccessToken";
final String expectedResponse = objectMapper.writeValueAsString(
Map.of(
"resultCode", "00",
"message", "success",
"response", Map.of(
"id", "1203443",
"nickname", "nickname",
"profile_image", "http://example.com/profile.jpg"
)
)
);

//wireMock 사용
stubFor(get(urlEqualTo("/v1/nid/me"))
.willReturn(aResponse()
.withStatus(HttpStatus.OK.value())
.withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.withBody(expectedResponse))
);


//when
OAuthLoginService.socialLogin(oauthProvider, naverAccessToken);

//then
assertNotNull(userRepository.findBySocialId("1203443"));
}

}

0 comments on commit 00c3637

Please sign in to comment.