Skip to main content

Let clients register themselves

Accept a registration request, return a client ID, and control which clients may register.

RecipeReadyView source
Works with
Plain HTTP
Standards
RFC 7591

What it shows​

  1. An administrator turns on dynamic client registration and creates an initial access token. Only those who hold the token may register.
  2. A partner app, "Northwind Calendar sync", posts its name and redirect URIs with the token.
  3. It receives a client ID, a client secret and a registration access token.
  4. It reads its own registration back with the registration access token.
  5. A web client with an http redirect URI is refused, and so is a request without the token.

Before you start​

  • An API key for the setup script, with the scopes admin:settings:read and admin:settings:write. The setup script changes a setting of the whole organization.
  • The switch is also in the portal: Security, Sign-in settings, section Platform security, Dynamic client registration. The initial access tokens have no page in the portal; the setup script sets them through the Admin API.
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.
LUMOAUTH_API_KEYFor the setup script only: an API key with the scopes admin:settings:read and admin:settings:write. The app that registers needs no API key.
INITIAL_ACCESS_TOKENThe token that allows a registration. The setup script prints it once.

The setup script turns on dynamic client registration for your organization and replaces its initial access tokens with a new one. Run node oauth/dynamic-client-registration/setup.mjs off to turn registration off again.

node oauth/dynamic-client-registration/setup.mjs
setup.mjs
// Lets applications register themselves: turns on dynamic client
// registration for the organization and sets one initial access token.
// Run it with `off` to turn registration off again.
//
// node oauth/dynamic-client-registration/setup.mjs
// node oauth/dynamic-client-registration/setup.mjs off
//
// The API key needs the scopes admin:settings:read and admin:settings:write.
import { randomBytes } from 'node:crypto';
import { call, printEnv } from '../../_lib/admin.mjs';

if (process.argv[2] === 'off') {
await call('PATCH', '/admin/settings/authentication', { dynamic_client_registration_enabled: false, initial_access_tokens: [] });
console.log('Dynamic client registration is off, and no initial access token is left.');
process.exit(0);
}

// The token lets its holder register an application. LumoAuth stores only a
// hash of it, so this is the only copy. (A value of 64 hex characters would
// count as a hash already, so the token has another form.)
const token = `iat_${randomBytes(24).toString('base64url')}`;

// The list is replaced: LumoAuth never returns the stored tokens, so a
// script cannot add one to them. A new run makes the old token invalid.
await call('PATCH', '/admin/settings/authentication', { dynamic_client_registration_enabled: true, initial_access_tokens: [token] });

console.log('Dynamic client registration is on. Give the token only to those who may register.');
printEnv({ INITIAL_ACCESS_TOKEN: token });

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. Each run registers one more application. The script prints whether a secret came back, never the secret.

oauth/dynamic-client-registration/http/register.sh
#!/bin/sh
# A partner app registers itself as an OAuth client, reads its registration
# back, and meets two refusals, with curl.
#
# sh oauth/dynamic-client-registration/http/register.sh
#
# The script needs curl and jq.
set -eu

REGISTER="$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1/connect/register"

register() {
curl --silent --show-error "$REGISTER" \
--header "Content-Type: application/json" \
--write-out '\n%{http_code}' \
"$@"
}

# 1. The app posts its name and redirect URIs, with the initial access token.
# A web client registers https redirect URIs only.
ANSWER=$(register \
--header "Authorization: Bearer $INITIAL_ACCESS_TOKEN" \
--data '{
"client_name": "Northwind Calendar sync",
"application_type": "web",
"redirect_uris": ["https://calendar.northwind.example/oauth/callback"],
"scope": "openid profile email"
}')
BODY=$(printf '%s' "$ANSWER" | sed '$d')
echo "registered: HTTP $(printf '%s' "$ANSWER" | tail -n 1)"
printf '%s' "$BODY" | jq -c '{client_id, token_endpoint_auth_method, grant_types, scope, client_secret: (.client_secret != null), registration_access_token: (.registration_access_token != null)}'

# 2. It keeps the secret and the registration access token, and reads its
# registration back from the address that the answer names.
curl --silent --show-error "$(printf '%s' "$BODY" | jq -r .registration_client_uri)" \
--header "Authorization: Bearer $(printf '%s' "$BODY" | jq -r .registration_access_token)" \
| jq -c '{client_name, redirect_uris, client_secret: (.client_secret != null)}'

# 3. A web client with an http redirect URI is refused.
register \
--header "Authorization: Bearer $INITIAL_ACCESS_TOKEN" \
--data '{"client_name": "Northwind test", "redirect_uris": ["http://calendar.northwind.example/oauth/callback"]}' \
| tr '\n' ' '
echo

# 4. Without the initial access token, nobody may register.
register --data '{"client_name": "Unknown app", "redirect_uris": ["https://unknown.example/callback"]}' | tr '\n' ' '
echo
sh oauth/dynamic-client-registration/http/register.sh

No SDK has a call for the registration endpoint, so the recipe uses curl. Any HTTP client can send the same JSON.

Check the result​

Output
registered: HTTP 201
{"client_id":"d6bb1228f35db904dc7812802b595f95","token_endpoint_auth_method":"client_secret_basic","grant_types":["authorization_code","refresh_token"],"scope":"openid profile email","client_secret":true,"registration_access_token":true}
{"client_name":"Northwind Calendar sync","redirect_uris":["https://calendar.northwind.example/oauth/callback"],"client_secret":false}
{"error":"invalid_redirect_uri","error_description":"Web Client redirect_uris must use the https scheme (use application_type \u0022native\u0022 for loopback or custom-scheme redirect URIs)"} 400
{"error":"access_denied","error_description":"Initial Access Token required"} 401
  • The new application is in the portal under Applications & developer tools, Applications.
  • The client secret and the registration access token come back once. LumoAuth stores only their hashes. The registration that you read back holds neither.
  • A native app registers "application_type": "native" with a loopback or private-use redirect URI, such as http://127.0.0.1/callback.

Limits​

  • An application can register the grant types authorization_code and refresh_token only. Any other grant type is refused with invalid_client_metadata.
  • Registration grants the scopes openid, profile and email. Other scopes that you ask for are left out without an error; an administrator can add them later.
  • The registration endpoint can read a registration, not change or delete it. An administrator does that in the portal or through the Admin API.
  • The endpoint accepts 10 registrations per minute.
  • An API key with the scope admin:clients:register can register an application without the switch and without an initial access token.
  • An MCP client can use a client ID metadata document instead, with no registration step.