Polyglot programmer, open source enthusiast and published author. Based in Huddersfield, UK. She/her
So many modern web applications, both client-side and server-side, use JSON Web Tokens (JWTs) for authentication, and this is an excellent approach. However when things don't work, it can be tricky to work out why. This post aims to give you some tactics for understanding and correcting problems with JWTs. If you're just getting started, check out the documentation on working with JWTs and our APIs first.
Sometimes the problem is as simple as knowing whether you even passed the right value into the right place, the equivalent to "is it plugged in?" question. Add a little debugging to your code to output the JWT somewhere you can see it, such as into your error log.
Then take a look for the following:
If the token passes visual inspection, then we need to get out some more specific tools.
There is an excellent JWT debugging tool (thanks, Auth0!) that can help us to understand when things are not what we were aiming for.
Paste your JWT into the left hand pane, and if it can be parsed, the details of the three sections will be shown on the right hand side.
The first section is the header, showing the type and algorithm used. For signing Vonage API calls, this will usually be
(the JWTs on the Message API signed webhooks are
, just in case that's useful information).
The middle section contains most of the actual data. There are a few expected fields here for Vonage API calls with JWTs:
stands for "issued at" and should be a unix timestamp
is the "expiration time" and is also a unix timestamp
stands for "JWT ID", and should be a unique identifier (format not specified)
is required for Vonage API calls, and it must match the Private Key used to sign the token.
You may also see a
field (the Client SDKs use this) or something called
which is the timestamp that this token is "Not Before", meaning the token isn't valid until that moment.
The third and final section in the jwt.io debugger is the signature. JWTs are created with a private key, but the key isn't included in the payload. The private key is essentially a shared secret between you and Vonage. You can check that the signature checks out by adding your private key into the web interface in this section.
Sometimes the problem we think is the token is actually something completely different! Here are some tactics to try when the first two steps have not helped.
Creating a new application, generating new keys, making sure you have the correct file named
... these are all steps that really shouldn't make a difference but sometimes are all that is needed. It's my "one weird trick" for JWT problems, and perhaps it will help you too?
Try generating a token and then using it either in your application or in a raw API call from your favourite HTTP client.
You can generate a JWT from the Nexmo CLI tool, using your application ID and Private Key, like this:
nexmo jwt:generate path/to/private.key application_id=asdasdas-asdd-2344-2344-asdasdasd345
Alternatively, we have a web-based helper on our Developer Portal that you can use to generate a JWT: https://developer.nexmo.com/jwt.
Fault finding is a skillset all of its own, and hopefully there was something in this post that helps you move forward building something awesome. If you have more tips to share, let us know! We're @VonageDev on Twitter.
Previously published at https://www.nexmo.com/blog/2020/08/26/how-to-debug-json-web-tokens-jwts
Create your free account to unlock your custom reading experience.