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 toPageImport from
Issue a token and check it when it comes backSign and verifyunjwt/jws
Sign one payload for several verifiersSign for several verifiersunjwt/jws
Send data only the recipient can readEncrypt and decryptunjwt/jwe
Encrypt one payload for several recipientsEncrypt for several recipientsunjwt/jwe
Create, import, export or convert keysKeysunjwt/jwk
Publish keys and rotate themUse a JWK Setunjwt/jwk
Keep a signed-in user in a cookieSessionsunjwt/session
Keep a session for a CLI or an app without cookiesSessions without cookiesunjwt/session
Run sessions on a framework with its own cookie APIWrite a driverunjwt/session
Bind an access token to the client's keyDPoP proofsunjwt/dpop
Check audience, issuer and token ageValidate claimsunjwt/jws, unjwt/jwe
Tell one failure from another in a catchHandle errorsunjwt/error
Encode base64url or check a value's shapeUtilitiesunjwt/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 JWTError with a code such as ERR_JWT_EXPIRED. Branch on the code, never on the message. The base64url helpers in unjwt/utils are the one exception: they throw an UnsecureError, also exported from unjwt/error.
  • Keys pick algorithms: a JWK names its own algorithm, so sign and encrypt read it from the key and verify and decrypt accept 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: verify and decrypt either 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: