Skip to content

Commit

Permalink
Merge branch 'refs/heads/feature/DRAW-347' into feature/DRAW-404
Browse files Browse the repository at this point in the history
  • Loading branch information
SunwoongH committed Nov 3, 2024
2 parents 4282546 + 37f2048 commit 0fbee2e
Show file tree
Hide file tree
Showing 41 changed files with 277 additions and 181 deletions.
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,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xorker.draw.config

import com.xorker.draw.mafia.dto.RedisMafiaGameInfo
import com.xorker.draw.mafia.dto.MafiaGameInfoRedisEntity
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand Down Expand Up @@ -32,13 +32,13 @@ internal class RedisConfig {
}

@Bean
fun redisTemplateWithObject(connectionFactory: RedisConnectionFactory): RedisTemplate<String, RedisMafiaGameInfo> {
val template = RedisTemplate<String, RedisMafiaGameInfo>()
fun redisTemplateWithObject(connectionFactory: RedisConnectionFactory): RedisTemplate<String, MafiaGameInfoRedisEntity> {
val template = RedisTemplate<String, MafiaGameInfoRedisEntity>()

template.connectionFactory = connectionFactory

template.keySerializer = StringRedisSerializer()
template.valueSerializer = Jackson2JsonRedisSerializer(RedisMafiaGameInfo::class.java)
template.valueSerializer = Jackson2JsonRedisSerializer(MafiaGameInfoRedisEntity::class.java)

return template
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ internal class RedisLockAdapter(
private val redisTemplate: RedisTemplate<String, String>,
) : LockRepository {

override fun lock(key: String) {
override fun <R> lock(key: String, call: () -> R): R {
while (getLock(key).not()) {
try {
Thread.sleep(SLEEP_TIME)
} catch (e: InterruptedException) {
throw UnSupportedException
}
}
try {
return call.invoke()
} finally {
unlock(key)
}
}

override fun unlock(key: String) {
private fun unlock(key: String) {
redisTemplate.delete(key + LOCK)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xorker.draw.mafia

import com.xorker.draw.mafia.dto.RedisMafiaGameInfo
import com.xorker.draw.mafia.dto.toRedisMafiaGameInfo
import com.xorker.draw.mafia.dto.MafiaGameInfoRedisEntity
import com.xorker.draw.mafia.dto.toMafiaGameInfoRedisEntity
import com.xorker.draw.room.Room
import com.xorker.draw.room.RoomId
import com.xorker.draw.room.RoomRepository
Expand All @@ -14,7 +14,7 @@ import org.springframework.stereotype.Component
@Component
internal class MafiaGameAdapter(
private val metricManager: MetricManager,
private val redisTemplateWithObject: RedisTemplate<String, RedisMafiaGameInfo>,
private val redisTemplateWithObject: RedisTemplate<String, MafiaGameInfoRedisEntity>,
private val redisTemplate: RedisTemplate<String, String>,
private val timerRepository: TimerRepository,
) : MafiaGameRepository, RoomRepository {
Expand All @@ -34,7 +34,7 @@ internal class MafiaGameAdapter(

redisTemplateWithObject
.opsForValue()
.set(room.id.value, gameInfo.toRedisMafiaGameInfo())
.set(room.id.value, gameInfo.toMafiaGameInfoRedisEntity())

room.players.forEach {
redisTemplate
Expand Down Expand Up @@ -66,7 +66,7 @@ internal class MafiaGameAdapter(
return redisTemplateWithObject
.opsForValue()
.get(roomId.value)
?.toMafiaGameInfo()
?.toDomain()
}

override fun getGameInfo(userId: UserId): MafiaGameInfo? {
Expand All @@ -77,7 +77,7 @@ internal class MafiaGameAdapter(
return redisTemplateWithObject
.opsForValue()
.get(roomId)
?.toMafiaGameInfo()
?.toDomain()
}

override fun removePlayer(userId: UserId) {
Expand All @@ -88,7 +88,7 @@ internal class MafiaGameAdapter(
return redisTemplateWithObject
.opsForValue()
.get(roomId.value)
?.toMafiaGameInfo()
?.toDomain()
?.room
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.xorker.draw.mafia.MafiaGameInfo

data class RedisMafiaGameInfo @JsonCreator constructor(
@JsonProperty("room") val room: RedisMafiaRoom,
@JsonProperty("phase") val phase: RedisMafiaPhase,
@JsonProperty("gameOption") val gameOption: RedisMafiaGameOption,
data class MafiaGameInfoRedisEntity @JsonCreator constructor(
@JsonProperty("room") val room: MafiaRoomRedisEntity,
@JsonProperty("phase") val phase: MafiaPhaseRedisEntity,
@JsonProperty("gameOption") val gameOption: MafiaGameOptionRedisEntity,
) {
fun toMafiaGameInfo(): MafiaGameInfo = MafiaGameInfo(
room = room.toRoom(),
phase = phase.toMafiaPhase(),
gameOption = gameOption.toGameOption(),
fun toDomain(): MafiaGameInfo = MafiaGameInfo(
room = room.toDomain(),
phase = phase.toDomain(),
gameOption = gameOption.toDomain(),
)
}

fun MafiaGameInfo.toRedisMafiaGameInfo(): RedisMafiaGameInfo = RedisMafiaGameInfo(
room = RedisMafiaRoom(
fun MafiaGameInfo.toMafiaGameInfoRedisEntity(): MafiaGameInfoRedisEntity = MafiaGameInfoRedisEntity(
room = MafiaRoomRedisEntity(
id = room.id.value,
locale = room.locale,
owner = RedisMafiaPlayer(
owner = MafiaPlayerRedisEntity(
id = room.owner.userId.value,
nickname = room.owner.nickname,
color = room.owner.color,
isConnect = room.owner.isConnect(),
),
maxMemberNum = room.maxMemberNum,
players = room.players.map { player ->
RedisMafiaPlayer(
MafiaPlayerRedisEntity(
id = player.userId.value,
nickname = player.nickname,
color = player.color,
Expand All @@ -37,8 +37,8 @@ fun MafiaGameInfo.toRedisMafiaGameInfo(): RedisMafiaGameInfo = RedisMafiaGameInf
},
isRandomMatching = room.isRandomMatching,
),
phase = phase.toRedisMafiaPhase(),
gameOption = RedisMafiaGameOption(
phase = phase.toMafiaPhaseRedisEntity(),
gameOption = MafiaGameOptionRedisEntity(
minimum = gameOption.minimum,
maximum = gameOption.maximum,
readyTime = gameOption.readyTime.toMillis(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
import com.xorker.draw.mafia.MafiaGameOption
import java.time.Duration

data class RedisMafiaGameOption @JsonCreator constructor(
data class MafiaGameOptionRedisEntity @JsonCreator constructor(
@JsonProperty("minimum") val minimum: Int,
@JsonProperty("maximum") val maximum: Int,
@JsonProperty("readyTime") val readyTime: Long,
Expand All @@ -19,7 +19,7 @@ data class RedisMafiaGameOption @JsonCreator constructor(
@JsonProperty("endTime") val endTime: Long,
)

fun RedisMafiaGameOption.toGameOption(): MafiaGameOption = MafiaGameOption(
fun MafiaGameOptionRedisEntity.toDomain(): MafiaGameOption = MafiaGameOption(
minimum = this.minimum,
maximum = this.maximum,
readyTime = Duration.ofMillis(this.readyTime),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.xorker.draw.mafia.MafiaKeyword

data class RedisMafiaKeyword @JsonCreator constructor(
data class MafiaKeywordRedisEntity @JsonCreator constructor(
@JsonProperty("category") val category: String,
@JsonProperty("answer") val answer: String,
)

fun RedisMafiaKeyword.toMafiaKeyword(): MafiaKeyword = MafiaKeyword(
fun MafiaKeywordRedisEntity.toDomain(): MafiaKeyword = MafiaKeyword(
answer = this.answer,
category = this.category,
)
Loading

0 comments on commit 0fbee2e

Please sign in to comment.