Skip to main content

Prove where a client runs

Let a client show a signed statement about its app or device when it authenticates.

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
Plain HTTP

What it shows​

  1. Each installed copy of the Acme Field app makes its own key pair. The private key never leaves the copy.
  2. Your attester, a backend of yours, vouches for the copy and its public key. It signs a Client Attestation JWT that is valid for one hour.
  3. For each token request, the copy signs a proof with its own key. The proof works once.
  4. The copy sends both JWTs in the headers OAuth-Client-Attestation and OAuth-Client-Attestation-PoP, in place of a client secret.
  5. LumoAuth verifies the attestation with the keys of the attester, and the proof with the key in the attestation.

No copy of the app holds a shared secret, and each copy has a key of its own.

Before you start​

  • An API key for the setup script, with the scopes admin:permissions:read, admin:permissions:write, admin:clients:read and admin:clients:write.
  • A confidential application with the client credentials grant. The setup script creates the application "Acme Field app", the scope reports:send, and the key pair of the attester.
  • A public https address for the key set of the attester, for example https://attester.example.com/.well-known/jwks.json. Publish the line {"keys":[…]} that the setup script prints there. LumoAuth reads the address itself, so it cannot be a local file or a private address.
  • Attestation turned on for the application. The Admin API cannot change it, so use the portal:
    1. Open Applications & developer tools, Applications, and select Acme Field app.
    2. Open the Security tab and go to FAPI 2.0 and client authentication.
    3. Turn on Attestation-based client auth (attest_jwt_client_auth).
    4. Enter the iss of your attester as Attester issuer, and the address of its key set as Attester JWKS URI.
    5. Select Save FAPI settings.
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:permissions:read, admin:permissions:write, admin:clients:read and admin:clients:write.
ATTESTER_KEY_FILEFor the setup script only, and optional: the file that holds the private key of the attester. Default: acme-attester-key.pem in your home folder.
CLIENT_IDThe client ID of the application. The setup script prints it.
ATTESTER_ISSUERThe iss of the attestations: the address of your attester. Default: https://attester.example.com. Set it before the setup script.
ATTESTER_KEY_IDThe ID (kid) of the key of the attester. The setup script prints it.
ATTESTER_PRIVATE_KEYThe private key of the attester, as PEM text. The setup script prints a line that reads it from the key file. In production, only your attester backend holds it.

The setup script creates the key pair of the attester, writes the private key to a file outside the repository, and creates the application. It prints the public key set that your attester publishes.

node oauth/client-attestation/setup.mjs
setup.mjs
// Creates what the recipe needs: the key pair of your attester, the scope of
// the reports API, and the confidential application "Acme Field app". Run it
// again at any time: it keeps the key file and the application that exist.
//
// node oauth/client-attestation/setup.mjs
//
// The private key of the attester is written to the file that
// ATTESTER_KEY_FILE names. Without that variable, the file is
// acme-attester-key.pem in your home folder. The API key needs the scopes
// admin:permissions:read, admin:permissions:write, admin:clients:read and
// admin:clients:write.
import { createHash, createPublicKey, generateKeyPairSync } from 'node:crypto';
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { ensure, printEnv } from '../../_lib/admin.mjs';

const keyFile = process.env.ATTESTER_KEY_FILE ?? join(homedir(), 'acme-attester-key.pem');
// The `iss` of the attestations. Use the address of your attester.
const attesterIssuer = process.env.ATTESTER_ISSUER ?? 'https://attester.example.com';

// The attester is your backend: it vouches for each installed copy of the
// app. An RSA key, so that the curl version can sign with openssl.
if (!existsSync(keyFile)) {
const pem = generateKeyPairSync('rsa', { modulusLength: 2048 }).privateKey.export({ type: 'pkcs8', format: 'pem' });
writeFileSync(keyFile, pem, { mode: 0o600 });
}
const { kty, n, e } = createPublicKey(readFileSync(keyFile, 'utf8')).export({ format: 'jwk' });
const kid = createHash('sha256').update(JSON.stringify({ e, kty, n })).digest('base64url');

await ensure('/admin/scopes', (s) => s.name === 'reports:send', { name: 'reports:send', description: 'Send field reports.' });
const client = await ensure('/admin/clients', (c) => c.name === 'Acme Field app', {
name: 'Acme Field app',
isConfidential: true,
grantTypes: ['client_credentials'],
scopes: ['reports:send'],
});

console.log('The application "Acme Field app" is ready. Publish this key set at a public https');
console.log('address of your attester, then turn on attestation-based client auth in the portal:\n');
console.log(JSON.stringify({ keys: [{ kty, n, e, kid, alg: 'RS256', use: 'sig' }] }));
printEnv({
CLIENT_ID: client.clientId,
ATTESTER_ISSUER: attesterIssuer,
ATTESTER_KEY_ID: kid,
ATTESTER_PRIVATE_KEY: `$(cat '${keyFile}')`,
});

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. The first line was not seen: it needs the key set of the attester at a public https address. The second line is the answer to a proof that is sent twice.

oauth/client-attestation/http/attest.sh
#!/bin/sh
# One installed copy of the Acme Field app gets an attestation from your
# attester, and authenticates with it at the token endpoint, with curl.
#
# sh oauth/client-attestation/http/attest.sh
#
# The script needs curl, openssl, xxd and jq.
set -eu

ISSUER="$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1"
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
printf '%s\n' "$ATTESTER_PRIVATE_KEY" > "$WORK/attester.pem"

b64url() { openssl base64 -A | tr '+/' '-_' | tr -d '='; }

# A JWT signed with RS256: header, claims, key file.
sign() {
HEADER=$(printf '%s' "$1" | b64url)
CLAIMS=$(printf '%s' "$2" | b64url)
SIGNATURE=$(printf '%s.%s' "$HEADER" "$CLAIMS" | openssl dgst -sha256 -sign "$3" | b64url)
printf '%s.%s.%s' "$HEADER" "$CLAIMS" "$SIGNATURE"
}

# 1. The app instance makes its own key pair. The private key stays in the
# instance; the attester sees only the public key.
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$WORK/instance.pem" 2>/dev/null
MODULUS=$(openssl rsa -in "$WORK/instance.pem" -noout -modulus | cut -d= -f2 | xxd -r -p | b64url)
INSTANCE_JWK=$(printf '{"kty":"RSA","e":"AQAB","n":"%s"}' "$MODULUS")

# 2. The attester checks the instance (on a phone: with the attestation of
# the platform) and vouches for it and its key for one hour.
NOW=$(date +%s)
ATTESTATION=$(sign \
"$(printf '{"alg":"RS256","kid":"%s","typ":"oauth-client-attestation+jwt"}' "$ATTESTER_KEY_ID")" \
"$(printf '{"iss":"%s","sub":"%s","iat":%s,"exp":%s,"cnf":{"jwk":%s}}' "$ATTESTER_ISSUER" "$CLIENT_ID" "$NOW" "$((NOW + 3600))" "$INSTANCE_JWK")" \
"$WORK/attester.pem")

# 3. For each request, the instance signs a proof with its own key. The
# audience is the issuer of the organization, and the proof works once.
proof() {
NOW=$(date +%s)
sign '{"alg":"RS256","typ":"oauth-client-attestation-pop+jwt"}' \
"$(printf '{"iss":"%s","aud":"%s","iat":%s,"exp":%s,"jti":"%s"}' "$CLIENT_ID" "$ISSUER" "$NOW" "$((NOW + 60))" "$(openssl rand -hex 16)")" \
"$WORK/instance.pem"
}

token_request() {
curl --silent --show-error "$ISSUER/oauth/token" \
--header "OAuth-Client-Attestation: $ATTESTATION" \
--header "OAuth-Client-Attestation-PoP: $1" \
--data "grant_type=client_credentials" \
--data "client_id=$CLIENT_ID" \
--data "scope=reports:send" \
--write-out ' HTTP %{http_code}\n' | sed 's/"access_token":"[^"]*"/"access_token":"…"/'
}

# 4. The two headers take the place of a client secret.
PROOF=$(proof)
token_request "$PROOF"

# 5. The same proof again: refused, because each proof works once.
token_request "$PROOF"
sh oauth/client-attestation/http/attest.sh

Compiles. Not run against an organization. Ran against an organization, and LumoAuth answered 401 to both requests: the key set of the attester was not at a public https address that LumoAuth could read. Both JWTs passed the JOSE checks of LumoAuth offline.

No SDK builds these JWTs, so the script signs them with openssl. In the script, one shell plays both parts. In production, only your attester holds its private key, and only the copy of the app holds its own.

Check the result​

Output
{"access_token":"…","token_type":"Bearer","expires_in":3600,"scope":"reports:send"} HTTP 200
{"error":"invalid_client","error_description":"Client authentication failed"} HTTP 401
  • Every refusal is the same 401 invalid_client. When both headers are present, LumoAuth never falls back to another method for that request.
  • The attestation names the application in sub and the key of the copy in cnf.jwk. The proof names the application in iss and the issuer of the organization in aud.
  • LumoAuth keeps the key set of the attester for 15 minutes.

Limits​

  • The token request was not seen to succeed when this page was checked, because the key set of the attester was not at a public address. The page stays a draft until it is.
  • The switch adds attestation next to the method of the application, so the client secret still works too. To refuse the secret, set Client authentication method to Private key JWT (private_key_jwt), or choose a FAPI 2.0 profile.
  • A proof may be valid for 10 minutes at most.