Before we jump on the approach of validating the JWT Token we need to know a few basics and then we can discuss about the below points.
- How we can sign the JWT Token.
- Why do we require a JWT Token to be signed.
- What is JSON Web Key Set ?
- Why and How to use JSON Web Key Set ?
- Locate JSON Web Key Set.
So, What is JSON Web Token?
JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object.-jwt.io
The above image shows how a JWT Content looks like and can be broken down to two main parts, one being the header and the other being the payload. We also need a secret to sign the token. These are separated by (.) dot.
Therefore, a JWT typically looks like the following.
Why Do we sign the TOken
So that the information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.
Although JWTs can be encrypted to also provide secrecy between parties, we will focus on signed tokens. Signed tokens can verify the integrity of the claims contained within it, while encrypted tokens hide those claims from other parties. When tokens are signed using public/private key pairs, the signature also certifies that only the party holding the private key is the one that signed it.
What is Jwks ?
The JSON Web Key Set (JWKS) is a set of keys containing the public keys used to verify any JSON Web Token (JWT) issued by the authorization server and signed using the RS256 signing algorithm.
Why and How to use JSON Web Key Set ?
We have been in the the world on Monolithic Architecture API from the past decades. So, Authentication and Claims Based Authorization was in this regard was easy in a monolithic. Now, we are heading to the more granular services which we call microservices.
In this previous era of Monolithic Architecture API we didn’t have any separation between the authentication/Authorization and other services.
Now, that we are moving towards microservices, each services has its own responsibility. But we also need to authenticate and authorize the user for each and every request. Now how do we do that?
If we create a symmetric key and use this to create a Token we would have to share this key to all the services that require to use the JWT token. But there are multiple draw back to this approach.
- We have to share the key with other services.
- IF a key from one service get compromised, the whole service ecosystem will be prone to be compromised.
- IF we need to change the secret to sign JWT we would have to update everywhere the token is being used.
How it works ?
When a user signs in to your application, we create a token that contains information about the user and sign the token using its private key before we send it back to your application. Auth0 secures the private key, which is unique per tenant.
To verify that the token is valid and originated from Identity Server, your application validates the token’s signature using the public key.
This specification defines two high-level data structures: JSON Web Key (JWK) and JSON Web Key Set (JWKS). Here are the definitions from the specification:
|JSON Web Key (JWK)||A JSON object that represents a cryptographic key. The members of the object represent properties of the key, including its value.|
|JSON Web Key Set (JWKS)||A JSON object that represents a set of JWKs. The JSON object MUST have a |
Here is an example of the JSON Web Key Set (JWKS) used by a sample tenant, containing a single JSON Web Key (JWK):
Each property in the key is defined by the JWK specification RFC 7517 Section 4 or, for algorithm-specific properties, in RFC 7518].
|The specific cryptographic algorithm used with the key.|
|The family of cryptographic algorithms used with the key.|
|How the key was meant to be used; |
|The x.509 certificate chain. The first entry in the array is the certificate to use for token verification; the other certificates can be used to verify this first certificate.|
|The modulus for the RSA public key.|
|The exponent for the RSA public key.|
|The unique identifier for the key.|
|The thumbprint of the x.509 cert (SHA-1 thumbprint).|
Locate JSON Web Key Sets
Use this Discovery endpoint to configure your application or API to automatically locate the JSON Web Key Set (JWKS) endpoint (
jwks_uri), which contains the JWKS used to sign all issued JSON Web Tokens (JWTs) signed. The endpoint exists at:
When validating a JWT using a JWKS, you will need to:
- Retrieve the JWKS from the well-known Discovery endpoint, and filter for potential signing keys (e.g., any keys missing a public key or with a
- Grab the
kidproperty from the Header of the decoded JWT.
- Search your filtered JWKS for the key with the matching
- Build a certificate using the corresponding
x5cproperty in your JWKS.
- Use the certificate to verify the JWT’s signature.