Let one service call another
Let a back-end service call another's API with a short-lived token from the realm, with no user involved.
APIRFC 6749 §4.4, RFC 8707
A report builder needs to call your reports API on a schedule, with no user signed in. With the client-credentials grant, the calling service gets a short-lived token from the realm and presents it to the API, which knows exactly which service is calling and what it may do. No shared passwords between services, and nothing that lasts longer than a few minutes travels with each call. This guide is for back-end services, scheduled jobs and integrations.
Before you start:
- Who can do it: a realm administrator, in a group that holds
RealmOwner(Delegated administration).RealmViewercan read the settings but not change them. - Where: today this is done through the API; Call the API shows how to get
$CVYUH_SESSION. - First: know the API’s audience, such as
https://reports.example.com/api, and the scopes it checks.
Set it up
Section titled “Set it up”-
Offer the API’s scopes in the realm: add them to the realm’s
scopes, as in step 1 of Sign users in to a web app. -
Register the calling service:
Terminal window curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=report-builder' \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "status": "active","clientName": "Report Builder","tokenEndpointAuthMethod": "client_secret_basic","clientSecret": "<a long random secret>","grantTypes": ["client_credentials"],"scopes": ["reports:read"],"defaultScopes": ["reports:read"],"allowedResources": ["https://reports.example.com/api"],"defaultAudience": "https://reports.example.com/api" }'For a stronger credential than a secret, see Choose how a client proves itself.
-
Give the service its client id, its secret and the token address,
https://foobar.cvyuh.com/oauth2/token. -
The service asks for a token whenever it needs one:
Terminal window curl https://foobar.cvyuh.com/oauth2/token \-u '<client-id>:<secret>' \-d grant_type=client_credentials \-d scope=reports:read \-d resource=https://reports.example.com/apiIt receives an access token for five minutes, and asks again when it expires. There’s no refresh token.
Check it worked
Section titled “Check it worked”The API receives a signed token (typ: at+jwt) with iss the realm (https://foobar.cvyuh.com/oauth2), aud its own
address, sub and client_id the calling service, scope what it may do, and principal_type showing a client,
not a user. It checks the signature with the realm’s published keys
(see Check what clients discover), or asks the realm, as in
Check a token at your API.
Each token the service gets is on the trail as a TOKEN_ISSUED record under the client, with grantType
client_credentials and what was granted. Find it in Audit → Authentication in the dashboard.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
invalid_client | Wrong ID or secret, the client is inactive, or it is locked out after wrong secrets | Check the credentials and status |
invalid_scope | A scope isn’t in the client’s scopes, or the realm doesn’t offer it | Add it to both (step 1 and 2) |
invalid_target | The resource isn’t one of the client’s allowedResources | Add it, or ask for the default |
unauthorized_client | The client lacks client_credentials, or is public | Add the grant; a public client can’t use it |
| The API refuses the token | Its aud isn’t the API’s address, or it expired | Ask with resource for that API; ask again after five minutes |
Undo it
Section titled “Undo it”Set the client’s status to inactive. Its next token request is refused.
Related
Section titled “Related”- Client credentials: the grant in full.
- Give a client tokens for one API: keep each token to one API.
- Choose how a client proves itself: a key instead of a secret.