Hello. I managed to set up JWT authentication in k...
# ktor
p
Hello. I managed to set up JWT authentication in ktor. It works. However I dont see how to get the claims containd in the token when processing a request. How can I get them?
p
If I'm not mistaken, you can get them inside your call by accessing the Principal property
p
That's what I did before replacing basic auth with JWT. I got it using
Copy code
val principal: UserIdPrincipal? = call.authentication.principal<UserIdPrincipal>()
But with JWT, call.authentication.principal is null
I found the solution. I have to get the claim like this:
Copy code
val principal: JWTPrincipal? = call.authentication.principal<JWTPrincipal>()
return principal!!.payload.getClaim("my_claim").asString()
p
Glad that you've found it, on a test I wrote i'm able to get it directly from the ApplicationCall.principal, but I don't know how your validate lambda is implemented
p
I’m interested in your validate lambda, @Pedro Flores. Could you post it please?
Mine looks like this:
Copy code
fun validateCredential(jwtCredential: JWTCredential): Principal? {

    val sessionId = jwtCredential.payload.getClaim("sessionId").asString()

    if (isValidSessionId(cuid, sessionId)) {
        return JWTPrincipal(jwtCredential.payload)
    }

    return null
}
p
Well it’s quite similar, the difference is that I have an extended Principal but still it should be working as the documentation intended :)
p
That’s the point I missed. It’s much simpler with a class extending Principal. Thanks @Pedro Flores! 👍