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-390 구글 로그인 #10

Merged
merged 4 commits into from
Oct 24, 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
1 change: 1 addition & 0 deletions adapter/oauth/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {

implementation("org.springframework.boot:spring-boot-starter:${Versions.SPRING_BOOT}")
implementation("org.springframework.cloud:spring-cloud-starter-openfeign:${Versions.OPEN_FEIGN}")
implementation("com.google.api-client:google-api-client:${Versions.GOOGLE_API_CLIENT}")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package com.xorker.draw.oauth
import com.xorker.draw.auth.AuthRepository
import com.xorker.draw.auth.AuthType
import com.xorker.draw.oauth.apple.AppleAuthService
import com.xorker.draw.oauth.google.GoogleAuthService
import org.springframework.stereotype.Component

@Component
internal class OAuthAdapter(
private val appleAuthService: AppleAuthService,
private val googleAuthService: GoogleAuthService,
) : AuthRepository {
override fun getPlatformUserId(authType: AuthType, token: String): String {
return when (authType) {
AuthType.APPLE_ID_TOKEN -> appleAuthService.getPlatformUserId(token)
AuthType.GOOGLE_ID_TOKEN -> googleAuthService.getPlatformUserId(token)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.xorker.draw.oauth.google

import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Configuration

@Configuration
@EnableConfigurationProperties(GoogleApiProperties::class)
class GoogleApiConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.xorker.draw.oauth.google

import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties("oauth.google")
data class GoogleApiProperties(
val clientId: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.xorker.draw.oauth.google

import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier
import com.google.api.client.http.javanet.NetHttpTransport
import com.google.api.client.json.gson.GsonFactory
import com.xorker.draw.exception.OAuthFailureException
import java.security.GeneralSecurityException
import org.springframework.stereotype.Component

@Component
internal class GoogleAuthService(
googleApiProperties: GoogleApiProperties,
) {
private val idTokenVerifier =
GoogleIdTokenVerifier.Builder(NetHttpTransport(), GsonFactory.getDefaultInstance())
.setAudience(listOf(googleApiProperties.clientId))
.build()

fun getPlatformUserId(token: String): String {
try {
val idToken = idTokenVerifier.verify(token)
return idToken?.payload?.subject ?: throw OAuthFailureException
} catch (e: GeneralSecurityException) {
throw OAuthFailureException
}
}
}
2 changes: 2 additions & 0 deletions adapter/oauth/src/main/resources/application-oauth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ oauth:
apple:
iss: https://appleid.apple.com
client-id: ${APPLE_CLIENT_ID}
google:
client-id: ${GOOGLE_CLIENT_ID}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ internal class UserAdapter(
private val userJpaRepository: UserJpaRepository,
private val authUserJpaRepository: AuthUserJpaRepository,
) : UserRepository {
override fun getUser(platform: AuthPlatform, platformUserId: String): User? =
override fun getUser(platform: AuthPlatform, platformUserId: String): UserInfo? =
authUserJpaRepository.find(platform, platformUserId)?.user?.toDomain()

override fun getUser(userId: UserId): User? =
override fun getUser(userId: UserId): UserInfo? =
userJpaRepository.findByIdOrNull(userId.value)?.toDomain()

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

override fun createUser(userName: String): User {
override fun createUser(userName: String?): UserInfo {
val user = UserJpaEntity.of(userName)
val savedUser = userJpaRepository.save(user)
return savedUser.toDomain()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.xorker.draw.user

import com.xorker.draw.BaseJpaEntity
import com.xorker.draw.exception.InvalidUserStatusException
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
Expand Down Expand Up @@ -35,7 +34,7 @@ internal class UserJpaEntity : BaseJpaEntity() {
internal fun of(id: Long): UserJpaEntity =
UserJpaEntity().apply { this.id = id }

internal fun of(name: String): UserJpaEntity =
internal fun of(name: String?): UserJpaEntity =
UserJpaEntity().apply { this.name = name }

internal fun from(user: User): UserJpaEntity =
Expand All @@ -54,7 +53,7 @@ internal class UserJpaEntity : BaseJpaEntity() {
}
}

internal fun UserJpaEntity.toDomain(): User = User(
internal fun UserJpaEntity.toDomain(): UserInfo = UserInfo(
id = UserId(this.id),
name = this.name ?: throw InvalidUserStatusException,
name = this.name,
)
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ object Versions {
const val MICROMETER = "1.12.9"
const val SENTRY = "7.0.0"
const val FIREBASE = "9.3.0"
const val GOOGLE_API_CLIENT = "1.32.1"
}
6 changes: 3 additions & 3 deletions core/src/main/kotlin/com/xorker/draw/auth/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.xorker.draw.auth
import com.xorker.draw.auth.token.AccessTokenRepository
import com.xorker.draw.auth.token.RefreshTokenRepository
import com.xorker.draw.auth.token.Token
import com.xorker.draw.user.User
import com.xorker.draw.user.UserId
import com.xorker.draw.user.UserInfo
import com.xorker.draw.user.UserRepository
import java.time.Duration
import java.time.Period
Expand All @@ -28,7 +28,7 @@ internal class AuthService(
}

override fun anonymousSignIn(): Token {
val user = userRepository.createUser(""); // TODO 이름 정책 정해지면 변경 예정
val user = userRepository.createUser(null); // TODO 이름 정책 정해지면 변경 예정

return createToken(user.id, Period.ofYears(100))
}
Expand All @@ -45,7 +45,7 @@ internal class AuthService(
userRepository.withdrawal(userId)
}

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

return userRepository.createUser(authType.authPlatform, platformUserId, userName)
Expand Down
14 changes: 8 additions & 6 deletions domain/src/main/kotlin/com/xorker/draw/auth/AuthPlatform.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.xorker.draw.auth

enum class AuthPlatform(description: String) {
APPLE("애플"),
;
}
package com.xorker.draw.auth

enum class AuthPlatform(description: String) {
APPLE("애플"),
GOOGLE("구글"),

;
}
1 change: 1 addition & 0 deletions domain/src/main/kotlin/com/xorker/draw/auth/AuthType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.xorker.draw.auth

enum class AuthType(val authPlatform: AuthPlatform) {
APPLE_ID_TOKEN(AuthPlatform.APPLE),
GOOGLE_ID_TOKEN(AuthPlatform.GOOGLE),
}
5 changes: 5 additions & 0 deletions domain/src/main/kotlin/com/xorker/draw/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ data class User(
val id: UserId,
val name: String,
)

data class UserInfo(
val id: UserId,
val name: String?,
)
8 changes: 4 additions & 4 deletions domain/src/main/kotlin/com/xorker/draw/user/UserRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package com.xorker.draw.user
import com.xorker.draw.auth.AuthPlatform

interface UserRepository {
fun getUser(platform: AuthPlatform, platformUserId: String): User?
fun getUser(platform: AuthPlatform, platformUserId: String): UserInfo?

fun getUser(userId: UserId): User?
fun getUser(userId: UserId): UserInfo?

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

fun createUser(userName: String): User
fun createUser(userName: String?): UserInfo

fun withdrawal(userId: UserId)
}
Loading