This article will show you how to configure a Kotlin Ktor application to get access tokens from IdentityServer4 using OAuth 2.0. These tokens can then be used to access an API on behalf of a user. We’ll be using JWTs as our access tokens. To find out how to authorize access to a Ktor API using JWTs, check out my past article “JSON Web Token Verification in Ktor using Kotlin and Java-JWT”.
Ktor OAuth Support
Currently, Ktor only supports OAuth which means our Ktor application can receive access tokens to talk to an API on behalf of the user, but it cannot find out who the user is. If we wanted to find out who the user is and to receive identity tokens, we would need OpenID Connect, which is currently unsupported...Read more
In my previous article, we looked at how to get an access token and use it to access a protected resource, in Kotlin. Now we’re going to take a look at the other side of the story: how to validate an access token (in this case a structured JWT) before allowing access to the protected resource.
For token verification we’re going to:
- Get available public keys from a JWKS endpoint
- Parse the public key used to sign the receive JWT
- Verify the access token signature, issuer, and audience. This will also verify that the token hasn’t expired (the
expclaim), that it was issued in the past (the
iatclaim), and that the token is allowed to be used (the
We’ll then use this logic to protect an API endpoint running on Ktor.Read more
I’ve recently been picking up Kotlin and, since I work with authentication and authorization protocols on a daily basis, I used a basic OAuth scenario as my learning activity and thought I'd share my journey.
The scenario was to issue an OAuth request, parse the results, and then access a protected resource using the resulting token. This is not using any of the browser based grant types, instead just back end communication using the token endpoint and the client credentials grant type.
I’m not a Java developer, so this use of Kotlin has also been my first experience with that entire eco system. As a result...Read more