Skip to content

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.

  1. Register the app once with registerOAuthClient. Keep the client id.
  2. Make the OAuth client with the issuer, the client id, the redirect URI, the resource, and the scopes.
  3. Open the URL of authorizeURL() in the system browser.
  4. Take the URL that the redirect brings back, and pass it to handleRedirect(url).
  5. Keep the refresh token in a safe store. Call refresh when the access token expires.
The OAuth client of a CLI
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 } : {}),
});
}
The sign-in as a test
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);
});
});
  • A CLI takes the redirect on 127.0.0.1 with any port.
  • A single-page app on its own registered origin can finish the sign-in in the browser. Pass browserPendingStore() as pending.
  • The token endpoint counts on oauth-token:ip:<ip>, 60 per 15 minutes (Limits).
  • redirect_mismatch: the state or the iss of 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.