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.
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.Proves the endpoint is actually protected before we do anything else.
fetch('/api/regression/run', { method: 'POST' })
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)
});
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.
fetch('/api/demo/token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
oktaDomain, authServerId, clientId, clientSecret, grantType, scope, authMethod
}),
})
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 }),
});
// 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,
}),
});
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"
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>"
Same call as step 1, now carrying Authorization: Bearer <token>. The resource validates it independently and, if valid, executes the action.
fetch('/api/regression/run', {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + lastToken },
})
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,
});
});