```open class SimpleJWT(val secret: String) { ...
# ktor
a
Copy code
open class SimpleJWT(val secret: String) {
    private val algorithm = Algorithm.HMAC256(secret)
    val verifier = JWT.require(algorithm).build()
    fun sign(name: String): String = JWT.create().withClaim("name", name).sign(algorithm)
}
fun Application.module() {
    val simpleJwt = SimpleJWT("my-super-secret-for-jwt")
    install(Authentication) {
        jwt {
            verifier(simpleJwt.verifier)
            validate {
                UserIdPrincipal(it.payload.getClaim("name").asString())
            }
        }
    }
    // ...
}
p
The withClaim method allows you to add additional information to your JWT Token (see https://jwt.io/ for more info). The UserIdPrincipal will be a provided Principal to your ApplicationCalls if you're properly authenticated.
a
Can you please elaborate UserIdPrincipal?
p
See for example on https://ktor.io/samples/feature/auth.html, you can get info about the current logged user. For example there you can access the claims you’ve setted in the JWT token to identify your user/ do some operations based on role values for example
đź‘Ť 1
a
I'll take look at samples. Thanks alot!