Skip to content

Keycloak token exchange: "Client is not within the token audience"

Identitytested on Ubuntu 24.04, k3s v1.37.2+k3s1, Keycloak 26.8.2

Anna is logged in, her token is fresh, and still Keycloak rejects the exchange. The AI assistant may only redeem a delegation that was issued for it. Anna's token, however, comes from a login at a different client, the shop till.

Symptom

python3 ~/kurs/11/assistent.py ~/kurs/11/anna-kasse.jwt "Wie viele Kisten sind im Lager?"
[Empfang] 403 {'error': 'access_denied', 'error_description': 'Client is not within the token audience'}

The message comes from the front desk (Keycloak), not from the warehouse – the exchange failed.

Diagnosis

Keycloak writes the reason in more detail to the journal. Then look into the token.

sudo journalctl -u keycloak --since "-2 min" --no-pager --grep TOKEN_EXCHANGE_ERROR
… type="TOKEN_EXCHANGE_ERROR", realmName="gebaeude", clientId="assistent",
  reason="client is not within the token audience", auth_method="token_exchange", …
  subject_token_client_id="ladenkasse-cli", … username="anna"

step crypto jwt inspect --insecure < ~/kurs/11/anna-kasse.jwt

Causes

audThe token is for a different client

The till's token contains "aud": "account" and "azp": "ladenkasse-cli". No assistent in aud – so the assistant may not exchange it.

IntentThe check is a barrier, not a bug

Without it, any program Anna ever gave a token to could use it to reach everything the assistant can reach.

MapperOnly an audience mapper makes a token exchangeable

The assistant's login client adds the client assistent to aud with the mapper fuer-assistent. The till deliberately does not get this mapper.

/opt/keycloak/bin/kcadm.sh create clients/ac58d634-f276-4f07-a606-0767e981ad19/protocol-mappers/models -r gebaeude -s name=fuer-assistent -s protocol=openid-connect -s protocolMapper=oidc-audience-mapper -s 'config."included.client.audience"=assistent' -s 'config."access.token.claim"=true'

Fix

Anna logs in at the assistant (assistent-cli). Its tokens carry assistent in aud.
curl -s http://172.16.0.126:8080/realms/gebaeude/protocol/openid-connect/token -d client_id=assistent-cli -d grant_type=urn:ietf:params:oauth:grant-type:device_code --data-urlencode device_code@$HOME/kurs/11/geraet-anna-code.txt -o ~/kurs/11/anmeldung-anna.json -w "%{http_code}\n"
jq -r .access_token ~/kurs/11/anmeldung-anna.json > ~/kurs/11/anna.jwt
step crypto jwt inspect --insecure < ~/kurs/11/anna.jwt
    "aud": [
      "assistent",
      "account"
    ],
    "azp": "assistent-cli",

Don't: Attach the fuer-assistent mapper to every client so that "it works". Then every token can be exchanged and the barrier is gone.

▶
Break it yourself and find it – free
Workload and AI agent identity hands-on, day 11: OAuth token exchange – the AI assistant acts on behalf of the human.
Go to day 11 →

FAQ

What does Keycloak check during standard token exchange?

Among other things, whether the requesting client is in aud of the subject token. If it is missing, the answer is access_denied with "Client is not within the token audience".

Where do I find the client the token was issued for?

The journal shows subject_token_client_id, the token itself shows azp.

Related error patterns

More