According to the documentation a signing key resolver can fetch a key dynamically: https://github.com/jwtk/jjwt#signing-key-resolver
The following code does call a kotlin suspending function which retrieves the public key in a non blocking way:
val parser =
Jwts.parserBuilder()
.setSigningKeyResolver(object : SigningKeyResolverAdapter() {
override fun resolveSigningKey(header: JwsHeader<out JwsHeader<*>>, claims: Claims?): Key {
return runBlocking {
retrievePublicKey(header["kid"])
}
}
}).build()
val claims = parser.parseClaimsJws(jwtString).body
Here is the definition of the suspending function
suspend fun retrievePublicKey(key:String):PublicKey {
...
}
The problem is that this code needs to block the thread(runBlocking
). Otherwise it can't work.
This challenge also exists for all other async frameworks (rxjava, listenablefuture, completablefuture, ...)