-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[YS-31] feat: 구글 OAuth 로그인 구현 #13
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
56683ae
feat: define domain models and enums for the project
chock-cho d328163
Merge branch 'dev' of https://github.com/YAPP-Github/25th-Web-Team-2-…
chock-cho b628066
refact: update domain structures for refactoring
chock-cho 2c55b09
fix: fixing some errors(Member, AuditingEntity)
chock-cho 7224c75
feat: 구글 OAuth 로그인 구현
chock-cho 8389202
fix: add dotenv libraries
chock-cho a90ec1b
fix: delete import which occurs dependency error
chock-cho 167c73d
fix: resolve context loading issue with active test profile
chock-cho 6d7ecbe
Merge branch 'dev' of https://github.com/YAPP-Github/25th-Web-Team-2-…
chock-cho 995bbfc
Merge branch 'dev' of https://github.com/YAPP-Github/25th-Web-Team-2-…
chock-cho 919cf9a
feat: implement google oauth login
chock-cho 260fcbe
refact: seperate responsibilities according to Clean Architecture
chock-cho 6f465bc
test: test code for Google OAuth login logic, AuditingEntity
chock-cho 91ad19d
test: add OauthMapperTest and MemberServiceTest for refact test coverage
chock-cho fe4ba57
Merge branch 'dev' of https://github.com/YAPP-Github/25th-Web-Team-2-…
chock-cho d9598f6
feat : add Google OAuth integration
chock-cho cf8d7a2
fix: add dependency code and custom exception code
chock-cho 85fa57f
fix: adjust things to build safely
chock-cho eaa7a47
refact: update test codes to satisfy the newly requirement
chock-cho e558b11
refact: reflect pr reviews and update changes
chock-cho 0c10ed2
refact: reflect code from reviews
chock-cho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/com/dobby/backend/application/mapper/OauthUserMapper.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,34 @@ | ||
package com.dobby.backend.application.mapper | ||
|
||
import com.dobby.backend.infrastructure.database.entity.Member | ||
import com.dobby.backend.infrastructure.database.entity.enum.MemberStatus | ||
import com.dobby.backend.infrastructure.database.entity.enum.ProviderType | ||
import com.dobby.backend.infrastructure.database.entity.enum.RoleType | ||
import com.dobby.backend.presentation.api.dto.request.OauthUserDto | ||
import com.dobby.backend.presentation.api.dto.response.OauthLoginResponse | ||
|
||
object OauthUserMapper { | ||
fun toDto( | ||
isRegistered: Boolean, | ||
accessToken: String, | ||
refreshToken: String, | ||
oauthEmail: String, | ||
oauthName: String, | ||
role: RoleType, | ||
provider: ProviderType, | ||
memberId: Long? = null | ||
): OauthLoginResponse { | ||
return OauthLoginResponse( | ||
isRegistered = isRegistered, | ||
accessToken = accessToken, | ||
refreshToken = refreshToken, | ||
memberInfo = OauthLoginResponse.MemberInfo( | ||
memberId = memberId, | ||
oauthEmail = oauthEmail, | ||
name = oauthName, | ||
role = role, | ||
provider = provider | ||
) | ||
) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
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.presentation.api.dto.response.OauthLoginResponse | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class OauthService( | ||
private val fetchGoogleUserInfoUseCase: FetchGoogleUserInfoUseCase | ||
) { | ||
fun getGoogleUserInfo(oauthLoginRequest: OauthLoginRequest): OauthLoginResponse { | ||
return fetchGoogleUserInfoUseCase.execute(oauthLoginRequest) | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
src/main/kotlin/com/dobby/backend/application/usecase/FetchGoogleUserInfoUseCase.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,68 @@ | ||
package com.dobby.backend.application.usecase | ||
|
||
import com.dobby.backend.application.mapper.OauthUserMapper | ||
import com.dobby.backend.domain.exception.OAuth2EmailNotFoundException | ||
import com.dobby.backend.domain.exception.OAuth2ProviderMissingException | ||
import com.dobby.backend.domain.exception.SignInMemberException | ||
import com.dobby.backend.infrastructure.config.properties.GoogleAuthProperties | ||
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.GoogleAuthFeignClient | ||
import com.dobby.backend.infrastructure.feign.GoogleUserInfoFeginClient | ||
import com.dobby.backend.infrastructure.token.JwtTokenProvider | ||
import com.dobby.backend.presentation.api.dto.request.GoogleTokenRequest | ||
import com.dobby.backend.presentation.api.dto.request.OauthLoginRequest | ||
import com.dobby.backend.presentation.api.dto.response.GoogleTokenResponse | ||
import com.dobby.backend.presentation.api.dto.response.OauthLoginResponse | ||
import com.dobby.backend.util.AuthenticationUtils | ||
import feign.FeignException | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class FetchGoogleUserInfoUseCase( | ||
private val googleAuthFeignClient: GoogleAuthFeignClient, | ||
private val googleUserInfoFeginClient: GoogleUserInfoFeginClient, | ||
private val jwtTokenProvider: JwtTokenProvider, | ||
private val googleAuthProperties: GoogleAuthProperties, | ||
private val memberRepository: MemberRepository | ||
){ | ||
fun execute(oauthLoginRequest: OauthLoginRequest) : OauthLoginResponse{ | ||
try { | ||
val googleTokenRequest = GoogleTokenRequest( | ||
code = oauthLoginRequest.authorizationCode, | ||
clientId = googleAuthProperties.clientId, | ||
clientSecret = googleAuthProperties.clientSecret, | ||
redirectUri = googleAuthProperties.redirectUri | ||
) | ||
|
||
val oauthRes = fetchAccessToken(googleTokenRequest) | ||
val oauthToken = oauthRes.accessToken | ||
|
||
val userInfo = googleUserInfoFeginClient.getUserInfo("Bearer $oauthToken") | ||
val email = userInfo.email as? String?: throw OAuth2EmailNotFoundException() | ||
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.GOOGLE | ||
) | ||
} catch (e : FeignException) { | ||
throw OAuth2ProviderMissingException() | ||
} | ||
} | ||
|
||
private fun fetchAccessToken(googleTokenRequest: GoogleTokenRequest): GoogleTokenResponse { | ||
return googleAuthFeignClient.getAccessToken(googleTokenRequest) | ||
} | ||
} |
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
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/domain/exception/OauthException.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.domain.exception | ||
|
||
open class OauthException ( | ||
errorCode: ErrorCode, | ||
) : DomainException(errorCode) | ||
class OAuth2AuthenticationException: OauthException(ErrorCode.OAUTH_USER_NOT_FOUND) | ||
class OAuth2ProviderMissingException: OauthException(ErrorCode.OAUTH_PROVIDER_MISSING) | ||
class OAuth2ProviderNotSupportedException: OauthException(ErrorCode.OAUTH_PROVIDER_NOT_FOUND) | ||
class OAuth2EmailNotFoundException : OauthException(ErrorCode.OAUTH_EMAIL_NOT_FOUND) | ||
class OAuth2NameNotFoundException : OauthException(ErrorCode.OAUTH_NAME_NOT_FOUND) | ||
|
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/dobby/backend/domain/exception/SignInRoleMismatchException.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.domain.exception | ||
|
||
import com.dobby.backend.infrastructure.database.entity.enum.RoleType | ||
import org.springframework.http.HttpStatus | ||
|
||
class SignInRoleMismatchException( | ||
registeredRole: RoleType?, | ||
requestedRole: RoleType | ||
) : RuntimeException( | ||
String.format( | ||
ErrorCode.SIGNIN_ROLE_MISMATCH.message, | ||
registeredRole?.name, | ||
requestedRole.name | ||
) | ||
) { | ||
val code: String = ErrorCode.SIGNIN_ROLE_MISMATCH.code | ||
val status: HttpStatus = ErrorCode.SIGNIN_ROLE_MISMATCH.httpStatus | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/dobby/backend/infrastructure/config/properties/GoogleAuthProperties.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.google") | ||
data class GoogleAuthProperties ( | ||
var clientId: String = "", | ||
var clientSecret: String= "", | ||
var redirectUri : String = "" | ||
) |
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
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/infrastructure/database/repository/MemberRepository.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.infrastructure.database.repository | ||
|
||
import com.dobby.backend.infrastructure.database.entity.Member | ||
import com.dobby.backend.infrastructure.database.entity.enum.MemberStatus | ||
import com.dobby.backend.infrastructure.database.entity.enum.ProviderType | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
interface MemberRepository : JpaRepository<Member, Long> { | ||
fun findByOauthEmailAndStatus(oauthEmail: String, status: MemberStatus): Member? | ||
} |
8 changes: 8 additions & 0 deletions
8
...main/kotlin/com/dobby/backend/infrastructure/database/repository/ParticipantRepository.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.infrastructure.database.repository | ||
|
||
import com.dobby.backend.infrastructure.database.entity.Participant | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
interface ParticipantRepository: JpaRepository<Participant, Long> { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/dobby/backend/infrastructure/feign/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.dobby.backend.infrastructure.feign | ||
|
||
import com.dobby.backend.presentation.api.dto.request.GoogleTokenRequest | ||
import com.dobby.backend.presentation.api.dto.response.GoogleTokenResponse | ||
import feign.Headers | ||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestParam | ||
|
||
@FeignClient( | ||
name = "google-auth-feign-client", | ||
url = "https://oauth2.googleapis.com", | ||
) | ||
interface GoogleAuthFeignClient { | ||
@PostMapping("/token") | ||
fun getAccessToken(@RequestBody googleTokenRequest: GoogleTokenRequest | ||
): GoogleTokenResponse | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기존 컬럼 규칙이 바뀌면 dev DB의 테이블도 이에 맞게 업데이트되어야 할 것 같습니다. 현재 DB에 데이터가 쌓이지 않은 상태라, 머지 전에 기존 테이블(
Member
,Participant
,Researcher
을 drop한 후 새로 업데이트된 테이블 정보로 create하면 문제없이 진행될 것 같아요! 😊