Skip to main content

A client that identifies itself with a URL

Publish a small JSON document, use its address as the client ID, and sign in with no registration step.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
JavaScript / TypeScriptPlain HTTP

What it shows​

  1. The desktop app "Northwind Notes" serves a metadata document at a public https address. Its client_id member is that address.
  2. The app starts the authorization code flow with PKCE, with the address as client_id. Nobody registered the app in LumoAuth.
  3. LumoAuth fetches the document, checks it, and shows the host of the address on the consent screen.
  4. The app redeems the code with PKCE and no secret, and reads the profile of the person.
  5. With a private key, the same program publishes the public key in the document and authenticates with a client assertion.

Before you start​

  • A public https address with a path for the document, for example https://client.example.com/oauth/metadata.json, that leads to PORT on your machine: your web server, or an https tunnel. LumoAuth reads the document itself, so a local or private address does not work.
  • Client ID metadata documents allowed in your organization. They are on by default: the switch is under Security, Sign-in settings, Platform security, Client ID metadata documents (CIMD).
  • For the key version, a private key: openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out notes-key.pem, then export CLIENT_PRIVATE_KEY="$(cat notes-key.pem)".
  • For the check script, an API key with the scope admin:clients:read.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
CLIENT_IDThe public https address of the metadata document, with a path, for example https://client.example.com/oauth/metadata.json. It must lead to PORT on your machine.
PORTOptional. The local port that serves the document. The default is 8787.
CALLBACK_PORTOptional. The loopback port for the callback of the sign-in. The default is 8788.
CLIENT_PRIVATE_KEYOptional. A private key as PEM text. With it, the document publishes the public key and the client authenticates with a client assertion.
LUMOAUTH_API_KEYFor the check script only: an API key with the scope admin:clients:read.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. This is the output of the Node.js program after a sign-in. The run needs the document at a public https address, and was not seen to the end: with a local address, LumoAuth answers unsafe_url. The check script prints the check result as JSON.

oauth/client-id-metadata-document/js/cimd-client.mjs
// A client that identifies itself with a URL. It serves its own metadata
// document, then signs a person in with that URL as client_id: there is no
// registration step. With CLIENT_PRIVATE_KEY set, the document publishes the
// public key, and the token request carries a client assertion.
//
// node oauth/client-id-metadata-document/js/cimd-client.mjs
//
// CLIENT_ID is a public https address with a path, for example
// https://client.example.com/oauth/metadata.json, that leads to PORT on this
// machine: through your web server, or through an https tunnel.
import { createHash, randomBytes } from 'node:crypto';
import { EventEmitter, once } from 'node:events';
import http from 'node:http';
import { CLIENT_ASSERTION_TYPE, ClientAssertionSigner, issuerFor } from '@lumoauth/agent';

const issuer = issuerFor(process.env.LUMOAUTH_URL ?? 'https://app.lumoauth.dev', process.env.LUMOAUTH_ORG_ID);
const clientId = process.env.CLIENT_ID;
const port = Number(process.env.PORT ?? 8787);
const callbackPort = Number(process.env.CALLBACK_PORT ?? 8788);
const redirectUri = `http://127.0.0.1:${callbackPort}/callback`;
const signer = process.env.CLIENT_PRIVATE_KEY ? ClientAssertionSigner.fromPem(process.env.CLIENT_PRIVATE_KEY, { keyId: 'cimd-1' }) : null;

// 1. The metadata document. Its client_id is the address that it is served
// at, byte for byte. A loopback redirect URI matches any port.
const metadata = {
client_id: clientId,
client_name: 'Northwind Notes for desktop',
client_uri: new URL(clientId).origin,
redirect_uris: ['http://127.0.0.1/callback'],
grant_types: ['authorization_code', 'refresh_token'],
response_types: ['code'],
scope: 'openid profile email',
...(signer ? { token_endpoint_auth_method: 'private_key_jwt', jwks: signer.jwks } : { token_endpoint_auth_method: 'none' }),
};
const documentServer = http.createServer((request, response) => {
if (request.method !== 'GET' || request.url !== new URL(clientId).pathname) return response.writeHead(404).end();
// LumoAuth keeps a valid document for 60 seconds to one hour.
response.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'public, max-age=60' });
response.end(JSON.stringify(metadata));
});
documentServer.listen(port);
console.log(`document: ${clientId} (${metadata.token_endpoint_auth_method})`);

// 2. The discovery document says whether the organization accepts such clients.
const server = await (await fetch(`${issuer}/.well-known/openid-configuration`)).json();
if (server.client_id_metadata_document_supported !== true) throw new Error('This organization accepts no client ID metadata documents.');

// 3. The authorization code flow with PKCE, with the URL as client_id.
// LumoAuth fetches the document now, and the consent screen names its host.
const b64url = (bytes) => bytes.toString('base64url');
const verifier = b64url(randomBytes(32));
const state = b64url(randomBytes(16));
const authorize = `${server.authorization_endpoint}?${new URLSearchParams({
response_type: 'code',
client_id: clientId,
redirect_uri: redirectUri,
scope: metadata.scope,
state,
code_challenge: b64url(createHash('sha256').update(verifier).digest()),
code_challenge_method: 'S256',
})}`;
const callbacks = new EventEmitter();
const callbackServer = http.createServer((request, response) => {
response.end('Done. You can close this tab.');
callbacks.emit('callback', new URL(request.url, redirectUri).searchParams);
});
callbackServer.listen(callbackPort, '127.0.0.1');
console.log(`Open this address in a browser and sign in:\n${authorize}\n`);
const [callback] = await once(callbacks, 'callback');
if (callback.get('state') !== state || !callback.get('code')) throw new Error(`Sign-in failed: ${callback.get('error_description') ?? callback.get('error')}`);

// 4. The token request: PKCE, and a client assertion when the document has a
// key. No client secret exists for such a client.
const response = await fetch(server.token_endpoint, {
method: 'POST',
body: new URLSearchParams({
grant_type: 'authorization_code',
code: callback.get('code'),
redirect_uri: redirectUri,
client_id: clientId,
code_verifier: verifier,
...(signer ? { client_assertion_type: CLIENT_ASSERTION_TYPE, client_assertion: signer.create(clientId, issuer) } : {}),
}),
});
const tokens = await response.json();
// error_description names the reason, for example "(client_id_mismatch)".
if (!response.ok) throw new Error(`The token request failed: ${tokens.error_description ?? tokens.error}`);
console.log(`signed in: scope ${tokens.scope}, refresh token: ${tokens.refresh_token ? 'yes' : 'no'}`);

const me = await (await fetch(server.userinfo_endpoint, { headers: { Authorization: `Bearer ${tokens.access_token}` } })).json();
console.log(`userinfo: ${me.email}`);
documentServer.close();
callbackServer.close();
callbackServer.closeAllConnections();
node oauth/client-id-metadata-document/js/cimd-client.mjs

Compiles. Not run against an organization. Ran with a local address as CLIENT_ID: the program served its document, and the sign-in page of LumoAuth refused the address with unsafe_url. A sign-in with a public address was not seen.

The Node.js program needs @lumoauth/agent only for the key version: ClientAssertionSigner gives the public key for the document and signs the assertions. The check script asks LumoAuth to read the document the way that the sign-in does, and prints the result. It registers nothing.

Check the result​

Output
document: https://client.example.com/oauth/metadata.json (none)
signed in: scope openid profile email, refresh token: yes
userinfo: maya.chen@example.com
  • The consent screen labels the app as an external client, with the host of its address. Because the callback goes to 127.0.0.1, it also warns that the app runs on the person's own machine.
  • After the first sign-in, the app is in the portal under Applications & developer tools, Applications, tab External clients (CIMD). An administrator can block it there.
  • When something is wrong, the error page of the sign-in and the token endpoint name the reason, for example client_id_mismatch. The check script shows the same reason, and the scopes and redirect URIs that the organization dropped.

Limits​

  • The address must be public https, with a path. A local address, localhost or a private network address is refused with unsafe_url. This page stays a draft until a sign-in with a public address is seen.
  • LumoAuth keeps a valid document for 60 seconds to one hour, after its Cache-Control header. A document that it refused is not kept, so a fixed document works at the next request.
  • A client of this kind signs people in. It never gets the client credentials grant, and it has no client secret.
  • The document cannot give its client more than the identity scopes, mcp:read and the scopes of your MCP servers, unless the organization allows more scopes for such clients.
  • For clients that need a custom scheme as redirect URI, use dynamic registration.