Hi all. I'm trying to use ktor with jwt and I'm ge...
# ktor
r
Hi all. I'm trying to use ktor with jwt and I'm getting the following error:
Copy code
2023-04-09 11:51:45.134 [eventLoopGroupProxy-4-1] TRACE io.ktor.auth.jwt - Failed to get JWK
com.auth0.jwk.SigningKeyNotFoundException: Failed to get key with kid null
Caused by: java.util.concurrent.ExecutionException: com.auth0.jwk.NetworkException: Cannot obtain jwks from url <https://localhost:8080/.well-known/jwks.json>
2023-04-09 12:52:13.754 [eventLoopGroupProxy-4-1] TRACE io.ktor.server.auth.Authentication - Trying to authenticate /api/erp/access/test with null
2023-04-09 12:52:13.803 [eventLoopGroupProxy-4-1] TRACE io.ktor.server.auth.Authentication - Authentication failed for /api/erp/access/test with provider io.ktor.server.auth.jwt.JWTAuthenticationProvider@e62069e
2023-04-09 12:52:13.804 [eventLoopGroupProxy-4-1] TRACE i.k.s.p.c.ContentNegotiation - Skipping because body is already converted.
My config.
Copy code
fun Application.configureSecurity() {
  authentication {
      val issuer = "issuer"
      val jwkProvider = JwkProviderBuilder(issuer)
        .cached(10, 24, TimeUnit.HOURS)
        .rateLimited(10, 1, TimeUnit.MINUTES)
        .build()
      val jwtAudience = "audience"
      realm = "realm"
      verifier(jwkProvider, issuer) {
        acceptLeeway(3)
      }
      validate { credential ->
        if (credential.payload.audience.contains(jwtAudience)) JWTPrincipal(credential.payload) else null
      }
    }
}
Copy code
fun generateJwt(user: User, roles: List<Role>): Result<String> = runCatching {
  val publicKey = readPublicKey("path to public key")
  val privateKey = readPrivateKey("path to private key",  "password")
  JWT.create()
    .withAudience("audience")
    .withIssuer("issuer")
    .withExpiresAt(Date(System.currentTimeMillis() + 7200000))
    .withClaim("username", user.username)
    .withClaim("email", user.email)
    .withClaim("company", user.company)
    .withArrayClaim("roles", roles.map { it.type.value }.toTypedArray())
    .sign(Algorithm.RSA512(publicKey, privateKey))
}.onFailure {
  Log.debug()
  throw it
}
Copy code
authenticate {
  get("/test") {
   // return OK
 }
}
a
Seems like no jwks provided
Copy code
com.auth0.jwk.NetworkException: Cannot obtain jwks from url <https://localhost:8080/.well-known/jwks.json>
r
this had never happened before. How to fix?
@Andrey Tabakov I`m getting this error Caused by: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message my realm http://localhost:8080/
278 Views