NHI / Machine-to-Machine Demo

This page walks through OAuth 2.0 Client Credentials against Okta — the flow for headless agents, CI/regression jobs, and service-to-service calls with zero human authentication anywhere in the chain. Edit the fields on the left, then run each step on the right in order.

Configuration

What each piece is for

Authorization Server
Okta object that mints tokens and enforces which client can request which scope, via its access policy + rule.
Access Policy / Rule
Not shown here as a field, but it's what actually grants this Client ID the right to request this Scope with this Grant Type. Configured once in Okta admin, not per-call.
Resource Server
The API being protected (this same box, in this demo) — it independently validates the token's signature, issuer, audience, and scope before doing anything. Okta doesn't do this validation on the resource's behalf.
human_user_involved: false
The field the resource returns to prove no person authenticated anywhere in this chain.
Bearer vs. private_key_jwt — not the same layer
"Bearer" (Step 3) describes how the access token is presented to the resource — whoever holds it can use it. It's identical either way below. client_secret_basic vs. private_key_jwt is a completely different fork, one step earlier: how the client proves its identity to Okta in Step 2, to get that access token in the first place.

1Call the resource with no token

not run

Proves the endpoint is actually protected before we do anything else.

Browser (this button's click handler)

fetch('/api/regression/run', { method: 'POST' })

Backend (server.js — Express route)

app.post('/api/regression/run', async (req, res) => {
  const authHeader = req.headers['authorization'] || '';
  const match = authHeader.match(/^Bearer\s+(.+)$/i);
  if (!match) {
    return res.status(401).json({ error: 'missing_token', detail: 'No Bearer token supplied' });
  }
  // ... (see Step 3 for what runs once a token IS present)
});

2Request a token from Okta

not run

Backend does POST https://{Okta Domain}/oauth2/{AS ID}/v1/token with grant_type=client_credentials and scope=..., authenticated using whichever method is selected on the left. No browser, no login page, no human — this call is entirely agent-to-Okta. The response's scp, aud, and sub claims below confirm the client — not a person — is the identity in the token.

Browser (this button's click handler)

fetch('/api/demo/token', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    oktaDomain, authServerId, clientId, clientSecret, grantType, scope, authMethod
  }),
})

Backend — path A: client_secret_basic

const basic = Buffer.from(`${clientId}:${clientSecret}`).toString('base64');

const oktaRes = await fetch(tokenUrl, {
  method: 'POST',
  headers: {
    Authorization: `Basic ${basic}`,
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: new URLSearchParams({ grant_type: 'client_credentials', scope }),
});

Backend — path B: private_key_jwt (server signs its own assertion)

// The client builds and signs a JWT ASSERTING its own identity —
// no secret is sent anywhere, only a signature Okta verifies
// against the public key already registered as this client's JWKS.
function buildClientAssertion({ clientId, tokenUrl, privateKeyPem, kid }) {
  const header  = { alg: 'RS256', kid, typ: 'JWT' };
  const payload = {
    iss: clientId, sub: clientId, aud: tokenUrl,
    iat: now, exp: now + 300, jti: crypto.randomUUID(),
  };
  const signingInput = `${b64url(header)}.${b64url(payload)}`;
  const signature = crypto.createSign('RSA-SHA256')
    .update(signingInput).sign(privateKeyPem);
  return `${signingInput}.${b64url(signature)}`;
}

const clientAssertion = buildClientAssertion({ clientId, tokenUrl, privateKeyPem, kid });

const oktaRes = await fetch(tokenUrl, {
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  body: new URLSearchParams({
    grant_type: 'client_credentials',
    scope,
    client_assertion_type: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
    client_assertion: clientAssertion,
  }),
});

Equivalent raw curl — path A

curl -X POST "https://{oktaDomain}/oauth2/{authServerId}/v1/token" \
  -H "Authorization: Basic base64(clientId:clientSecret)" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=client_credentials&scope=regression.tests.run"

Equivalent raw curl — path B

curl -X POST "https://{oktaDomain}/oauth2/{authServerId}/v1/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=client_credentials" \
  -d "scope=regression.tests.run" \
  -d "client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer" \
  -d "client_assertion=<signed JWT>"

3Call the resource WITH the token

not run

Same call as step 1, now carrying Authorization: Bearer <token>. The resource validates it independently and, if valid, executes the action.

Browser (this button's click handler)

fetch('/api/regression/run', {
  method: 'POST',
  headers: { 'Authorization': 'Bearer ' + lastToken },
})

Backend (server.js — validates the token independently of Okta)

const JWKS = createRemoteJWKSet(new URL(`${ISSUER}/v1/keys`));

app.post('/api/regression/run', async (req, res) => {
  const token = req.headers['authorization'].replace(/^Bearer\s+/i, '');

  // Signature, issuer, and audience are all checked here — the resource
  // server trusts nothing from Okta blindly, it verifies on every call.
  const { payload } = await jwtVerify(token, JWKS, {
    issuer: ISSUER,          // must match this AS exactly
    audience: AUDIENCE,      // must match "api://nhi-demo"
  });

  if (!payload.scp?.includes('regression.tests.run')) {
    return res.status(403).json({ error: 'insufficient_scope' });
  }

  return res.json({
    status: 'regression suite triggered',
    triggered_by_client: payload.cid || payload.sub,  // no human "sub" — it's the client itself
    human_user_involved: false,
  });
});