Skip to content
Workforce IdentityMarkdown

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). RealmViewer can 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.
  1. 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.

  2. 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.

  3. Give the service its client id, its secret and the token address, https://foobar.cvyuh.com/oauth2/token.

  4. 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/api

    It receives an access token for five minutes, and asks again when it expires. There’s no refresh token.

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.

SymptomCauseFix
invalid_clientWrong ID or secret, the client is inactive, or it is locked out after wrong secretsCheck the credentials and status
invalid_scopeA scope isn’t in the client’s scopes, or the realm doesn’t offer itAdd it to both (step 1 and 2)
invalid_targetThe resource isn’t one of the client’s allowedResourcesAdd it, or ask for the default
unauthorized_clientThe client lacks client_credentials, or is publicAdd the grant; a public client can’t use it
The API refuses the tokenIts aud isn’t the API’s address, or it expiredAsk with resource for that API; ask again after five minutes

Set the client’s status to inactive. Its next token request is refused.