From 91edc7bb2929000bfdf9c692a592824fa6ea935d Mon Sep 17 00:00:00 2001 From: pricelees Date: Wed, 23 Jul 2025 11:31:21 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20JwtHandler=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EB=8A=94=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=ED=83=80=EC=9E=85=EC=9D=84=20=EC=83=88=EB=A1=9C=20=EC=A0=95?= =?UTF-8?q?=EC=9D=98=ED=95=9C=20AuthException=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/infrastructure/jwt/JwtHandler.kt | 22 ++++++++----------- .../auth/infrastructure/jwt/JwtHandlerTest.kt | 16 +++++++------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/roomescape/auth/infrastructure/jwt/JwtHandler.kt b/src/main/kotlin/roomescape/auth/infrastructure/jwt/JwtHandler.kt index feaabcda..4437021b 100644 --- a/src/main/kotlin/roomescape/auth/infrastructure/jwt/JwtHandler.kt +++ b/src/main/kotlin/roomescape/auth/infrastructure/jwt/JwtHandler.kt @@ -1,11 +1,12 @@ package roomescape.auth.infrastructure.jwt -import io.jsonwebtoken.* +import io.jsonwebtoken.ExpiredJwtException +import io.jsonwebtoken.Jwts +import io.jsonwebtoken.SignatureAlgorithm import org.springframework.beans.factory.annotation.Value -import org.springframework.http.HttpStatus import org.springframework.stereotype.Component -import roomescape.common.exception.ErrorType -import roomescape.common.exception.RoomescapeException +import roomescape.auth.exception.AuthErrorCode +import roomescape.auth.exception.AuthException import java.util.* @Component @@ -36,15 +37,10 @@ class JwtHandler( .body .get(MEMBER_ID_CLAIM_KEY, Number::class.java) .toLong() - } catch (e: Exception) { - when (e) { - is ExpiredJwtException -> throw RoomescapeException(ErrorType.EXPIRED_TOKEN, HttpStatus.UNAUTHORIZED) - is UnsupportedJwtException -> throw RoomescapeException(ErrorType.UNSUPPORTED_TOKEN, HttpStatus.UNAUTHORIZED) - is MalformedJwtException -> throw RoomescapeException(ErrorType.MALFORMED_TOKEN, HttpStatus.UNAUTHORIZED) - is SignatureException -> throw RoomescapeException(ErrorType.INVALID_SIGNATURE_TOKEN, HttpStatus.UNAUTHORIZED) - is IllegalArgumentException -> throw RoomescapeException(ErrorType.INVALID_TOKEN, HttpStatus.UNAUTHORIZED) - else -> throw RoomescapeException(ErrorType.UNEXPECTED_ERROR, HttpStatus.INTERNAL_SERVER_ERROR) - } + } catch (_: ExpiredJwtException) { + throw AuthException(AuthErrorCode.EXPIRED_TOKEN) + } catch (_: Exception) { + throw AuthException(AuthErrorCode.INVALID_TOKEN) } } diff --git a/src/test/kotlin/roomescape/auth/infrastructure/jwt/JwtHandlerTest.kt b/src/test/kotlin/roomescape/auth/infrastructure/jwt/JwtHandlerTest.kt index 53f12279..91b9f376 100644 --- a/src/test/kotlin/roomescape/auth/infrastructure/jwt/JwtHandlerTest.kt +++ b/src/test/kotlin/roomescape/auth/infrastructure/jwt/JwtHandlerTest.kt @@ -5,8 +5,8 @@ import io.jsonwebtoken.SignatureAlgorithm import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe -import roomescape.common.exception.ErrorType -import roomescape.common.exception.RoomescapeException +import roomescape.auth.exception.AuthErrorCode +import roomescape.auth.exception.AuthException import roomescape.util.JwtFixture import java.util.* import kotlin.random.Random @@ -33,15 +33,15 @@ class JwtHandlerTest : FunSpec({ Thread.sleep(expirationTime) // 만료 시간 이후로 대기 // when & then - shouldThrow { + shouldThrow { shortExpirationTimeJwtHandler.getMemberIdFromToken(token) - }.errorType shouldBe ErrorType.EXPIRED_TOKEN + }.errorCode shouldBe AuthErrorCode.EXPIRED_TOKEN } test("토큰이 빈 값이면 예외를 던진다.") { - shouldThrow { + shouldThrow { jwtHandler.getMemberIdFromToken("") - }.errorType shouldBe ErrorType.INVALID_TOKEN + }.errorCode shouldBe AuthErrorCode.INVALID_TOKEN } test("시크릿 키가 잘못된 경우 예외를 던진다.") { @@ -53,9 +53,9 @@ class JwtHandlerTest : FunSpec({ .signWith(SignatureAlgorithm.HS256, JwtFixture.SECRET_KEY.substring(1).toByteArray()) .compact() - shouldThrow { + shouldThrow { jwtHandler.getMemberIdFromToken(invalidSignatureToken) - }.errorType shouldBe ErrorType.INVALID_SIGNATURE_TOKEN + }.errorCode shouldBe AuthErrorCode.INVALID_TOKEN } } })