-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[YS-32] feat: Naver OAuth 로그인 추가 (#20)
* feat: add feignclient for naver * feat: add FetchNaverUserInfoUseCase logic * test: add FetchNaverUserInfoUseCaseTest * chore: add naver variable to template-application-local.yml * refact: delete unused import and move testcode to appropriate package * refact: refactor NaverUserInfoFeignClient interface * refact: move class file to appropriate package
- Loading branch information
Showing
32 changed files
with
348 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 9 additions & 3 deletions
12
src/main/kotlin/com/dobby/backend/application/service/OauthService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,22 @@ | ||
package com.dobby.backend.application.service | ||
|
||
import com.dobby.backend.application.usecase.FetchGoogleUserInfoUseCase | ||
import com.dobby.backend.presentation.api.dto.request.OauthLoginRequest | ||
import com.dobby.backend.application.usecase.FetchNaverUserInfoUseCase | ||
import com.dobby.backend.presentation.api.dto.request.auth.GoogleOauthLoginRequest | ||
import com.dobby.backend.presentation.api.dto.request.auth.NaverOauthLoginRequest | ||
import com.dobby.backend.presentation.api.dto.response.auth.OauthLoginResponse | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class OauthService( | ||
private val fetchGoogleUserInfoUseCase: FetchGoogleUserInfoUseCase | ||
private val fetchGoogleUserInfoUseCase: FetchGoogleUserInfoUseCase, | ||
private val fetchNaverUserInfoUseCase: FetchNaverUserInfoUseCase, | ||
) { | ||
fun getGoogleUserInfo(oauthLoginRequest: OauthLoginRequest): OauthLoginResponse { | ||
fun getGoogleUserInfo(oauthLoginRequest: GoogleOauthLoginRequest): OauthLoginResponse { | ||
return fetchGoogleUserInfoUseCase.execute(oauthLoginRequest) | ||
} | ||
|
||
fun getNaverUserInfo(oauthLoginRequest: NaverOauthLoginRequest): OauthLoginResponse { | ||
return fetchNaverUserInfoUseCase.execute(oauthLoginRequest) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/dobby/backend/application/service/SignupService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/kotlin/com/dobby/backend/application/usecase/FetchNaverUserInfoUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.dobby.backend.application.usecase | ||
|
||
import com.dobby.backend.application.mapper.OauthUserMapper | ||
import com.dobby.backend.domain.exception.SignInMemberException | ||
import com.dobby.backend.infrastructure.config.properties.NaverAuthProperties | ||
import com.dobby.backend.infrastructure.database.entity.enum.MemberStatus | ||
import com.dobby.backend.infrastructure.database.entity.enum.ProviderType | ||
import com.dobby.backend.infrastructure.database.repository.MemberRepository | ||
import com.dobby.backend.infrastructure.feign.naver.NaverAuthFeignClient | ||
import com.dobby.backend.infrastructure.feign.naver.NaverUserInfoFeignClient | ||
import com.dobby.backend.infrastructure.token.JwtTokenProvider | ||
import com.dobby.backend.presentation.api.dto.request.auth.NaverOauthLoginRequest | ||
import com.dobby.backend.presentation.api.dto.request.auth.NaverTokenRequest | ||
import com.dobby.backend.presentation.api.dto.response.auth.NaverTokenResponse | ||
import com.dobby.backend.presentation.api.dto.response.auth.OauthLoginResponse | ||
import com.dobby.backend.util.AuthenticationUtils | ||
|
||
class FetchNaverUserInfoUseCase( | ||
private val naverAuthFeignClient: NaverAuthFeignClient, | ||
private val naverUserInfoFeginClient: NaverUserInfoFeignClient, | ||
private val jwtTokenProvider: JwtTokenProvider, | ||
private val naverAuthProperties: NaverAuthProperties, | ||
private val memberRepository: MemberRepository | ||
) : UseCase<NaverOauthLoginRequest, OauthLoginResponse> { | ||
|
||
override fun execute(input: NaverOauthLoginRequest): OauthLoginResponse { | ||
try { | ||
val naverTokenRequest = NaverTokenRequest( | ||
grantType = "authorization_code", | ||
clientId = naverAuthProperties.clientId, | ||
clientSecret = naverAuthProperties.clientSecret, | ||
code = input.authorizationCode, | ||
state = input.state | ||
) | ||
|
||
val oauthRes = fetchAccessToken(naverTokenRequest) | ||
val oauthToken = oauthRes.accessToken | ||
|
||
val userInfo = naverUserInfoFeginClient.getUserInfo("Bearer $oauthToken") | ||
val email = userInfo.email | ||
val regMember = memberRepository.findByOauthEmailAndStatus(email, MemberStatus.ACTIVE) | ||
?: throw SignInMemberException() | ||
|
||
val regMemberAuthentication = AuthenticationUtils.createAuthentication(regMember) | ||
val jwtAccessToken = jwtTokenProvider.generateAccessToken(regMemberAuthentication) | ||
val jwtRefreshToken = jwtTokenProvider.generateRefreshToken(regMemberAuthentication) | ||
|
||
return OauthUserMapper.toDto( | ||
isRegistered = true, | ||
accessToken = jwtAccessToken, | ||
refreshToken = jwtRefreshToken, | ||
oauthEmail = regMember.oauthEmail, | ||
oauthName = regMember.name ?: throw SignInMemberException(), | ||
role = regMember.role ?: throw SignInMemberException(), | ||
provider = ProviderType.NAVER | ||
) | ||
} catch (e: SignInMemberException) { | ||
throw SignInMemberException() | ||
} | ||
} | ||
|
||
private fun fetchAccessToken(naverTokenRequest: NaverTokenRequest): NaverTokenResponse { | ||
return naverAuthFeignClient.getAccessToken(naverTokenRequest) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/dobby/backend/infrastructure/config/properties/NaverAuthProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.dobby.backend.infrastructure.config.properties | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = "spring.security.oauth.client.registration.naver") | ||
data class NaverAuthProperties ( | ||
var clientId: String = "", | ||
var clientSecret: String= "", | ||
var redirectUri : String = "" | ||
) |
6 changes: 3 additions & 3 deletions
6
...astructure/feign/GoogleAuthFeignClient.kt → ...ure/feign/google/GoogleAuthFeignClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ucture/feign/GoogleUserInfoFeginClient.kt → ...feign/google/GoogleUserInfoFeginClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/dobby/backend/infrastructure/feign/naver/NaverAuthFeignClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.dobby.backend.infrastructure.feign.naver | ||
|
||
import com.dobby.backend.presentation.api.dto.request.auth.NaverTokenRequest | ||
import com.dobby.backend.presentation.api.dto.response.auth.NaverTokenResponse | ||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
|
||
@FeignClient( | ||
name = "naver-auth-feign-client", | ||
url = "https://nid.naver.com/oauth2.0/token" | ||
) | ||
interface NaverAuthFeignClient { | ||
@PostMapping | ||
fun getAccessToken( | ||
@RequestBody naverTokenRequest: NaverTokenRequest | ||
): NaverTokenResponse | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/dobby/backend/infrastructure/feign/naver/NaverUserInfoFeignClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.dobby.backend.infrastructure.feign.naver | ||
|
||
import com.dobby.backend.presentation.api.dto.response.auth.NaverInfoResponse | ||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestHeader | ||
|
||
@FeignClient( | ||
name = "naver-userinfo-feign-client", | ||
url = "https://openapi.naver.com/v1/nid/me" | ||
) | ||
interface NaverUserInfoFeignClient { | ||
@PostMapping | ||
fun getUserInfo(@RequestHeader("Authorization") accessToken: String): NaverInfoResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...dobby/backend/presentation/api/controller/SignupController/ParticipantSignupController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
src/main/kotlin/com/dobby/backend/presentation/api/dto/request/OauthUserDto.kt
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
...tion/api/dto/request/OauthLoginRequest.kt → ...o/request/auth/GoogleOauthLoginRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package com.dobby.backend.presentation.api.dto.request | ||
package com.dobby.backend.presentation.api.dto.request.auth | ||
|
||
import jakarta.validation.constraints.NotBlank | ||
|
||
data class OauthLoginRequest( | ||
data class GoogleOauthLoginRequest( | ||
@NotBlank(message = "authorizationCode는 공백일 수 없습니다.") | ||
val authorizationCode: String | ||
) | ||
) |
2 changes: 1 addition & 1 deletion
2
...ion/api/dto/request/GoogleTokenRequest.kt → ...pi/dto/request/auth/GoogleTokenRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../dto/request/MemberRefreshTokenRequest.kt → ...request/auth/MemberRefreshTokenRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...main/kotlin/com/dobby/backend/presentation/api/dto/request/auth/NaverOauthLoginRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.dobby.backend.presentation.api.dto.request.auth | ||
|
||
import jakarta.validation.constraints.NotBlank | ||
|
||
data class NaverOauthLoginRequest( | ||
@NotBlank(message = "authorizationCode는 공백일 수 없습니다.") | ||
val authorizationCode: String, | ||
|
||
@NotBlank(message = "state는 공백일 수 없습니다.") | ||
val state: String, | ||
) |
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/com/dobby/backend/presentation/api/dto/request/auth/NaverTokenRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.dobby.backend.presentation.api.dto.request.auth | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class NaverTokenRequest ( | ||
@JsonProperty("grant_type") | ||
val grantType: String = "authorization_code", | ||
|
||
@JsonProperty("client_id") | ||
val clientId: String, | ||
|
||
@JsonProperty("client_secret") | ||
val clientSecret: String, | ||
|
||
@JsonProperty("code") | ||
val code: String, | ||
|
||
@JsonProperty("state") | ||
val state: String | ||
) |
2 changes: 1 addition & 1 deletion
2
...i/dto/request/ParticipantSignupRequest.kt → ...equest/signup/ParticipantSignupRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...esponse/auth/google/GoogleInfoResponse.kt → ...i/dto/response/auth/GoogleInfoResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...sponse/auth/google/GoogleTokenResponse.kt → .../dto/response/auth/GoogleTokenResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/dobby/backend/presentation/api/dto/response/auth/NaverInfoResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.dobby.backend.presentation.api.dto.response.auth | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class NaverInfoResponse( | ||
@JsonProperty("email") | ||
val email: String, | ||
|
||
@JsonProperty("name") | ||
val name: String | ||
) |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/dobby/backend/presentation/api/dto/response/auth/NaverTokenResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.dobby.backend.presentation.api.dto.response.auth | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class NaverTokenResponse ( | ||
@JsonProperty("access_token") | ||
val accessToken: String? | ||
) |
Oops, something went wrong.