Skip to content

Commit

Permalink
#146 Test invalid JWT token
Browse files Browse the repository at this point in the history
  • Loading branch information
vityaman committed Jun 17, 2024
1 parent 75394ea commit b8fdbe5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package ru.vityaman.lms.botalka.core.security.auth

import io.jsonwebtoken.ExpiredJwtException
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.MalformedJwtException
import io.jsonwebtoken.io.Decoders
import io.jsonwebtoken.security.Keys
import ru.vityaman.lms.botalka.core.exception.AuthenticationException
import ru.vityaman.lms.botalka.core.model.User
import java.time.Clock
import java.util.*
import java.util.Date
import javax.crypto.SecretKey
import kotlin.time.toJavaDuration
import java.time.Duration as JavaDuration
Expand Down Expand Up @@ -50,6 +51,8 @@ class JwtTokenService(
AccessToken.Payload(userId)
} catch (e: ExpiredJwtException) {
throw AuthenticationException(e.message!!, e)
} catch (e: MalformedJwtException) {
throw AuthenticationException(e.message!!, e)
}

data class Config(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ class JwtTokenServiceTest {
assertThrows<AuthenticationException> { tokens.decode(token) }
}
}

@Test
fun invalidToken(): Unit = runBlocking {
assertThrows<AuthenticationException> {
tokens.decode(AccessToken("invalid-token"))
}
}
}

0 comments on commit b8fdbe5

Please sign in to comment.