Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
[WEAV-000] 리프레쉬 토큰 만료 예외 변경 및 토큰 조회 메서드 리팩토링 (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
dojinyou authored May 1, 2024
1 parent 7792aa3 commit 7fd146c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface UserRefreshTokenRepository {
expirationSeconds: Long
)

fun findByUserId(userId: UUID): String?
fun existsByUserId(userId: UUID): Boolean

fun deleteByUserId(userId: UUID)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import com.studentcenter.weave.application.user.port.inbound.RefreshToken
import com.studentcenter.weave.application.user.port.outbound.UserRefreshTokenRepository
import com.studentcenter.weave.application.user.service.domain.UserDomainService
import com.studentcenter.weave.application.user.service.util.UserTokenService
import com.studentcenter.weave.application.user.vo.UserTokenClaims
import com.studentcenter.weave.domain.user.entity.User
import com.studentcenter.weave.support.security.jwt.exception.JwtException
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

@Service
class RefreshTokenService(
Expand All @@ -19,15 +20,16 @@ class RefreshTokenService(

@Transactional
override fun invoke(command: RefreshToken.Command): RefreshToken.Result {
val refreshToken: String = command.refreshToken
val refreshTokenUserId: UUID = userTokenService
.resolveRefreshToken(refreshToken)
.userId

val s = userRefreshTokenRepository.findByUserId(refreshTokenUserId)
validateRefreshTokenExists(s)

val user: User = userDomainService.getById(refreshTokenUserId)
// TODO(dojin): Result<T> 형태로 resolve쪽을 리팩토링하면서 try-catch 없애기
lateinit var refreshToken: UserTokenClaims.RefreshToken
try {
refreshToken = userTokenService.resolveRefreshToken(command.refreshToken)
} catch (e: JwtException.Expired) {
throw AuthException.RefreshTokenNotFound()
}
validateRefreshToken(refreshToken)

val user: User = userDomainService.getById(refreshToken.userId)
val accessToken: String = userTokenService.generateAccessToken(user)
val newRefreshToken: String = userTokenService.generateRefreshToken(user)

Expand All @@ -37,8 +39,10 @@ class RefreshTokenService(
)
}

private fun validateRefreshTokenExists(refreshToken: String?): String {
return refreshToken ?: throw AuthException.RefreshTokenNotFound()
private fun validateRefreshToken(refreshToken: UserTokenClaims.RefreshToken) {
if (userRefreshTokenRepository.existsByUserId(refreshToken.userId).not()) {
throw AuthException.RefreshTokenNotFound()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class LogoutTest : DescribeSpec({
sut.invoke()

// assert
userRefreshTokenRepositorySpy.findByUserId(user.id) shouldBe null
userRefreshTokenRepositorySpy.existsByUserId(user.id) shouldBe false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package com.studentcenter.weave.application.user.port.outbound
import java.util.*
import java.util.concurrent.ConcurrentHashMap

class UserRefreshTokenRepositorySpy :
com.studentcenter.weave.application.user.port.outbound.UserRefreshTokenRepository {
class UserRefreshTokenRepositorySpy : UserRefreshTokenRepository {

private val bucket = ConcurrentHashMap<UUID, String>()

Expand All @@ -16,8 +15,8 @@ class UserRefreshTokenRepositorySpy :
bucket[userId] = refreshToken
}

override fun findByUserId(userId: UUID): String? {
return bucket[userId]
override fun existsByUserId(userId: UUID): Boolean {
return bucket[userId] != null
}

override fun deleteByUserId(userId: UUID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ class UserRefreshTokenRedisAdapter(
userRefreshTokenRedisRepository.save(userRefreshTokenRedisHash)
}

override fun findByUserId(userId: UUID): String? {
return userRefreshTokenRedisRepository
.findById(userId)
.map { it.refreshToken }
.orElse(null)
override fun existsByUserId(userId: UUID): Boolean {
return userRefreshTokenRedisRepository.existsById(userId)
}

override fun deleteByUserId(userId: UUID) {
Expand Down

0 comments on commit 7fd146c

Please sign in to comment.