A JWT (JSON Web Token) is composed of three parts:
To validate a JWT issued by AWS Cognito, you first need to download the JSON Web Key Set (JWKS), which contains the public keys used to verify the token’s signature. You can obtain the JWKS from the following URL:
To simplify the process of verifying a JWT, you can use the jwx
library, which handles the token parsing and validation.
A JWK cache helps you avoid repeatedly downloading the keys for each token validation. Here’s how to refresh the cache with the public keys:
Once you have the JWKS, you can parse the token using the cached keys. This ensures the token’s signature is valid:
After parsing the token, you need to verify the claims it contains. Start by checking the client_id claim to ensure the token was issued for the correct client:
Next, confirm the iss (issuer) claim to ensure the token was issued by your Cognito user pool:
You can securely validate JWT tokens issued by AWS Cognito using the jwx library. This ensures the token’s signature and claims are valid before granting access to your protected resources.