One client per calling service
Create an OpenID Connect client for each service, never one shared by several. Turn Client authentication on and Service accounts roles on, and turn Standard flow off: nobody signs in to this client through a browser. The service then asks for a token:
curl -s https://auth.yourcompany.com/realms/services/protocol/openid-connect/token \
-d grant_type=client_credentials \
-d client_id=orders-worker \
-d client_secret="$CLIENT_SECRET"The answer is a signed JWT access token with no refresh token. Cache it and ask again shortly before it expires, rather than asking for every call. The token request builder writes the request for each grant.
Roles, scopes and audience
A client with service accounts has a service account user. Give it roles on its Service accounts roles tab: client roles of the API it calls, such as orders:read, rather than broad realm roles.
APIs should refuse tokens meant for someone else. Add an Audience mapper to a client scope, pointing at the API’s client, so the token’s aud names that API. Keep access tokens short, a few minutes, so a leaked token is soon useless.
Validating tokens in the API
Each API checks the signature with the realm’s keys, published at /realms/services/protocol/openid-connect/certs, and caches them. Then it checks iss, aud, exp and the scope or role the call needs. Most JWT libraries do this from the realm’s discovery document. When an API needs to know a token is still active, it calls the introspection endpoint instead.
The JWT verifier and the JWKS viewer let you check a token and the keys by hand.
Calling another service for a user
When the Orders API must call the Billing API for the user who called it, it should not forward that user’s token as is. Standard token exchange (RFC 8693), supported since Keycloak 26.2, swaps it for a token meant for the next service:
grant_type=urn:ietf:params:oauth:grant-type:token-exchange
subject_token=<the user’s access token>
subject_token_type=urn:ietf:params:oauth:token-type:access_token
audience=billing-apiTurn on Standard token exchange in the requesting client’s settings. The audience parameter narrows the new token to the service named. Standard token exchange covers exchanges within one realm. The older token exchange (version 1), which also did impersonation and exchanges with external providers, is a deprecated preview feature, off by default, and our clusters run Keycloak’s defaults.
Keeping client credentials safe
- Prefer a signed JWT (
private_key_jwt) to a shared secret where the service can keep a private key. Upload its public key or certificate to the client, so Keycloak needs no outbound call to check it. - Rotate secrets without downtime: client secret rotation, supported since Keycloak 26.8, keeps up to two secrets active while services move to the new one.
- Bind tokens to a key with DPoP, supported since Keycloak 26.4, when a stolen token must be useless without the key.
Mutual TLS client authentication needs the client certificate to reach Keycloak, and TLS ends at our gateway, so ask us before you plan on it.