Reason JWT decode


#1

Hi :wave:

is there already a library for Reason (Bucklescript) to handle JWTs?
I googled around and only found Ocaml libs (https://github.com/besport/ocaml-jwt).

What I really want to achieve is to decode the payload in the token (client side).
Is this even possible at all? Because the decoder wouldn’t know the types of the decoded payload, right?

Thank you for your help!
Torben


#2

Hey

I have recently developed a pretty comprehensive library for JOSE, which among other things includes JWT. Sadly (in this case) it’s for native so won’t work in bucklescript, but I have done insights.

If you just want to decode the payload you can just split the string on . and then take the middle part and base64url decode it. That will give you a JSON string that you can parse with bs-json.
If you want to validate the JWT I’m not sure how to do that in a JS environment and it also depends on what algorithm is used.


#3

Just a quick tip – JWTs actually use base64url, not base64. There are a couple of slight differences which caused me big problems at work last week! Using atob for JWT payloads is unsafe.