Hi! I need to fetch the raw JWT accessToken from t...
# ktor
h
Hi! I need to fetch the raw JWT accessToken from the current
call
in my Ktor server. Anyone know how I can do that? I got the
Authentication
installed and
jwt()
setup, so I can wrap my endpoint with
authenticate { }
, but now I need to take the accessToken and pass it on to a different service (not HTTP).
a
You can decode the JWT with the following code:
Copy code
get {
    val jwt = JWT
        .require(Algorithm.HMAC256("secret"))
        .withAudience("audience")
        .withIssuer("issuer")
        .build()
    val auth = call.request.headers[HttpHeaders.Authorization] ?: return@get
    val authHeader = parseAuthorizationHeader(auth)
    if (authHeader is HttpAuthHeader.Single) {
        val decodedJWT = jwt.verify(authHeader.blob)
    }
}
h
Ok, I want the raw accessToken. Not the decoded one. I can ge that from
call.principal()
t
I just ended up implementing it by myself.
Copy code
install(Authentication) {
  bearer {
    authenticate { (token) ->
      // Parse the JWT and return something
    }
}
h
Yes, but that is not inside a call.
a
Can you share the code of your current implementation which obtains the access token?
h
Basically this:
Copy code
fun Headers.getAccessToken() = this["Authorization"]?.removePrefix("Bearer ")
It works, and I'm fine if this is what I have to use. The reason I need this is to propagate the JWT when I call other services.