22 lines
767 B
Kotlin
22 lines
767 B
Kotlin
package be.simplenotes.domain.security
|
|
|
|
import be.simplenotes.domain.model.PersistedUser
|
|
import com.auth0.jwt.exceptions.JWTVerificationException
|
|
|
|
data class JwtPayload(val userId: Int, val username: String) {
|
|
constructor(user: PersistedUser) : this(user.id, user.username)
|
|
}
|
|
|
|
class JwtPayloadExtractor(private val jwt: SimpleJwt) {
|
|
operator fun invoke(token: String): JwtPayload? = try {
|
|
val decodedJWT = jwt.verifier.verify(token)
|
|
val id = decodedJWT.getClaim("id").asInt() ?: null
|
|
val username = decodedJWT.getClaim("username").asString() ?: null
|
|
id?.let { username?.let { JwtPayload(id, username) } }
|
|
} catch (e: JWTVerificationException) {
|
|
null
|
|
} catch (e: IllegalArgumentException) {
|
|
null
|
|
}
|
|
}
|