Sign and verify a JWT with Azion Lib
Sign a JSON Web Token with a shared secret, verify it, and reject a token with the wrong signature or a past expiration, with the @aziontech/jwt package.
You can sign a JSON Web Token (JWT) with a shared secret and verify it later, with the @aziontech/jwt package of Azion Lib. The examples run in Node.js, sign with the default HS256 algorithm, and need no Azion account or personal token. To sign with a key pair instead, refer to Keys and algorithms.
Prerequisites
- Node.js and npm. The script is an ES module that uses top-level
await. - A secret to sign the tokens with, kept out of your code. The examples read it from the
JWT_SECRETenvironment variable.
Install the package
The script imports @aziontech/jwt from the node_modules directory of its project.
In an empty directory, create the project and install the package:
npm prints the new package.json, and the install ends with:
The project has @aziontech/jwt in its package.json dependencies.
Write the token script
The script runs one of three functions of the package, named by its first argument:
- With
sign, the script callssign. Thesubclaim is the value you pass. The third argument setsexpin whole seconds from now,3600by default. - With
verify, the script callsverify, which checks the signature and the time claims and returns the payload. - With
decode, the script callsdecode, which reads the claims without checking the signature.
verify rejects an invalid token with an error. The script prints the error’s name, such as JwtTokenExpired, because the package does not export the error classes. For every error name, refer to Errors.
Create a token.mjs file:
Without JWT_SECRET, the script prints Set the JWT_SECRET variable first and exits with code 1. The check exists because sign, called with no secret, fails with a TypeError instead of a named error.
Sign a token
The script reads the secret from JWT_SECRET, so the secret stays out of the script’s code.
To sign a token:
-
Store your secret in the
JWT_SECRETvariable. Replace[TOKEN VALUE]with your secret:Every script you start from this terminal reads the secret, until the terminal closes.
-
Sign a token for the subject
user-42that expires in one hour, and store it in theTOKENvariable:Shell
TOKEN holds a signed token. Its characters differ from the example, because the payload carries the current time and your secret signs it.
Verify a token
verify returns the payload only when the signature matches the secret and no time claim fails. An exp in the past, or an nbf or iat in the future, rejects the token. A token with no exp claim never expires, so the script always sets one.
To verify a token with the secret that signed it:
-
Store your secret in the
JWT_SECRETvariable. Replace[TOKEN VALUE]with your secret: -
Sign a token for the subject
user-42, and store it in theTOKENvariable:TOKENholds a token that your secret signed. -
Verify the token:
The token is valid, and the script prints its claims. Times are Unix timestamps, in seconds.
Reject an invalid token
Two tokens fail the check: one that another secret signed, and one whose exp claim is in the past. In both cases the script prints invalid with the error name and exits with code 1.
To see both rejections:
-
Store your secret in the
JWT_SECRETvariable. Replace[TOKEN VALUE]with your secret: -
Sign a token for the subject
user-42, and store it in theTOKENvariable:TOKENholds a token that your secret signed. -
Verify the token with a different secret:
-
Sign a token that expired 60 seconds ago, and store it in the
EXPIREDvariable:EXPIREDholds a token whoseexpclaim is in the past. -
Verify the expired token:
The script rejects a token that another secret signed and a token that has expired.
Read the claims without verifying
decode reads the header and the claims of any token, including an expired one. It checks neither the signature nor the time claims. Use it to inspect a token, never to trust one.
To read the claims of an expired token:
-
Store your secret in the
JWT_SECRETvariable. Replace[TOKEN VALUE]with your secret: -
Sign a token that expired 60 seconds ago, and store it in the
EXPIREDvariable:EXPIREDholds a token whoseexpclaim is in the past. -
Decode the token:
The output shows the algorithm that the token header names, and an exp earlier than iat.