Skip to main content

Push the authorization request

Send the authorization request from your server first, so that the browser carries only a reference to it.

RecipeReadyView source
Works with
JavaScript / TypeScriptPlain HTTP
Standards
RFC 9126

What it shows​

  1. The server of the web app posts the whole authorization request to the PAR endpoint, with its client secret. It receives a request_uri.
  2. The browser goes to the authorization endpoint with client_id and request_uri only. The address bar shows no scope and no redirect URI.
  3. The person signs in, and the rest is the authorization code flow.
  4. The request_uri works once. The same address again gets an error page.
  5. The application requires PAR, so a request with the parameters in the address is refused.

The browser, its extensions and its history never see the parameters, and nobody can change them on the way.

Before you start​

  • An API key for the setup script, with the scopes admin:clients:read and admin:clients:write.
  • A confidential application. The setup script creates the application "Northwind Payroll".
  • PAR required for the application. The Admin API cannot change this switch, so use the portal:
    1. Open Applications & developer tools, Applications, and select Northwind Payroll.
    2. Open the Security tab.
    3. Under FAPI 2.0 and client authentication, turn on Require PAR.
    4. Select Save FAPI settings.
  • A user of your organization to sign in with, and a browser.
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 client ID of the application. The setup script prints it.
CLIENT_SECRETThe secret of the application. The setup script prints it once, when it creates the application.
REDIRECT_URIOptional. Where LumoAuth sends the browser after sign-in. The default is http://localhost:3000/callback. Set it before the setup script too.

The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.

node oauth/pushed-authorization-requests/setup.mjs
setup.mjs
// Creates what the recipe needs: the confidential application "Northwind
// Payroll", a web app with a server. Run it again at any time. The client
// secret is shown once, when the application is created: add --new-secret to
// replace a secret that you lost.
//
// node oauth/pushed-authorization-requests/setup.mjs
//
// The API key needs the scopes admin:clients:read and admin:clients:write.
import { call, list, printEnv } from '../../_lib/admin.mjs';

// Where LumoAuth sends the browser after sign-in. The programs listen there.
const redirectUri = process.env.REDIRECT_URI ?? 'http://localhost:3000/callback';

const name = 'Northwind Payroll';
let client = (await list('/admin/clients')).find((c) => c.name === name);
let secret;
if (!client) {
const created = await call('POST', '/admin/clients', {
name,
// The pushed request is sent from the server, with the client secret.
isConfidential: true,
grantTypes: ['authorization_code'],
redirectUris: [redirectUri],
scopes: ['openid', 'profile'],
});
client = created.data;
secret = client.secret;
} else if (process.argv.includes('--new-secret')) {
secret = (await call('POST', `/admin/clients/${client.id}/rotate-secret`)).data.secret;
}

console.log(`The application "${name}" is ready. Turn on "Require PAR" for it in the portal.`);
printEnv({ CLIENT_ID: client.clientId, ...(secret ? { CLIENT_SECRET: secret } : {}), REDIRECT_URI: redirectUri });
if (!secret) console.log('# CLIENT_SECRET was printed when the application was created. Add --new-secret to get a new one.');

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 program prints two addresses and waits after each one. Open the first one and sign in, then open the second one in the same browser. The curl version asks you to paste the address that the browser ends on, and prints the answers of the server.

oauth/pushed-authorization-requests/js/par.mjs
// Push the authorization request from the server, then send the browser to
// LumoAuth with a reference to it. The address bar shows no scope and no
// redirect URI.
//
// node oauth/pushed-authorization-requests/js/par.mjs
import { EventEmitter, once } from 'node:events';
import http from 'node:http';
import { AuthModule, generateCodeChallenge, generateCodeVerifier, generateState } from '@lumoauth/backend';

const baseUrl = process.env.LUMOAUTH_URL ?? 'https://app.lumoauth.dev';
const oauth = `${baseUrl}/orgs/${process.env.LUMOAUTH_ORG_ID}/api/v1/oauth`;
const redirectUri = process.env.REDIRECT_URI ?? 'http://localhost:3000/callback';
const { CLIENT_ID: clientId, CLIENT_SECRET: clientSecret } = process.env;

// The redirect URI of the application. Each request to it answers one
// authorization request.
const callbacks = new EventEmitter();
const server = http.createServer((request, response) => {
response.end('Done. You can close this tab.');
callbacks.emit('callback', new URL(request.url, redirectUri).searchParams);
});
server.listen(new URL(redirectUri).port);

// The authorization request. Without PAR, all of it would be in the address bar.
const codeVerifier = generateCodeVerifier();
const state = generateState();
const parameters = {
response_type: 'code',
client_id: clientId,
redirect_uri: redirectUri,
scope: 'openid profile',
state,
code_challenge: await generateCodeChallenge(codeVerifier),
code_challenge_method: 'S256',
};

// 1. The server posts the request to the PAR endpoint, with the client secret.
// No SDK has a call for this endpoint, so it is a plain POST.
const pushed = await fetch(`${oauth}/par`, {
method: 'POST',
headers: { Authorization: `Basic ${btoa(`${clientId}:${clientSecret}`)}` },
body: new URLSearchParams(parameters),
});
if (pushed.status !== 201) throw new Error(`The pushed request failed: ${await pushed.text()}`);
const { request_uri: requestUri, expires_in: expiresIn } = await pushed.json();
console.log(`pushed: HTTP ${pushed.status}, the reference is valid for ${expiresIn} s`);

// 2. The browser carries the client ID and the reference, nothing else.
const url = `${oauth}/authorize?${new URLSearchParams({ client_id: clientId, request_uri: requestUri })}`;
console.log(`Open this address in a browser and sign in:\n${url}\n`);
console.log(`the address holds: ${[...new URL(url).searchParams.keys()].join(', ')}`);

// 3. The rest is the authorization code flow.
const [callback] = await once(callbacks, 'callback');
if (callback.get('state') !== state) throw new Error('The state does not match.');
const auth = new AuthModule({ baseUrl, orgId: process.env.LUMOAUTH_ORG_ID, clientId });
const tokens = await auth.exchangeCodeForTokens({ code: callback.get('code'), codeVerifier, redirectUri, clientSecret });
console.log(`signed in: scope ${tokens.scope}`);

// 4. The reference works once: LumoAuth answers the same address with an error page.
const again = await fetch(url, { redirect: 'manual' });
console.log(`the same address again: HTTP ${again.status}`);

// 5. The application requires PAR, so a request with the parameters in the
// address is refused. LumoAuth sends the error to the redirect URI.
console.log(`Open this address in the same browser:\n${oauth}/authorize?${new URLSearchParams(parameters)}\n`);
const [refusal] = await once(callbacks, 'callback');
console.log(`a request without PAR: ${refusal.get('error')}`);
server.close();
server.closeAllConnections();
node oauth/pushed-authorization-requests/js/par.mjs

No SDK has a call for the PAR endpoint, so the Node.js version posts the request with fetch. It uses @lumoauth/backend for the PKCE values and the code exchange.

Check the result​

Output
pushed: HTTP 201, the reference is valid for 600 s
the address holds: client_id, request_uri
signed in: scope openid profile
the same address again: HTTP 400
a request without PAR: invalid_request
  • The first address holds two parameters. Everything else, from the scope to the PKCE challenge, is stored at LumoAuth for 600 seconds.
  • The refusal of the second address comes after the person has signed in. LumoAuth then sends error=invalid_request to the redirect URI, with a description that asks for PAR.
  • Turn off Require PAR and open the second address again. The person now gets to the redirect URI with a code.

Limits​

  • Only a confidential application can push a request: the PAR endpoint needs client authentication.
  • A pushed request expires after 600 seconds, and it works for one sign-in only. Push a new request for each sign-in.
  • The request_uri belongs to the application that pushed it. A redirect with the client ID of another application is refused.
  • The PAR endpoint accepts 60 pushed requests per minute from one IP address.
  • PAR is part of the FAPI 2.0 profile. An application with that profile requires PAR without the switch.