Decode an OAuth Token with F#

October 9, 2024    Development .Net Functional Programming F#

Decode an OAuth Token with F#

I usually go to Jwt.io to decode OAuth tokens, but it’s not private. I’m sure they don’t, but what if they are recording the entries?

So I decided to look into how to run it locally with F#.

Thanks to the Bing Chat AI , I quickly got the code and ran it.

In VS Code, with the Ionide extension you can quickly run FSharp code in a .fsx file.

Here’s the code from Bing Chat. I only had to change from the dotnet add package suggestion to using the inline #r "nuget: System.IdentityModel.Tokens.Jwt"


#r "nuget: System.IdentityModel.Tokens.Jwt"
open System.IdentityModel.Tokens.Jwt

let decodeJwtToken (token: string) =
    let handler = JwtSecurityTokenHandler()
    let jsonToken = handler.ReadToken(token) :?> JwtSecurityToken
    jsonToken.Claims
    |> Seq.map (fun claim -> claim.Type, claim.Value)
    |> Seq.toList

// Example usage
let token = "your_token_here"
let claims = decodeJwtToken token
claims |> List.iter (fun (typ, value) -> printfn "%s: %s" typ value)

Bing referenced https://fsharpforfunandprofit.com/posts/capability-based-security-3/ , https://github.com/Gab-km/OAuth-in-FSharp and https://blog.bitscry.com/2018/03/05/retrieving-oauth2-tokens-in-net/

I saw this just after writing the above and am impressed. You can decode the JWT in the Visual Studio Previewer!

VS Previewer



Watch the Story for Good News
I gladly accept BTC Lightning Network tips at [email protected]

Please consider using Brave and adding me to your BAT payment ledger. Then you won't have to see ads! (when I get to $100 in Google Ads for a payout (I'm at $95.73!), I pledge to turn off ads)

Use Brave

Also check out my Resources Page for referrals that would help me.


Swan logo
Use Swan Bitcoin to onramp with low fees and automatic daily cost averaging and get $10 in BTC when you sign up.