Skip to content
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

DRAW-428 소셜 로그인 이메일 가져오기 #15

Merged
merged 12 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ internal class OAuthAdapter(
override fun getPlatformUserName(authType: AuthType, platformUserId: String): String {
return ""
}

override fun getPlatformEmail(authType: AuthType, token: String): String? {
return when (authType) {
AuthType.APPLE_ID_TOKEN -> appleAuthService.getEmail(token)
AuthType.GOOGLE_ID_TOKEN -> googleAuthService.getEmail(token)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ internal class AppleAuthService(

return validator.validateAndGetSubject(token, publicKey)
}

internal fun getEmail(token: String): String? {
val publicKey = keyGenerator.generatePublicKey(token)

return validator.validateAndGetEmail(token, publicKey)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ internal class AppleIdTokenValidator(
aud = clientId,
) ?: throw OAuthFailureException
}

internal fun validateAndGetEmail(token: String, key: SignatureKey): String? {
return jwtProvider.validateAndGetClaim(
token = token,
key = key,
claimName = "email",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ internal class GoogleAuthService(
throw OAuthFailureException
}
}

fun getEmail(token: String): String? {
try {
val idToken = idTokenVerifier.verify(token)
return idToken?.payload?.email
} catch (e: GeneralSecurityException) {
return null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ internal class AuthUserJpaEntity : BaseJpaEntity() {
lateinit var platformUserId: String
protected set

@Column(name = "email", columnDefinition = "varchar(100)")
var email: String? = null

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@Cascade(CascadeType.PERSIST)
Expand All @@ -44,10 +47,12 @@ internal class AuthUserJpaEntity : BaseJpaEntity() {
platform: AuthPlatform,
platformUserId: String,
user: UserJpaEntity,
email: String?,
): AuthUserJpaEntity = AuthUserJpaEntity().apply {
this.platform = platform
this.platformUserId = platformUserId
this.user = user
this.email = email
}
}
}
10 changes: 5 additions & 5 deletions adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ internal class UserAdapter(
return authUserJpaRepository.findByUserId(userId.value)?.toDomain()
}

override fun createUser(platform: AuthPlatform, platformUserId: String, userName: String): UserInfo {
override fun createUser(platform: AuthPlatform, platformUserId: String, userName: String, email: String?): UserInfo {
val user = UserJpaEntity()
val authUser = authUserJpaRepository.save(AuthUserJpaEntity.of(platform, platformUserId, user))
val authUser = authUserJpaRepository.save(AuthUserJpaEntity.of(platform, platformUserId, user, email))
return authUser.user.toDomain()
}

Expand All @@ -44,9 +44,9 @@ internal class UserAdapter(
}

@Transactional
override fun transfer(userId: UserId, platform: AuthPlatform, platformUserId: String): UserInfo {
override fun transfer(userId: UserId, platform: AuthPlatform, platformUserId: String, email: String?): UserInfo {
val user = userJpaRepository.findByIdOrNull(userId.value) ?: throw NotFoundUserException
val authUser = authUserJpaRepository.save(AuthUserJpaEntity.of(platform, platformUserId, user))
val authUser = authUserJpaRepository.save(AuthUserJpaEntity.of(platform, platformUserId, user, email))
return authUser.user.toDomain()
}

Expand All @@ -59,6 +59,6 @@ internal class UserAdapter(

private fun AuthUserJpaEntity.toDomain(): AuthInfo = AuthInfo(
this.platform,
"[email protected]", // TODO
this.email,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table auth_user
add column email varchar(100) COMMENT '소셜 로그인에 사용 된 이메일';
10 changes: 6 additions & 4 deletions core/src/main/kotlin/com/xorker/draw/auth/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class AuthService(
@Transactional
override fun signIn(authType: AuthType, token: String): Token {
val platformUserId = authRepository.getPlatformUserId(authType, token)
val user = userRepository.getUser(authType.authPlatform, platformUserId) ?: createUser(authType, platformUserId)
val user = userRepository.getUser(authType.authPlatform, platformUserId) ?: createUser(authType, platformUserId, token)

return createToken(
userId = user.id,
Expand Down Expand Up @@ -65,7 +65,8 @@ internal class AuthService(

validateIsAnonymousUser(authType, platformUserId, userId)

val user = userRepository.transfer(userId, authType.authPlatform, platformUserId)
val email = authRepository.getPlatformEmail(authType, token)
val user = userRepository.transfer(userId, authType.authPlatform, platformUserId, email)

return createToken(
userId = user.id,
Expand All @@ -74,10 +75,11 @@ internal class AuthService(
)
}

private fun createUser(authType: AuthType, platformUserId: String): UserInfo {
private fun createUser(authType: AuthType, platformUserId: String, token: String): UserInfo {
val userName = authRepository.getPlatformUserName(authType, platformUserId)
val email = authRepository.getPlatformEmail(authType, token)

return userRepository.createUser(authType.authPlatform, platformUserId, userName)
return userRepository.createUser(authType.authPlatform, platformUserId, userName, email)
}

private fun validateIsAnonymousUser(
Expand Down
2 changes: 1 addition & 1 deletion domain/src/main/kotlin/com/xorker/draw/auth/AuthInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package com.xorker.draw.auth

data class AuthInfo(
val authPlatform: AuthPlatform,
val email: String,
val email: String?,
)
2 changes: 2 additions & 0 deletions domain/src/main/kotlin/com/xorker/draw/auth/AuthRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ interface AuthRepository {
fun getPlatformUserId(authType: AuthType, token: String): String

fun getPlatformUserName(authType: AuthType, platformUserId: String): String

fun getPlatformEmail(authType: AuthType, token: String): String?
}
4 changes: 2 additions & 2 deletions domain/src/main/kotlin/com/xorker/draw/user/UserRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ interface UserRepository {

fun getAuthInfo(userId: UserId): AuthInfo?

fun createUser(platform: AuthPlatform, platformUserId: String, userName: String): UserInfo
fun createUser(platform: AuthPlatform, platformUserId: String, userName: String, email: String?): UserInfo

fun createUser(userName: String?): UserInfo

fun withdrawal(userId: UserId)

fun transfer(userId: UserId, platform: AuthPlatform, platformUserId: String): UserInfo
fun transfer(userId: UserId, platform: AuthPlatform, platformUserId: String, email: String?): UserInfo

fun updateNickname(userId: UserId, nickname: String): User
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class JwtProvider {
return null
}

fun validateAndGetClaim(token: String, key: SignatureKey, claimName: String): String? {
val payload = parsePayload(token, key) ?: return null
return payload.get(claimName, String::class.java)
}

fun parseHeader(token: String): Map<String, String>? {
try {
val header = token.split(".")[0]
Expand Down
Loading