unjwt
JSON Web Tokens on the Web Crypto API
Sign, verify, encrypt and decrypt JSON Web Tokens, manage their keys, and keep sessions in a cookie on any Web standard runtime. JWS, JWE, JWK and DPoP in one zero-dependency package, with one typed error.
// Sign with the private half, verify with the public one
import { sign, verify } from "unjwt/jws";
import { generateJWK } from "unjwt/jwk";
const { privateKey, publicKey } = await generateJWK("ES256");
const token = await sign({ sub: "user_1a2b" }, privateKey, { expiresIn: "15m" });
const { payload } = await verify(token, publicKey);
// Keep a session in a cookie, on any Web Request
import { defineJWESession } from "unjwt/session";
const appSession = defineJWESession({ key: await generateJWK("A256GCM"), maxAge: "7D" });
const session = await appSession.use({ req: request, res: { headers } });
await session.update({ user: "user_1a2b" });
Features
Web Crypto, zero dependencies
Built on the Web Crypto API. No runtime dependencies, no Node built-ins: the same code runs in Node.js, Bun, Deno, Workers and browsers.
Keys pick the algorithm
Pass a JWK and the algorithm comes from the key, on both sides. Key pairs, JWK Sets with rotation, PEM import and thumbprints as key IDs.
Sessions on any Web runtime
Signed or encrypted cookie sessions over a Web Request: h3, Nitro, Nuxt, Hono, Workers, Bun and Deno, and a driver for any other framework.
One error, one code
Every failure, a bad token or a setup mistake, throws a JWTError with a code you can branch on. Nothing returns a half-checked result.
Limits that fail closed
Every call that reads a token bounds its work: signatures, recipients, key trials and PBES2 iterations. Past a limit the token is refused.