Skip to content

Commit 449a668

Browse files
committed
refactor(auth): Replace requireNotNull with field validation utility
- Use checkField for refresh token validation - Remove IllegalArgumentException handler - Add Kotlin contracts to validation utilities - Bump version to 0.6.1
1 parent f30eaac commit 449a668

5 files changed

Lines changed: 41 additions & 14 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010

1111
group = "me.loghub"
1212

13-
version = "0.6.0-SNAPSHOT"
13+
version = "0.6.1-SNAPSHOT"
1414

1515
java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }
1616

src/main/kotlin/me/loghub/api/controller/auth/RefreshController.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import org.springframework.web.bind.annotation.RestController
1818
class RefreshController(private val refreshService: RefreshService) {
1919
@PostMapping
2020
fun refreshToken(
21-
@CookieValue(
22-
RefreshTokenConfig.NAME,
23-
required = false
24-
) refreshToken: String?
21+
@CookieValue(RefreshTokenConfig.NAME, required = false) refreshToken: String?
2522
): ResponseEntity<ResponseBody> {
2623
val token = refreshService.refreshToken(refreshToken)
2724
val responseBody = MessageResponseBody(

src/main/kotlin/me/loghub/api/handler/exception/ValidationExceptionHandler.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ class ValidationExceptionHandler {
2525
).toResponseEntity()
2626
}
2727

28-
@ExceptionHandler(IllegalArgumentException::class)
29-
fun handleException(e: IllegalArgumentException): ResponseEntity<ResponseBody> {
30-
return MessageResponseBody(
31-
message = e.message ?: ResponseMessage.Default.INVALID_REQUEST,
32-
status = HttpStatus.BAD_REQUEST
33-
).toResponseEntity()
34-
}
35-
3628
@ExceptionHandler(ConflictFieldException::class)
3729
fun handleException(e: ConflictFieldException): ResponseEntity<ResponseBody> {
3830
return FieldErrorsResponseBody(

src/main/kotlin/me/loghub/api/service/auth/RefreshService.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package me.loghub.api.service.auth
22

33
import jakarta.transaction.Transactional
4+
import me.loghub.api.config.RefreshTokenConfig
45
import me.loghub.api.constant.message.ResponseMessage
56
import me.loghub.api.dto.auth.token.TokenDTO
67
import me.loghub.api.exception.auth.BadRefreshTokenException
78
import me.loghub.api.lib.redis.key.RedisKeys
89
import me.loghub.api.repository.user.UserRepository
910
import me.loghub.api.service.auth.token.TokenService
11+
import me.loghub.api.util.checkField
1012
import org.springframework.data.redis.core.RedisTemplate
1113
import org.springframework.stereotype.Service
1214
import kotlin.time.Duration.Companion.seconds
@@ -20,7 +22,7 @@ class RefreshService(
2022
) {
2123
@Transactional
2224
fun refreshToken(token: String?): TokenDTO {
23-
requireNotNull(token) { ResponseMessage.Auth.INVALID_TOKEN }
25+
checkField(RefreshTokenConfig.NAME, token != null) { ResponseMessage.Auth.INVALID_TOKEN }
2426

2527
val redisKey = RedisKeys.REFRESH_TOKEN(token)
2628
val userId = redisTemplate.opsForValue().get(redisKey.key)

src/main/kotlin/me/loghub/api/util/ValidationUtil.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:OptIn(ExperimentalContracts::class)
2+
13
package me.loghub.api.util
24

35
import me.loghub.api.exception.auth.PermissionDeniedException
@@ -8,12 +10,18 @@ import me.loghub.api.exception.entity.EntityNotFoundFieldException
810
import me.loghub.api.exception.validation.ConflictFieldException
911
import me.loghub.api.exception.validation.CooldownNotElapsedException
1012
import me.loghub.api.exception.validation.IllegalFieldException
13+
import kotlin.contracts.ExperimentalContracts
14+
import kotlin.contracts.contract
1115

1216
inline fun checkField(
1317
field: String,
1418
condition: Boolean,
1519
lazyMessage: () -> String,
1620
) {
21+
contract {
22+
returns() implies condition
23+
}
24+
1725
if (!condition) {
1826
val message = lazyMessage()
1927
throw IllegalFieldException(field, message)
@@ -24,6 +32,10 @@ inline fun checkExists(
2432
condition: Boolean,
2533
lazyMessage: () -> String,
2634
) {
35+
contract {
36+
returns() implies condition
37+
}
38+
2739
if (!condition) {
2840
val message = lazyMessage()
2941
throw EntityNotFoundException(message)
@@ -35,6 +47,10 @@ inline fun checkExists(
3547
condition: Boolean,
3648
lazyMessage: () -> String,
3749
) {
50+
contract {
51+
returns() implies condition
52+
}
53+
3854
if (!condition) {
3955
val message = lazyMessage()
4056
throw EntityNotFoundFieldException(field, message)
@@ -45,6 +61,10 @@ inline fun checkPublished(
4561
condition: Boolean,
4662
lazyMessage: () -> String,
4763
) {
64+
contract {
65+
returns() implies condition
66+
}
67+
4868
if (!condition) {
4969
val message = lazyMessage()
5070
throw EntityNotFoundException(message)
@@ -55,6 +75,10 @@ inline fun checkConflict(
5575
condition: Boolean,
5676
lazyMessage: () -> String,
5777
) {
78+
contract {
79+
returns() implies !condition
80+
}
81+
5882
if (condition) {
5983
val message = lazyMessage()
6084
throw EntityConflictException(message)
@@ -66,6 +90,10 @@ inline fun checkConflict(
6690
condition: Boolean,
6791
lazyMessage: () -> String,
6892
) {
93+
contract {
94+
returns() implies !condition
95+
}
96+
6997
if (condition) {
7098
val message = lazyMessage()
7199
throw EntityExistsFieldException(field, message)
@@ -76,6 +104,10 @@ inline fun checkCooldown(
76104
condition: Boolean,
77105
lazyMessage: () -> String,
78106
) {
107+
contract {
108+
returns() implies !condition
109+
}
110+
79111
if (condition) {
80112
val message = lazyMessage()
81113
throw CooldownNotElapsedException(message)
@@ -86,6 +118,10 @@ inline fun checkPermission(
86118
condition: Boolean,
87119
lazyMessage: () -> String,
88120
) {
121+
contract {
122+
returns() implies condition
123+
}
124+
89125
if (!condition) {
90126
val message = lazyMessage()
91127
throw PermissionDeniedException(message)

0 commit comments

Comments
 (0)