Docs / Guides

Login tokens

tokens issues and checks JWS login tokens signed with ML-DSA-65 (RFC 9964), with a reference mode that keeps browser cookies small.

01Bearer tokens

const token = tokens.issue(tokenKeys, { iss: 'https://login.example', sub: user.id, aud: 'api' }, { ttl: 900 });
const claims = tokens.check(publicTokenKeys, token, { iss: 'https://login.example', aud: 'api' });
token = tokens.issue({"iss": "https://login.example", "sub": user_id, "aud": "api"}, token_keys, ttl=900)
claims = tokens.check(token, public_token_keys, iss="https://login.example", aud="api")
tok, err := tokens.Issue(tokenKeys, tokens.Claims{"iss": "https://login.example", "sub": userID, "aud": "api"}, nil)
claims, err := tokens.Check(publicTokenKeys, tok, tokens.CheckOptions{Iss: "https://login.example", Aud: "api"})

issue fills iat, exp (default 15 minutes) and jti. check returns the claims only if every rule holds:

02Reference mode for cookies

An ML-DSA-65 token is about 4.6 KB, which is too big for a cookie. Reference mode stores the signed token on the server and gives the browser a ~49-byte pqlr. cookie. The store is keyed by the SHA-256 of the cookie id, so a leaked store yields no usable cookies, and every request still runs the full check.

const store = new tokens.MemoryStore();          // use Redis or a database in production
const ref = await tokens.issueReference(tokenKeys, store, { iss, sub: user.id, aud: 'web' });
res.setHeader('Set-Cookie', tokens.setCookieHeader('session', ref));   // HttpOnly; Secure; SameSite=Lax

const claims = await tokens.checkReference(publicTokenKeys, store, req.cookies.session, { iss, aud: 'web' });
await tokens.revokeReference(store, req.cookies.session);              // logout
ref, err := tokens.IssueReference(ctx, tokenKeys, store, claims, nil)
header, err := tokens.SetCookieHeader("session", ref)
claims, err := tokens.CheckReference(ctx, publicTokenKeys, store, cookie, opts)
err = tokens.RevokeReference(ctx, store, cookie)

03Accepting old tokens during migration

tokens.check never accepts a classical algorithm. While old RS256 or ES256 tokens are still live, check them through legacy.verifyJwt, which only verifies and never issues. See Migration.

pqlock v0.1.0 · unaudited preview · source of truth: spec/