From 3c280c53539a0fd3259c73cb1f75e3749999ecfe Mon Sep 17 00:00:00 2001 From: server_main Date: Mon, 17 Aug 2026 13:00:01 +0000 Subject: [PATCH] refactor: better jwt convert --- .../kotlin/club/itstudio/itmanager/utils/JwtUtils.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/backend/src/main/kotlin/club/itstudio/itmanager/utils/JwtUtils.kt b/src/backend/src/main/kotlin/club/itstudio/itmanager/utils/JwtUtils.kt index b2e3995..7e3b1cc 100644 --- a/src/backend/src/main/kotlin/club/itstudio/itmanager/utils/JwtUtils.kt +++ b/src/backend/src/main/kotlin/club/itstudio/itmanager/utils/JwtUtils.kt @@ -25,7 +25,7 @@ class JwtUtils( private val algorithm: Algorithm = Algorithm.HMAC256(key) private val jwtVerifier: JWTVerifier = JWT.require(algorithm).build() private val logger = LoggerFactory.getLogger(this::class.java) - + fun invalidateJwt(headerToken: String?): Boolean { val token = this.convertToToken(headerToken) ?: return false try { @@ -58,10 +58,10 @@ class JwtUtils( val decodedJWT = jwtVerifier.verify(token) if (this.isInvalidToken(decodedJWT.id)) return null val expiresAt = decodedJWT.expiresAt - + return if (Date().after(expiresAt)) null else decodedJWT - + } catch (e: Exception) { logger.error("Error occurred while resolving JWT", e) return null @@ -103,6 +103,8 @@ class JwtUtils( return cal.time } - fun convertToToken(headerToken: String?): String? = - headerToken?.replace("Bearer ", "") + fun convertToToken(headerToken: String?): String? { + val token = headerToken?.replace("Bearer ", "")?.trim() + return token?.takeIf { it.isNotEmpty() } + } } \ No newline at end of file