Skip to content

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.

  1. Set jwtMint.audience to the URL of the other server. The default is baseURL.
  2. Set jwtMint.ttlMinutes short. A sign-out does not end a token.
  3. Add your own claims with jwtMint.claims. Core sets the reserved claims itself.
The JWT config
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" }),
},
});
}
  1. In the app, call client.jwt.token().
    • Send the token with each request to the other server.
  2. On the other server, check the token with createVerifier of @authtempo/verify. It checks the signature, iss, aud, exp, and the header typ: "JWT".
The check of the other server
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 } : {}),
});
}
Mint and verify as a test
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 });
});
});
  • The claims are iss (baseURL plus basePath), sub, aud, iat, exp, sid, email, email_verified, name, and org.
  • 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 under secret.
  • api.jwt.rotate() makes a new signing key. The old key verifies for 24 hours and signs nothing.
  • GET /auth/jwks answers the public keys with cache-control: public, max-age=300.
  • The same key signs OAuth access tokens, with typ: "at+jwt". A server that skips the typ check can take one kind for the other.
  • Core refuses a jwtMint.audience entry that is also an oauth.resources entry.
  • Where a sign-out must end access at once, set checkSessionEnd: true on the verifier. It asks POST /auth/token/live for each token.
  • unauthorized: GET /auth/token needs a session. An API token and a JWT get 401.
  • hook_failed: jwtMint.claims threw, or answered a value that is not an object.