A collection of low-level and high-level, server-agnostic, Authentication and Authorization (soon) utilities.
Warning
This package is in active development. It is not recommended for production use yet unless you are willing to help with testing and feedback. Expect breaking changes.
- OAuth 2.1 and OpenID Connect — one handler for both legs of a sign-in, presets for GitHub, Google, Apple, Microsoft, Discord, GitLab, LinkedIn, Facebook, Twitch, X, Spotify, TikTok, Netlify, Vercel and Cloudflare Access, generic
defineOAuth2ProvideranddefineOidcProvider - AT Protocol — sign in with any PDS account: handle resolution, PAR, PKCE, DPoP, the account verified against its PDS automatically
- Custom providers — any protocol over the same engine, with
startandcompletelegs - Sessions —
unauth/sessionis unjwt's cookie sessions plusdefineOpaqueSession: a random token in the cookie or a bearer header, the record in your store, same manager as the JWE session - One-time tokens —
unauth/token: signed, single-use, expiring tokens, and opaque tokens with their hash - Web-standard — handlers take a request event
{ req, res: { headers } };startFlowandcompleteFlowtake aRequest - unjwt included —
unauth/jwk,unauth/jws,unauth/jwe,unauth/utils,unauth/sessionandunauth/dpopare unjwt's subpaths, one dependency to install
Built on:
npx nypm install unauthunauth never reads or writes the application's session. onSuccess(event, result) gets the request event and the proven result.identity, and the application saves whatever session it wants. Two ways to write the createSession and useAuthSession the examples below call:
A JWE cookie session with unauth/session (unjwt's defineJWESession, re-exported):
import type { H3Event } from "h3";
import { defineJWESession } from "unauth/session";
const sessions = defineJWESession<{ userId: string }, "1h">({ key, maxAge: "1h", rolling: "15m" });
export async function createSession(event: H3Event, { userId }: { userId: string }) {
const session = await sessions.use(event);
await session.clear();
await session.update({ userId });
}
export async function useAuthSession(event: H3Event) {
const session = await sessions.use(event);
return session.id ? { userId: session.data.userId } : undefined;
}An opaque session with defineOpaqueSession: the cookie carries a random token, a store of yours (five verbs over a table, or kvSessionStore over any CacheDriver) holds the session under it, so sessions can be listed and revoked. Unlike the JWE manager, whose data travels in the cookie, this one reads the store only when asked: use() is synchronous, record is a promise.
import type { H3Event } from "h3";
import { defineOpaqueSession } from "unauth/session";
const sessions = defineOpaqueSession<{ userId: string }>({ store, maxAge: "30D", idle: "7D" });
export async function createSession(event: H3Event, { userId }: { userId: string }) {
const session = sessions.use(event);
await session.clear();
await session.update({ userId });
}
export async function useAuthSession(event: H3Event) {
const { data } = await sessions.use(event).record;
return data;
}maxAge is the absolute lifetime, idle ends a session left alone, either or both. rotate() moves a session under a new token, issue(data) mints one without a request, sessionHeader: true also reads a Bearer token. The store may be a function of the request event, so a session write joins the transaction the request opened. A route that queries the database anyway can skip the session read and filter on the carried session.token (hashed as the store hashes it) inside its own query; playgrounds/nuxt-4-opaque does both over drizzle.
The full versions, with lifetimes, rotation and sign-out, are playgrounds/nuxt-4 and playgrounds/nuxt-4-opaque.
One route serves both legs: without code or error it redirects to the provider with a sealed flow cookie; with one it completes the callback and calls onSuccess with the proven identity.
import type { H3Event } from "h3";
import { deriveJWK } from "unauth/n";
import { defineGitHubHandler } from "unauth/n/providers/github";
app.all(
"/auth/github",
defineGitHubHandler({
clientId: process.env.GITHUB_CLIENT_ID!,
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
key: () => deriveJWK(process.env.AUTH_SECRET!, { info: "unauth:flow" }),
origin: "https://app.example",
onSuccess: async (event: H3Event, { identity }) => {
const user = await findOrCreateUser(identity);
await createSession(event, { userId: user.id });
},
}),
);key: the key that seals the sign-in's flow cookie, or a callback returning it, called once on the first request.deriveJWK(secret, { info })turns a random secret of 32+ characters into one. Any symmetric JWE key works, the shapes unjwt's JWE sessions take: anoctJWK, a secretCryptoKey, or a JWK Set whose first key seals and every key opens, to rotate. A key naming no algorithm needsjwe: { alg, enc };onKeyLookuppicks the opening key from a token's header. A key pair is refused, since a token sealed to a public key could be minted by anyone holding it.origin: the callback is this route on that origin, and its forms may start a sign-in that links an account or names it; a request on any other origin is refused with a421.redirectUrinames the callback outright instead. Withorigintoo, both are where the sign-in runs.onStart(event): optional; returns{ redirect, memo, loginHint, scopes, params }for this request.memois anything the application wants back at the callback, sealed;loginHintnames the account (OAuth'slogin_hint, an AT Protocol handle). A start that returns eithermemo, orloginHintfor a custom provider, is taken only as aPOSTfrom the application's own origin (ERR_AUTHN_CROSS_SITE_START, a403, otherwise): start those sign-ins from a form, and return them only whenevent.req.method === "POST".onSuccess(event, result): required;resultholdsidentity,tokens,memoandredirect. Returning nothing answers a302toresult.redirect, else theredirectoption, else/; returning aResponseor a value answers that.- Headers the application sets on
event.res.headersinonStartoronSuccess(its session cookie), and the expiry of the consumed flow cookie, reach the browser because the framework sends them with whatever the handler returns, as h3, Nitro and Nuxt do; an adapter for another framework must apply them. fetch: optional; replaces the globalfetchfor every request a sign-in makes to a provider, where a deployment refuses private network ranges.onError(event, error): optional;erroris anUnauthErrorwithcode,causeandstatus, anUnauthErrorthrown byonStartoronSuccessincluded. Without it, or when it returns nothing, the failure is answered with a plainResponseof that status: answer the codes you care about, return nothing for the rest.- Annotate the
eventof the first hook you write with the framework's event type: every hook and the handler take it,resultandmemostay inferred. defineSignInHandler(provider, options)is the same handler for any provider. It answersGETandPOSTand refuses other methods with a405: mount it without a method filter (app.all(...), aroutes/auth/github.tsrather than.get.ts).
onStart notes the signed-in user in memo; it comes back as result.memo, sealed, and onSuccess decides what to do with it. Such a start comes from a form, never from a link: a link proves nothing about who chose it, a form on the application's own page does. onStart returns the memo only for a POST, so a signed-in user following a sign-in link signs in as usual instead of meeting a 403.
onStart: async (event: H3Event) => {
if (event.req.method !== "POST") return;
const session = await useAuthSession(event);
return session ? { memo: { userId: session.userId } } : undefined;
},
onSuccess: async (event, { identity, memo }) => {
if (memo) return linkIdentity(memo.userId, identity);
// a plain sign-in otherwise
},<form method="post" action="/auth/github"><button>Link a GitHub account</button></form>import { defineAtprotoHandler } from "unauth/n/providers/atproto";
app.all(
"/auth/atproto",
defineAtprotoHandler({
clientId: "https://app.example/oauth-client-metadata.json",
redirectUri: "https://app.example/auth/atproto",
key,
onStart: async (event: H3Event) => ({ loginHint: (await event.req.formData()).get("login") }),
onSuccess,
}),
);onStartnames the account (a handle, a DID or a server URL) asloginHint; the handler reads nothing from the request. Naming the account picks the server the sign-in is pushed to, so the start is aPOSTform from the application's own pages (ERR_AUTHN_CROSS_SITE_STARTotherwise).- The provider's
client.metadatais the document to serve atclientId; define the provider once withdefineAtprotoProviderand pass it asproviderto share it with that route. - A
clientIdonhttp://localhostis the development client; its callback must be on a loopback IP.devOrigins: ["http://localhost:3000"]maps alocalhost:3000callback to127.0.0.1:3000and returns the result, or its failure, tohttp://localhost:3000, whereonSuccessoronErrorruns.
A custom provider runs its own two legs over the engine's flow. state and the flow cookie only prove that the callback came back to the browser that started the sign-in, and that browser may be anyone's: complete must take the identity from a proof the provider issued and verifies there, never from what start was told. A magic link, with the proof a one-time token sent to the address:
import { UnauthError, defineProvider } from "unauth/n";
import { defineOneTimeToken } from "unauth/token";
const magicLinks = defineOneTimeToken<{ email: string; state: string }>({
key: signingKey,
maxAge: "15m",
purpose: "magic-link",
consume: ({ jti, expiresAt }) => usedLinks.add(jti, expiresAt),
});
const magic = defineProvider<{ email: string }>({
id: "magic",
kind: "custom",
start: async ({ record, redirectUri, loginHint }) => {
if (!loginHint) {
throw new UnauthError("Enter your email to sign in.", "ERR_AUTHN_LOGIN_HINT_REQUIRED", {
status: 400,
});
}
const link = new URL(redirectUri);
link.searchParams.set("state", record.state);
link.searchParams.set(
"code",
await magicLinks.issue({ email: loginHint, state: record.state }),
);
await sendMail(loginHint, link.href);
return { url: new URL("/check-your-inbox", redirectUri) };
},
complete: async ({ params, record }) => {
const claim = await magicLinks.open(params.code ?? "");
if (claim.state !== record.state) {
throw new UnauthError("This link belongs to another sign-in.", "ERR_AUTHN_STATE_MISMATCH", {
status: 401,
cause: { provider: "magic" },
});
}
return {
identity: {
subject: claim.email,
email: claim.email,
emailVerified: true,
profile: { email: claim.email },
},
};
},
});startsends the user somewhere and may sealdatain the record forcomplete; it is the application's input, sealed, not a proof. Here the proof is the token: only the inbox receives it, and it names thestateof the sign-in it was issued for.completereceives the callback'sparamswithstatealready checked against the record, and returns the identity it verified. A callback without a usable proof must throw: the engine never completes on its own.consumerecords each token'sjtiuntilexpiresAtand answers whether this was its first use;usedLinksis whatever store the deployment has.
The playgrounds/ run the same application on h3, Nitro 3 and Nuxt 4.
local development
I started by building unjwt, as I needed a cryptographically secure way to transmit sensitive information between various programming languages and servers. Not long after I started requiring some standardization, in particular on how to handle authentication flows, but as I was testing various libraries I've never been satisfied by their DX (either too basic or too complex to maintain). So I started building unauth as a collection of low-level primitives that get wrapped in high-level abstractions for every day use.
- Thanks to Vidbase, Inc. (in particular to Van Nguyen) for the npm package name donation
Published under the MIT license. Made by community 💛
🤖 auto updated with automd