Introduction
unjwt signs, verifies, encrypts and decrypts JSON Web Tokens, manages the keys behind them, and keeps sessions in a cookie, on the Web Crypto API with no dependencies.
unjwt is a TypeScript library for JSON Web Tokens. It signs tokens so you can trust them when they come back (JWS), encrypts them so nobody else can read them (JWE), and generates, imports and rotates the keys both need (JWK). On top of that it keeps a user's session in a cookie or a header, and creates and checks DPoP proofs that bind a token to the client holding it.
Everything runs on the Web Crypto API, so the same code works in Node.js, Bun, Deno, Cloudflare Workers and browsers, and the package pulls in no other dependencies. If "signed" and "encrypted" still blur together for you, start with Concepts.
#What you can do with it
Find the job in front of you in the first column, and the row tells you where to go:
| You want to | Page | Import from |
|---|---|---|
| Issue a token and check it when it comes back | Sign and verify | unjwt/jws |
| Sign one payload for several verifiers | Sign for several verifiers | unjwt/jws |
| Send data only the recipient can read | Encrypt and decrypt | unjwt/jwe |
| Encrypt one payload for several recipients | Encrypt for several recipients | unjwt/jwe |
| Create, import, export or convert keys | Keys | unjwt/jwk |
| Publish keys and rotate them | Use a JWK Set | unjwt/jwk |
| Keep a signed-in user in a cookie | Sessions | unjwt/session |
| Keep a session for a CLI or an app without cookies | Sessions without cookies | unjwt/session |
| Run sessions on a framework with its own cookie API | Write a driver | unjwt/session |
| Bind an access token to the client's key | DPoP proofs | unjwt/dpop |
| Check audience, issuer and token age | Validate claims | unjwt/jws, unjwt/jwe |
Tell one failure from another in a catch | Handle errors | unjwt/error |
| Encode base64url or check a value's shape | Utilities | unjwt/utils |
#The rules that hold everywhere
A few rules run through every module, so you can count on them on every page:
- One error class: whatever goes wrong, a bad token or a mistake in your setup, you get a
JWTErrorwith acodesuch asERR_JWT_EXPIRED. Branch on the code, never on the message. The base64url helpers inunjwt/utilsare the one exception: they throw anUnsecureError, also exported fromunjwt/error. - Keys pick algorithms: a JWK names its own algorithm, so
signandencryptread it from the key andverifyanddecryptaccept only that one. A key that names none needs the algorithm spelled out. - Limits fail closed: every call that reads a token bounds its work, such as how many signatures it checks or keys it tries. Past a limit the call refuses the token, it never quietly checks less. Limits lists them.
- A bad token throws:
verifyanddecrypteither return the whole result or throw. You never get a payload whose signature or claims weren't checked.
#What it doesn't do
unjwt works on tokens and keys and leaves the rest of the app to you. It has no HTTP client or server, so fetching a remote JWK Set or answering a request is your code. It stores nothing: revocation lists, replay records and refresh-token tables live in your database. It isn't an OAuth or OpenID Connect server, though it signs and checks the tokens one issues. And it doesn't hash passwords; unsecure does that, and it is meant to be used alongside this library.
#Where to go next
If you're new here, the first three pages in order take you to working code:
Concepts
Tokens, keys, claims and sessions, explained once for every other page.
Install unjwt
Requirements, the install command, and every import path.
Quickstart
Four short tasks: an API token, an encrypted payload, a cookie session and a failed token.
Migration guides
What changed between releases and the edits each one asks for.