Skip to content
Workforce IdentityMarkdown

Client credentials

A back-end service gets a token for itself, with no user and no browser involved, and presents it to the API it calls.

AvailableAPIRFC 6749 §4.4, RFC 9068

A service proves who it is to the realm and gets an access token about itself. No user signs in and no browser is involved: the service presents the token to the API it calls, and the API sees which service is calling and what it may do.

Use it for service-to-service calls: a nightly job, a back end calling another back end, a webhook handler. Never use it in a browser or a mobile app, because the service must keep a secret or a private key. For access on a user’s behalf, use the browser sign-in or token exchange. An agent’s client uses this same grant, and its token is then about the agent.

  • Who can do it: a realm administrator (a group holding RealmOwner) registers the service; RealmViewer can see it but not change it. The service then asks for its own tokens.
  • Where: today this is done through the API (Call the API shows how to get $CVYUH_SESSION).
  • First: the service can keep a secret or a private key, and you know the API it calls.

The service asks the token endpoint, proving who it is (client authentication) and naming the API it will call:

Terminal window
curl https://foobar.cvyuh.com/oauth2/token \
-u orders-sync:s6BhdRkqt3 \
-d grant_type=client_credentials \
-d scope=orders.read \
--data-urlencode resource=https://orders.example.com/api
{
"access_token": "eyJhbGciOiJFUzI1NiIs…",
"token_type": "Bearer",
"expires_in": 300
}

scope is in the answer only when it differs from what was asked, such as when nothing was asked and the client’s defaults were granted. The access token’s claims:

{
"iss": "https://foobar.cvyuh.com/oauth2",
"sub": "orders-sync",
"aud": "https://orders.example.com/api",
"exp": <iat + 300>,
"iat": <unix time>,
"jti": "b1f7c0d2-5e3a-4a8f-9c61-2d4e7f8a9b10",
"client_id": "orders-sync",
"principal_type": "oauth2Client",
"scope": "orders.read"
}
ClaimWhat it says
sub, client_idThe client, because the token is about the client
audThe API it is for (resource indicators)
scopeWhat it allows
principal_typeThat it names a client, so an API never mistakes it for a user

There is no refresh token and no ID token: the service asks again when the token expires.

Register the service with POST https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=orders-sync (see Call the API), setting the fields below; realm settings are changed with PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings.

WhereSettingWhat it does
ClientgrantTypes includes client_credentialsThe default for a new client
ClienttokenEndpointAuthMethod and its secret or keyHow the service proves who it is (client authentication)
Clientscopes, defaultScopesWhat it may ask for, and what it gets when it asks for nothing
ClientallowedResources, defaultAudienceThe APIs its tokens may be for, and the one used when the request names none
RealmgrantTypes includes client_credentialsOn by default
RealmrequireResourceEvery request must name its API (default off)
RealmaccessTokenLifetimeSecsHow long access tokens live (default 300)
Setting or limitDefaultRange
client_credentials in a new client’s grantTypesyesper client
requireResourceoffon or off
accessTokenLifetimeSecs3001 to 86,400; an agent’s token may be capped lower by its risk class
Refresh tokensnone for this grantfixed
SymptomCauseFix
invalid_scopeThe scope isn’t held by both the client and the realm, or it’s openidAdd the scope to both lists; drop openid
invalid_targetNo resource and no defaultAudience, or requireResource is onSend resource, or set defaultAudience
unauthorized_clientThe client is public (none)Give it a secret or a key
A new token on every callThe service doesn’t keep its token until it expiresCache the token, and ask again shortly before expires_in

Each request is one TOKEN_ISSUED record with grantType client_credentials, under the client (or the agent, for an agent’s client), issued or refused with its error. See them in Audit → Authentication.

  • Scopes must be held by both the client and the realm. Any other is refused invalid_scope. With none asked, the client’s defaults the realm holds are granted. openid is refused: no user signed in.
  • Every token names an API. A request that names no resource gets the client’s defaultAudience. A client with no defaultAudience must name one, and so must every request when the realm sets requireResource. Otherwise the request is refused invalid_target.
  • Structured permissions are allowed. A request may carry authorization_details, of the types the client may ask for (rich authorization requests).
  • A public client can never use this grant. It’s refused unauthorized_client.
  • For an agent’s client, the realm’s agent guardrails apply. A scope a guardrail holds for the business owner’s approval is refused interaction_required, or waits for the owner when the request asks to (the request waits for the owner). The token lives no longer than the agent’s risk class allows.
ErrorWhen
invalid_client (401)The client didn’t prove who it is
unauthorized_clientThe client is public, or may not use this grant
invalid_scopeA scope the client or the realm doesn’t hold, or openid
invalid_targetA resource the client may not name; or none, with no default or under requireResource
interaction_requiredAn agent’s client asked for a scope its owner must approve first