Mint a JWT for your other services
For: a developer whose other server cannot call auth.session, but must know the signed-in user.
What it does: core signs a JWT about the session user. The other server checks it against the public keys at /auth/jwks.
Before you start
Section titled “Before you start”- A working setup from a getting started tutorial.
- The verify package on the other server. The verify package lists its options.
- Set
jwtMint.audienceto the URL of the other server. The default isbaseURL. - Set
jwtMint.ttlMinutesshort. A sign-out does not end a token. - Add your own claims with
jwtMint.claims. Core sets the reserved claims itself.
import { createAuth, type MailPort, type StorageAdapter } from "@authtempo/core";
export function createJwtAuth(deps: { storage: StorageAdapter; mail: MailPort; secret: string }) { return createAuth({ baseURL: "https://api.example.com", trustedOrigins: [], secret: deps.secret, storage: deps.storage, mail: deps.mail, // JWT minting is on by default. `jwtMint: false` removes /jwks, /token, and api.jwt. jwtMint: { // EdDSA (Ed25519) is the default. ES256 and RS256 are the other choices. alg: "EdDSA", // The server that takes the token. The default is baseURL. audience: "https://files.example.com", // The life of a token, 1 to 60 minutes. A sign-out does not end it. ttlMinutes: 10, // The app's own claims. Core sets the reserved claims itself. claims: () => ({ plan: "free" }), }, });}- In the app, call
client.jwt.token().- Send the token with each request to the other server.
- On the other server, check the token with
createVerifierof@authtempo/verify. It checks the signature,iss,aud,exp, and the headertyp: "JWT".
import { createVerifier, type VerifierOptions } from "@authtempo/verify";
/** * The check of the other server, with @authtempo/verify. It reads the key set * of the API, keeps it for its max-age, and checks the signature, iss, aud, * exp, and the header typ "JWT", which refuses an OAuth access token. */export function createJwtVerifier(fetch?: VerifierOptions["fetch"]) { return createVerifier({ issuer: "https://api.example.com/auth", audience: "https://files.example.com", ...(fetch ? { fetch } : {}), });}import { createAuth } from "@authtempo/core";import { describe, expect, it } from "vitest";import { call, harness, signIn } from "../support/harness.ts";import { createJwtAuth } from "./jwt.ts";import { createJwtVerifier } from "./jwt-verify.ts";
const files = (token: string) => new Request("https://files.example.com/doc", { headers: { authorization: `Bearer ${token}` } });
describe("mint a JWT for your other services", () => { it("mints a session JWT that another server verifies against the key set", async () => { const h = harness(); const auth = createJwtAuth(h); const ada = await signIn(auth, h, "ada@example.com"); const { token } = (await ( await call(auth, "GET", "/auth/token", undefined, { cookie: ada }) ).json()) as { token: string }; const verifier = createJwtVerifier((input, init) => auth.handle(new Request(input, init))); const r = await verifier.verifyRequest(files(token)); if (!r.ok || r.value?.kind !== "session-jwt") throw new Error("no caller"); expect(r.value.email).toBe("ada@example.com"); expect(r.value.claims.plan).toBe("free"); expect(typeof r.value.sessionId).toBe("string"); });
it("refuses a token for another audience", async () => { // A config with no jwtMint.audience mints for baseURL, not for the files server. const h = harness(); const auth = createAuth({ baseURL: "https://api.example.com", trustedOrigins: [], secret: h.secret, storage: h.storage, mail: h.mail, }); const ada = await signIn(auth, h, "ada@example.com"); const { token } = (await ( await call(auth, "GET", "/auth/token", undefined, { cookie: ada }) ).json()) as { token: string }; const verifier = createJwtVerifier((input, init) => auth.handle(new Request(input, init))); expect(await verifier.verifyRequest(files(token))).toEqual({ ok: true, value: null }); });});How it behaves
Section titled “How it behaves”- The claims are
iss(baseURLplusbasePath),sub,aud,iat,exp,sid,email,email_verified,name, andorg. - The key is EdDSA (Ed25519) by default, ES256, or RS256 (
jwtMint.alg). Core writes the first key at the first use and seals its private half undersecret. api.jwt.rotate()makes a new signing key. The old key verifies for 24 hours and signs nothing.GET /auth/jwksanswers the public keys withcache-control: public, max-age=300.- The same key signs OAuth access tokens, with
typ: "at+jwt". A server that skips thetypcheck can take one kind for the other. - Core refuses a
jwtMint.audienceentry that is also anoauth.resourcesentry. - Where a sign-out must end access at once, set
checkSessionEnd: trueon the verifier. It asksPOST /auth/token/livefor each token.
If it fails
Section titled “If it fails”unauthorized:GET /auth/tokenneeds a session. An API token and a JWT get 401.hook_failed:jwtMint.claimsthrew, or answered a value that is not an object.