Skip to content

Commit

Permalink
fix: 토큰만 사용 되는 API 요청에 대한 로그아웃 블랙리스트 검증 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
TaeyeonRoyce committed Mar 31, 2024
1 parent 45c86b7 commit eea6e24
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
29 changes: 26 additions & 3 deletions src/main/kotlin/com/petqua/presentation/auth/AuthExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ class AuthExtractor(
memberRepository.existActiveByIdOrThrow(accessTokenClaims.memberId) {
MemberException(NOT_FOUND_MEMBER)
}
throwExceptionWhen(blackListTokenCacheStorage.isBlackListed(accessTokenClaims.memberId, token)) {
AuthException(UNABLE_ACCESS_TOKEN)
}
validateBlackListed(accessTokenClaims.memberId, token)
return accessTokenClaims
} catch (e: ExpiredJwtException) {
throw AuthException(EXPIRED_ACCESS_TOKEN)
Expand All @@ -73,4 +71,29 @@ class AuthExtractor(
throw AuthException(INVALID_ACCESS_TOKEN)
}
}

private fun validateBlackListed(memberId: Long, token: String) {
throwExceptionWhen(blackListTokenCacheStorage.isBlackListed(memberId, token)) {
AuthException(UNABLE_ACCESS_TOKEN)
}
}

fun validateBlacklistTokenRegardlessExpiration(token: String) {
val accessTokenClaims = getAccessTokenClaimsRegardlessExpiration(token)
validateBlackListed(accessTokenClaims.memberId, token)
}

private fun getAccessTokenClaimsRegardlessExpiration(token: String): AccessTokenClaims {
return try {
AccessTokenClaims.from(jwtProvider.getPayload(token))
} catch (e: ExpiredJwtException) {
val payload = mutableMapOf<String, String>()
e.claims.forEach { payload[it.key] = it.value.toString() }
AccessTokenClaims.from(payload)
} catch (e: JwtException) {
throw AuthException(INVALID_ACCESS_TOKEN)
} catch (e: NullPointerException) {
throw AuthException(INVALID_ACCESS_TOKEN)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TokenArgumentResolver(
): AuthToken {
val request = webRequest.getHttpServletRequestOrThrow()
val accessToken = authExtractor.extractAccessToken(request)
authExtractor.validateBlacklistTokenRegardlessExpiration(accessToken)
val refreshToken = authExtractor.extractRefreshToken(request)
return AuthToken(accessToken, refreshToken)
}
Expand Down
44 changes: 33 additions & 11 deletions src/test/kotlin/com/petqua/presentation/auth/AuthControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,43 @@ class AuthControllerTest(
}

Given("로그아웃 된 인증정보로") {
val member = memberRepository.save(member())
val createAuthToken = authTokenProvider.createAuthToken(member, Date())
val accessToken = createAuthToken.accessToken
val refreshToken = createAuthToken.refreshToken
refreshTokenRepository.save(
RefreshToken(
memberId = member.id,
token = refreshToken
When("인증이 필요한 요청에 사용하는 경우") {
val member = memberRepository.save(member())
val createAuthToken = authTokenProvider.createAuthToken(member, Date())
val accessToken = createAuthToken.accessToken
val refreshToken = createAuthToken.refreshToken
refreshTokenRepository.save(
RefreshToken(
memberId = member.id,
token = refreshToken
)
)
)
requestSignOut(accessToken, refreshToken)
requestSignOut(accessToken, refreshToken)

When("인증이 필요한 요청에 사용하는 경우") {
val response = requestDeleteMember(accessToken)
Then("사용 할 수 없다") {
val errorResponse = response.`as`(ExceptionResponse::class.java)
assertSoftly(response) {
statusCode shouldBe UNAUTHORIZED.value()
errorResponse.message shouldBe UNABLE_ACCESS_TOKEN.errorMessage()
}.statusCode shouldBe 401
}
}

When("로그인 연장 요청시에 사용하는 경우") {
val member = memberRepository.save(member())
val createAuthToken = authTokenProvider.createAuthToken(member, Date())
val accessToken = createAuthToken.accessToken
val refreshToken = createAuthToken.refreshToken
refreshTokenRepository.save(
RefreshToken(
memberId = member.id,
token = refreshToken
)
)
requestSignOut(accessToken, refreshToken)

val response = requestExtendLogin(accessToken, refreshToken)
Then("사용 할 수 없다") {
val errorResponse = response.`as`(ExceptionResponse::class.java)
assertSoftly(response) {
Expand Down

0 comments on commit eea6e24

Please sign in to comment.