Skip to content

Commit

Permalink
[fix #155] 로그인 버그 수정 (#156)
Browse files Browse the repository at this point in the history
* feat : User sub 필드 추가

* feat : 구글, 애플 api sub값 파싱

* feat : sub값 기준으로 로그인 로직 수정
  • Loading branch information
dlswns2480 authored Sep 10, 2024
1 parent 78225cb commit a3b4974
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ data class PrincipalUser(
val id: Long,
val email: String,
val role: Role,
val authPlatform: AuthPlatform
val authPlatform: AuthPlatform,
val sub: String?
) : UserDetails {
companion object {
fun of(user: User) =
PrincipalUser(
id = user.id,
email = user.email,
role = user.role,
authPlatform = user.authPlatform
authPlatform = user.authPlatform,
sub = user.sub
)
}

Expand All @@ -36,4 +38,4 @@ data class PrincipalUser(
}
}

fun PrincipalUser.toDomain() = User(id = this.id, email = this.email, role = this.role, authPlatform = this.authPlatform)
fun PrincipalUser.toDomain() = User(id = this.id, email = this.email, role = this.role, authPlatform = this.authPlatform, sub = this.sub)
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class UserAdapter(
return savedUser.toDomain()
}

override fun loadByEmailAndAuthPlatform(email: String, authPlatform: AuthPlatform) =
userRepository.findByEmailAndAuthPlatformAndDeleted(email, authPlatform, false)
override fun loadBySubAndAuthPlatform(sub: String, authPlatform: AuthPlatform) =
userRepository.findBySubAndAuthPlatformAndDeleted(sub, authPlatform, false)
?.run { toDomain() }

override fun loadById(id: Long) = userRepository.findByIdAndDeleted(id, false)
Expand All @@ -36,4 +36,7 @@ class UserAdapter(
userRepository.findByDeleted(false)
.map { it.id }

override fun loadByEmailAndAuthPlatform(email: String, authPlatform: AuthPlatform) =
userRepository.findByEmailAndAuthPlatformAndDeleted(email, authPlatform, false)
?.run { toDomain() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class UserEntity(
var deleted: Boolean = false,

@Column(name = "is_registered")
var registered: Boolean
var registered: Boolean,

@Column(name = "sub")
var sub: String?
) : BaseEntity() {
fun delete() {
this.deleted = true
Expand All @@ -46,7 +49,8 @@ class UserEntity(
role = user.role,
nickname = user.nickName,
authPlatform = user.authPlatform,
registered = user.registered
registered = user.registered,
sub = user.sub
)
}
}
Expand All @@ -57,5 +61,6 @@ fun UserEntity.toDomain() = User(
role = this.role,
nickName = this.nickname,
authPlatform = this.authPlatform,
registered = this.registered
registered = this.registered,
sub = this.sub
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import com.pokit.token.model.AuthPlatform
import org.springframework.data.jpa.repository.JpaRepository

interface UserRepository : JpaRepository<UserEntity, Long> {
fun findByEmailAndAuthPlatformAndDeleted(email: String,authPlatform: AuthPlatform, deleted: Boolean): UserEntity?
fun findBySubAndAuthPlatformAndDeleted(sub: String, authPlatform: AuthPlatform, deleted: Boolean): UserEntity?

fun existsByNickname(nickname: String): Boolean

fun findByIdAndDeleted(id: Long, deleted: Boolean): UserEntity?

fun findByDeleted(deleted: Boolean): List<UserEntity>

fun findByEmailAndAuthPlatformAndDeleted(email: String, authPlatform: AuthPlatform, deleted: Boolean): UserEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import com.pokit.user.model.User

class UserFixture {
companion object {
fun getUser() = User(1L, "ij@naver.com", Role.USER, authPlatform = AuthPlatform.GOOGLE)
fun getUser() = User(1L, "ij@naver.com", Role.USER, authPlatform = AuthPlatform.GOOGLE, sub = "asdasd")

fun getUserInfo() = UserInfo("ig@naver.com", AuthPlatform.GOOGLE)
fun getUserInfo() = UserInfo("ig@naver.com", AuthPlatform.GOOGLE, sub = "asdasd")

fun getInvalidUser() = User(2L, "dls@naver.com", Role.USER, authPlatform = AuthPlatform.GOOGLE)
fun getInvalidUser() = User(2L, "dls@naver.com", Role.USER, authPlatform = AuthPlatform.GOOGLE, sub = "sub")

fun getSignUpRequest() = SignUpRequest("인주니", listOf(InterestType.SPORTS))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pokit.auth.common.dto

data class GoogleUserResponse(
val email: String
val email: String,
val sub: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class AppleApiAdapter(
override fun getUserInfo(idToken: String): UserInfo {
val claims = decodeAndVerifyIdToken(idToken) // id token을 통해 사용자 정보 추출
val email = claims["email"] as String

return UserInfo(email = email, authPlatform = AuthPlatform.APPLE)
val sub = claims["sub"] as String
return UserInfo(email = email, authPlatform = AuthPlatform.APPLE, sub = sub)
}

override fun revoke(refreshToken: String) {
Expand All @@ -38,7 +38,7 @@ class AppleApiAdapter(
revokeAuth(refreshToken, clientSecret)
}

private fun decodeAndVerifyIdToken(idToken: String): Map<String, Any> {
private fun decodeAndVerifyIdToken(idToken: String): Map<String, Any?> {
val publicKeys = appleFeignClient.getApplePublicKeys()

val header = appleTokenParser.parseHeader(idToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.pokit.auth.impl

import com.pokit.auth.common.property.GoogleProperty
import com.pokit.auth.common.support.GoogleFeignClient
import com.pokit.auth.port.out.GoogleApiClient
import com.pokit.common.exception.ClientValidationException
Expand All @@ -19,12 +18,13 @@ class GoogleApiAdapter(

return UserInfo(
email = response.email,
authPlatform = AuthPlatform.GOOGLE
authPlatform = AuthPlatform.GOOGLE,
sub = response.sub
)
}

override fun revoke(refreshToken: String) {
if(refreshToken.isBlank()) {
if (refreshToken.isBlank()) {
return
}
val revokeResponse = googleFeignClient.revoke(refreshToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ class AuthService(
AuthPlatform.GOOGLE -> googleApiClient.getUserInfo(request.idToken)
AuthPlatform.APPLE -> appleApiClient.getUserInfo(request.idToken)
}

val user = userPort.loadByEmailAndAuthPlatform(userInfo.email, platformType) ?: createUser(userInfo) // 없으면 저장
val user = userPort.loadBySubAndAuthPlatform(userInfo.sub, platformType)
?: getUserByEmail(userInfo) // 기존 로그인 유저용, 추후 제거
?: createUser(userInfo) // 없으면 저장

val token = tokenProvider.createToken(user.id)

Expand All @@ -63,7 +64,26 @@ class AuthService(
}

private fun createUser(userInfo: UserInfo): User {
val user = User(email = userInfo.email, role = Role.USER, authPlatform = userInfo.authPlatform)
val user = User(
email = userInfo.email!!, // 존재하지 않았던 유저면 이메일 항상 존재
role = Role.USER,
authPlatform = userInfo.authPlatform,
sub = userInfo.sub
)
return userPort.persist(user)
}

private fun getUserByEmail(userInfo: UserInfo): User? {
val userEmail = userInfo.email

return if(userEmail == null) {
return null
} else {
val user = userPort.loadByEmailAndAuthPlatform(userEmail, userInfo.authPlatform)
user?.let {
it.insertSub(userInfo.sub)
userPort.persist(user)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.pokit.user.model.User
interface UserPort {
fun persist(user: User): User

fun loadByEmailAndAuthPlatform(email: String, authPlatform: AuthPlatform): User?
fun loadBySubAndAuthPlatform(sub: String, authPlatform: AuthPlatform): User?

fun loadById(id: Long): User?

Expand All @@ -15,4 +15,6 @@ interface UserPort {
fun delete(user: User)

fun loadAllIds(): List<Long>

fun loadByEmailAndAuthPlatform(email: String, authPlatform: AuthPlatform): User?
}
5 changes: 3 additions & 2 deletions domain/src/main/kotlin/com/pokit/user/dto/UserInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pokit.user.dto
import com.pokit.token.model.AuthPlatform

data class UserInfo(
val email: String,
val authPlatform: AuthPlatform
var email: String?,
val authPlatform: AuthPlatform,
val sub: String
)
7 changes: 6 additions & 1 deletion domain/src/main/kotlin/com/pokit/user/model/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ data class User(
val role: Role,
var nickName: String = email,
val authPlatform: AuthPlatform,
var registered: Boolean = false
var registered: Boolean = false,
var sub: String?
) {
fun register(nickName: String) {
this.nickName = nickName
this.registered = true
}

fun insertSub(sub: String) {
this.sub = sub
}

fun modifyNickname(nickName: String) {
this.nickName = nickName
}
Expand Down

0 comments on commit a3b4974

Please sign in to comment.