Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…m-2-BE into sandbox
  • Loading branch information
comforest committed Jul 8, 2024
2 parents ab98cb6 + 4c6c657 commit 1e54966
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import org.springframework.stereotype.Component
internal class AccessTokenAdapter(
private val jwtProvider: JwtProvider,
private val jwtSecretKey: JwtSecretKey,
) : AccessTokenQueryRepository, AccessTokenCommandRepository {
override fun getUserIdOrThrow(accessToken: String): UserId {
return UserId(0L) // TODO 추후 메서드 필요 시 변경
}

) : AccessTokenRepository {
override fun createAccessToken(userId: UserId): String {
val now = LocalDateTime.now()
return jwtProvider.generate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal class RefreshTokenAdapter(
val token = ByteArray(TOKEN_BYTE_SIZE).apply {
random.nextBytes(this)
}
val expiredAt = LocalDateTime.now().plusMonths(3)
val expiredAt = LocalDateTime.now().plusYears(100) // TODO 소셜 & 익명 구분 필요

jpaRepository.save(
RefreshTokenJpaEntity.of(token, userId, expiredAt),
Expand Down
10 changes: 8 additions & 2 deletions adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ internal class UserAdapter(
override fun getUser(platform: AuthPlatform, platformUserId: String): User? =
authUserJpaRepository.find(platform, platformUserId)?.user?.toDomain()

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

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

override fun getUser(userId: UserId): User? =
userJpaRepository.findByIdOrNull(userId.value)?.toDomain()
override fun createUser(userName: String): User {
val user = UserJpaEntity.of(userName)
val savedUser = userJpaRepository.save(user)
return savedUser.toDomain()
}

@Transactional
override fun withdrawal(userId: UserId) {
Expand Down
18 changes: 11 additions & 7 deletions adapter/rdb/src/main/kotlin/com/xorker/draw/user/UserJpaEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ internal class UserJpaEntity : BaseJpaEntity() {
}

companion object {
internal fun of(id: Long): UserJpaEntity = UserJpaEntity().apply { this.id = id }
internal fun of(id: Long): UserJpaEntity =
UserJpaEntity().apply { this.id = id }

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

internal fun from(user: User): UserJpaEntity =
UserJpaEntity().apply {
this.id = user.id.value
this.name = user.name
}

internal fun of(
id: Long,
Expand All @@ -41,12 +51,6 @@ internal class UserJpaEntity : BaseJpaEntity() {
this.id = id
this.name = name
}

internal fun from(user: User): UserJpaEntity =
UserJpaEntity().apply {
this.id = user.id.value
this.name = user.name
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class AuthController(
return token.toResponse()
}

@Operation(summary = "익명 로그인 API")
@PostMapping("/api/v1/auth/anonymous-signin")
fun anonymousSignin(): AuthTokenResponse {
val token = authUserCase.anonymousSignIn()
return token.toResponse()
}

@PostMapping("/api/v1/auth/reissue")
fun reissue(@RequestBody request: AuthReissueRequest): AuthTokenResponse {
val token = authUserCase.reissue(request.refreshToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ internal class JwtService(
private val jwtProvider: JwtProvider,
private val jwtSecretKey: JwtSecretKey,
) : TokenUseCase {
override fun getUserId(token: String): UserId {
val subject = jwtProvider.validateAndGetSubject(token, jwtSecretKey) ?: throw JwtValidationFailException
override fun getUserId(token: String): UserId? {
val subject = jwtProvider.validateAndGetSubject(token, jwtSecretKey) ?: return null

return UserId(subject.toLong())
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.xorker.draw.support.auth.core
import com.xorker.draw.user.UserId

internal interface TokenUseCase {
fun getUserId(token: String): UserId
fun getUserId(token: String): UserId?
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.xorker.draw.support.auth.security

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

@EnableMethodSecurity
@EnableWebSecurity
@Configuration
internal class SecurityConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import org.springframework.web.filter.OncePerRequestFilter
internal class TokenAuthenticationFilter(
private val tokenUseCase: TokenUseCase,
) : OncePerRequestFilter() {

override fun doFilterInternal(
request: HttpServletRequest,
response: HttpServletResponse,
filterChain: FilterChain,
) {
val accessToken = getAccessToken(request)
if (accessToken != null) {
val userId = tokenUseCase.getUserId(accessToken)
setAuthentication(request, accessToken, userId)
tokenUseCase.getUserId(accessToken)?.let {
setAuthentication(request, accessToken, it)
}
}
filterChain.doFilter(request, response)
}
Expand Down
12 changes: 9 additions & 3 deletions core/src/main/kotlin/com/xorker/draw/auth/AuthService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xorker.draw.auth

import com.xorker.draw.auth.token.AccessTokenCommandRepository
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
Expand All @@ -13,7 +13,7 @@ import org.springframework.transaction.annotation.Transactional
internal class AuthService(
private val authRepository: AuthRepository,
private val userRepository: UserRepository,
private val accessTokenCommandRepository: AccessTokenCommandRepository,
private val accessTokenRepository: AccessTokenRepository,
private val refreshTokenRepository: RefreshTokenRepository,
) : AuthUseCase {
@Transactional
Expand All @@ -24,6 +24,12 @@ internal class AuthService(
return createToken(user.id)
}

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

return createToken(user.id)
}

override fun reissue(refreshToken: String): Token {
val userId = refreshTokenRepository.getUserIdOrThrow(refreshToken)

Expand All @@ -44,7 +50,7 @@ internal class AuthService(

private fun createToken(userId: UserId): Token {
return Token(
accessTokenCommandRepository.createAccessToken(userId),
accessTokenRepository.createAccessToken(userId),
refreshTokenRepository.createRefreshToken(userId),
)
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/com/xorker/draw/auth/AuthUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.xorker.draw.user.UserId
interface AuthUseCase {
fun signIn(authType: AuthType, token: String): Token

fun anonymousSignIn(): Token

fun reissue(refreshToken: String): Token

fun withdrawal(userId: UserId)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.xorker.draw.auth.token

import com.xorker.draw.user.UserId

interface AccessTokenCommandRepository {
interface AccessTokenRepository {
fun createAccessToken(userId: UserId): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import com.xorker.draw.auth.AuthPlatform
interface UserRepository {
fun getUser(platform: AuthPlatform, platformUserId: String): User?

fun getUser(userId: UserId): User?

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

fun getUser(userId: UserId): User?
fun createUser(userName: String): User

fun withdrawal(userId: UserId)
}

0 comments on commit 1e54966

Please sign in to comment.