Sign in from a third-party app
For: a developer of a CLI, a native app, or a server that acts for a person at an authtempo provider.
What it does: the app gets an access token for one resource and a refresh token, with PKCE and a check of state and iss.
Before you start
Section titled “Before you start”- A provider with
oauth.resources. Be the OAuth and OIDC provider for MCP clients sets it.
- Register the app once with
registerOAuthClient. Keep the client id. - Make the OAuth client with the issuer, the client id, the redirect URI, the resource, and the scopes.
- Open the URL of
authorizeURL()in the system browser. - Take the URL that the redirect brings back, and pass it to
handleRedirect(url). - Keep the refresh token in a safe store. Call
refreshwhen the access token expires.
import { createOAuthClient, registerOAuthClient } from "@authtempo/client/oauth";
const ISSUER = "https://api.example.com/auth";// A CLI takes the redirect on a loopback port. Core accepts http on 127.0.0.1 with any port.const REDIRECT = "http://127.0.0.1:8123/callback";
/** Registers the CLI once. Keep the client id in the CLI's config. */export function registerCli(fetch?: typeof globalThis.fetch) { return registerOAuthClient({ issuer: ISSUER, clientName: "Example CLI", redirectUris: [REDIRECT], ...(fetch ? { fetch } : {}), });}
/** The OAuth client of the CLI: an access token for the MCP resource and a refresh token. */export function cliOAuthClient(clientId: string, fetch?: typeof globalThis.fetch) { return createOAuthClient({ issuer: ISSUER, clientId, redirectUri: REDIRECT, resource: "https://api.example.com/mcp", // offline_access asks for a refresh token. scopes: ["openid", "offline_access"], ...(fetch ? { fetch } : {}), });}import { createAuth } from "@authtempo/core";import { describe, expect, it } from "vitest";import { API, harness, signIn } from "../support/harness.ts";import { cliOAuthClient, registerCli } from "./third-party-app.ts";
describe("sign in from a third-party app", () => { it("registers a CLI, signs the person in with PKCE, and refreshes", async () => { const h = harness(); const auth = createAuth({ baseURL: API, trustedOrigins: [], secret: h.secret, storage: h.storage, mail: h.mail, oauth: { resources: [`${API}/mcp`], registration: { cimd: false, dynamic: true } }, }); const fetch = ((input: RequestInfo | URL, init?: RequestInit) => auth.handle(new Request(input, init))) as typeof globalThis.fetch; const reg = await registerCli(fetch); if (!reg.ok) throw new Error(reg.code); const oauth = cliOAuthClient(reg.value.clientId, fetch); const url = await oauth.authorizeURL(); if (!url.ok) throw new Error(url.code); // The person's browser: signed in, it opens the URL and allows the CLI. const cookie = await signIn(auth, h, "ada@example.com"); const page = await auth.handle(new Request(url.value, { headers: { cookie } })); const id = /name="request_id" value="(oq_[A-Za-z0-9_-]{43})"/.exec(await page.text())?.[1]; const allowed = await auth.handle( new Request(`${API}/auth/oauth/authorize`, { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded", origin: API, cookie }, body: new URLSearchParams({ request_id: id ?? "", decision: "allow" }).toString(), }), ); const tokens = await oauth.handleRedirect(allowed.headers.get("location") ?? ""); if (!tokens.ok) throw new Error(tokens.code); expect(tokens.value.scopes).toEqual(["openid", "offline_access"]); const next = await oauth.refresh(tokens.value.refreshToken ?? ""); expect(next.ok).toBe(true); });});How it behaves
Section titled “How it behaves”- A CLI takes the redirect on
127.0.0.1with any port. - A single-page app on its own registered origin can finish the sign-in in the browser. Pass
browserPendingStore()aspending. - The token endpoint counts on
oauth-token:ip:<ip>, 60 per 15 minutes (Limits).
If it fails
Section titled “If it fails”redirect_mismatch: thestateor theissof the redirect does not match a sign-in of this client. Start again.invalid_grant: the code or the refresh token is spent or expired. Start again.access_denied: the person did not allow the app.invalid_client: the provider does not know the client id.