From 7fd146c5bea8f2a1960502060b540cb435f74950 Mon Sep 17 00:00:00 2001 From: dojin Date: Wed, 1 May 2024 21:34:27 +0900 Subject: [PATCH] =?UTF-8?q?[WEAV-000]=20=EB=A6=AC=ED=94=84=EB=A0=88?= =?UTF-8?q?=EC=89=AC=20=ED=86=A0=ED=81=B0=20=EB=A7=8C=EB=A3=8C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=ED=86=A0=ED=81=B0?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81=20(#260)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../outbound/UserRefreshTokenRepository.kt | 2 +- .../application/RefreshTokenService.kt | 28 +++++++++++-------- .../user/service/application/LogoutTest.kt | 2 +- .../outbound/UserRefreshTokenRepositorySpy.kt | 7 ++--- .../adapter/UserRefreshTokenRedisAdapter.kt | 7 ++--- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/application/src/main/kotlin/com/studentcenter/weave/application/user/port/outbound/UserRefreshTokenRepository.kt b/application/src/main/kotlin/com/studentcenter/weave/application/user/port/outbound/UserRefreshTokenRepository.kt index 12f2a3bb..d8708279 100644 --- a/application/src/main/kotlin/com/studentcenter/weave/application/user/port/outbound/UserRefreshTokenRepository.kt +++ b/application/src/main/kotlin/com/studentcenter/weave/application/user/port/outbound/UserRefreshTokenRepository.kt @@ -10,7 +10,7 @@ interface UserRefreshTokenRepository { expirationSeconds: Long ) - fun findByUserId(userId: UUID): String? + fun existsByUserId(userId: UUID): Boolean fun deleteByUserId(userId: UUID) diff --git a/application/src/main/kotlin/com/studentcenter/weave/application/user/service/application/RefreshTokenService.kt b/application/src/main/kotlin/com/studentcenter/weave/application/user/service/application/RefreshTokenService.kt index 3c0514d1..b5882a31 100644 --- a/application/src/main/kotlin/com/studentcenter/weave/application/user/service/application/RefreshTokenService.kt +++ b/application/src/main/kotlin/com/studentcenter/weave/application/user/service/application/RefreshTokenService.kt @@ -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( @@ -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 형태로 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) @@ -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() + } } } diff --git a/application/src/test/kotlin/com/studentcenter/weave/application/user/service/application/LogoutTest.kt b/application/src/test/kotlin/com/studentcenter/weave/application/user/service/application/LogoutTest.kt index b402142f..641dac45 100644 --- a/application/src/test/kotlin/com/studentcenter/weave/application/user/service/application/LogoutTest.kt +++ b/application/src/test/kotlin/com/studentcenter/weave/application/user/service/application/LogoutTest.kt @@ -37,7 +37,7 @@ class LogoutTest : DescribeSpec({ sut.invoke() // assert - userRefreshTokenRepositorySpy.findByUserId(user.id) shouldBe null + userRefreshTokenRepositorySpy.existsByUserId(user.id) shouldBe false } } diff --git a/application/src/testFixtures/kotlin/com/studentcenter/weave/application/user/port/outbound/UserRefreshTokenRepositorySpy.kt b/application/src/testFixtures/kotlin/com/studentcenter/weave/application/user/port/outbound/UserRefreshTokenRepositorySpy.kt index 4f29231d..6a846ab1 100644 --- a/application/src/testFixtures/kotlin/com/studentcenter/weave/application/user/port/outbound/UserRefreshTokenRepositorySpy.kt +++ b/application/src/testFixtures/kotlin/com/studentcenter/weave/application/user/port/outbound/UserRefreshTokenRepositorySpy.kt @@ -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() @@ -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) { diff --git a/infrastructure/redis/src/main/kotlin/com/studentcenter/weave/infrastructure/redis/user/adapter/UserRefreshTokenRedisAdapter.kt b/infrastructure/redis/src/main/kotlin/com/studentcenter/weave/infrastructure/redis/user/adapter/UserRefreshTokenRedisAdapter.kt index 280d6572..6ed751fe 100644 --- a/infrastructure/redis/src/main/kotlin/com/studentcenter/weave/infrastructure/redis/user/adapter/UserRefreshTokenRedisAdapter.kt +++ b/infrastructure/redis/src/main/kotlin/com/studentcenter/weave/infrastructure/redis/user/adapter/UserRefreshTokenRedisAdapter.kt @@ -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) {