Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 15 min read

Who is the agent acting for? A customer's sign-in across Google Agent Platform and Microsoft Foundry, one token per hop

As of November 2026. This is a proof of concept, not a production system. All identifiers in this post are examples. This is the third post about a proof of concept with two AI agents. Agent A runs on Azure, in Microsof

Who is the agent acting for? A customer's sign-in across Google Agent Platform and Microsoft Foundry, one token per hop

As of November 2026. This is a proof of concept, not a production system. All identifiers in this post are examples.

This is the third post about a proof of concept with two AI agents. Agent A runs on Azure, in Microsoft Foundry. Agent B runs on Google Cloud, in Agent Runtime. The first post gave each agent an identity with no stored secret. The second gave them models with no API key.

Both posts answered "who is the agent?". This one answers the next question: who is the agent acting for, and what may it do for that person?

A customer signs in and asks for their account balance. Agent B receives the question and hands it to Agent A, which calls the accounts API. Four parties handle that request, and each one receives a different token.

Figure 1: the path of a customer's request

The problem with the usual approaches

There are two quick ways to do this, and both go wrong.

Forward the customer's token. The agent passes on the token it received. Now every agent on the path holds a token that works everywhere the customer's token works, for everything the customer may do. If a malicious instruction tricks one agent, that agent can replay the token anywhere.

Give the agents their own access. The agents call the accounts API with a service identity that can read every account. The customer is now just a parameter in the request. The API cannot tell whether the customer really asked, and an agent that hallucinates a customer number gets real data back.

We wanted the accounts API to receive proof of three things on every call: which customer this is, which agents are acting for them, and that this call is within what those agents may do.

The idea

Four rules carry the design.

  1. No token is forwarded to a new destination. Whoever passes a request on swaps the token for a new one that only works at the next destination. The swap is OAuth 2.0 Token Exchange (RFC 8693).
  2. At the entry, the gateway swaps. The customer's sign-in token is addressed to the gateway and stops there. The gateway exchanges it for a token addressed to the first agent.
  3. After that, whoever leaves asks, as itself. An agent or a service that hands the request on exchanges the token it received. It signs in to the customer identity provider (the CIAM) with its own platform identity, with no secret. On those hops the gateway checks the token and does not exchange it.
  4. The scope only shrinks. What an agent may do for the customer is the customer's rights, cut down to what the organisation allows that agent. No exchange can add a scope.

The customer never sees a consent screen that lists agents. The organisation decides what each agent may do for customers, as a matter of policy, the same way it decides what its own mobile app may do.

In the proof of concept the CIAM is Keycloak 26 on Cloud Run. The requests are plain RFC 8693, but the rules on top of them are not Keycloak's default behaviour: they are a Keycloak extension we wrote (more on that below). The gateways are Apigee on Google Cloud and API Management on Azure.

Why the party that leaves asks

The gateway could do every exchange instead, presenting the agent's own Entra token as the actor (actor_token). RFC 8693 allows that too. We chose otherwise, for three reasons.

  • The token stays with its recipient. A token addressed to Agent A can only be exchanged by Agent A, authenticated as itself. Anyone else who gets hold of it, including a gateway, cannot turn it into a token for somewhere else. This is also the default rule in Keycloak's own token exchange, where the requester must be in the subject token's audience.
  • A compromised gateway can do less. When the gateway does every exchange, a gateway under an attacker's control can ask for tokens on behalf of every agent behind it. Now, on internal hops, it only sees tokens addressed to the next destination, and it cannot exchange them.
  • The actor proves itself where the token is issued. The CIAM no longer has to trust a gateway's claim that "this agent is acting". The agent authenticates to the CIAM directly.

The cost is that every agent, and every service that leaves its business domain, now talks to the CIAM and has a few lines of exchange code. The model never sees any of it (see "What this design asks of you").

The exchange, as the agent sends it

When Agent B hands the customer's request to Agent A, Agent B sends this:

POST https://ciam.example.com/realms/customers/protocol/openid-connect/token

grant_type=urn:ietf:params:oauth:grant-type:token-exchange
&client_id=agent-b
&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
&client_assertion=<Agent B's own Entra token, issued for the CIAM>
&subject_token=<the customer's token Agent B received>
&subject_token_type=urn:ietf:params:oauth:token-type:access_token
&requested_token_type=urn:ietf:params:oauth:token-type:access_token
&audience=agent-a

There is no actor_token. The client that authenticated is the actor. The CIAM refuses a request that carries one.

The client assertion is the identity from the first post, pointed at a new audience. Agent B gets an Entra token for the CIAM's own application, the same way it gets one for a gateway. That application requires a role assignment: an agent without the role gets AADSTS501051 from Entra and never reaches the CIAM. The CIAM checks the token's signature against Entra's keys, and checks that its subject is the agent's identity. No client secret exists anywhere in this chain. Services do the same with their own platform identity: the accounts API, on Cloud Run, presents a Google identity token issued for the CIAM.

Before it answers, the CIAM checks:

  • The client is a registered agent or service, enabled to act for customers, and it authenticated with its platform identity.
  • The customer's token is valid, and the customer's session is still open.
  • The customer's token was addressed to this client. For a service, it was addressed to that service's business domain. Nobody can exchange a token that was not given to them.
  • The requested destination is one this client may reach.
  • The chain of actors is not longer than the configured limit.
  • The scope rules hold (next sections).

At the entry, the gateway sends the same request with its own client and its own platform identity: Apigee's service account, or API Management's managed identity. The CIAM accepts only the customer's sign-in token there, addressed to that gateway and with nobody acting yet. A gateway that presents an actor is refused.

What the gateway does on the other hops

When Agent B calls Agent A through API Management, it sends two things: its Entra token, which authenticates the call as in the first post, and the token it just obtained from the CIAM for Agent A. The gateway does not exchange. It checks:

  • the customer token was issued by the CIAM, is signed and still valid;
  • it is addressed to this destination;
  • the caller is the actor written in it: the act claim names Agent B, and the Entra token says the caller is Agent B;
  • for a service, the scope covers the operation being called;
  • the customer has not signed out since the token was issued.

The first four checks are local. The last one is a question to the CIAM: the gateway asks, as itself, whether the token is still active (RFC 7662 token introspection). A signature cannot answer that, because a token stays correctly signed after the customer signs out. If the CIAM does not clearly answer "active", the request stops there.

A token handed on without a new exchange fails the second check. A token presented by someone other than its actor fails the third. A token obtained before the customer signed out fails the last. Both gateways answer with the reason, in the same shape:

{ "error": "customer_context_refused", "reason": "presenter_is_not_the_actor" }

Four tokens, claim by claim

One question produces four tokens. This is what each party sees:

Figure 2: the four tokens

Claim What it says Along the chain
sub The customer Never changes
aud The one place that accepts it Changes at every exchange
azp Who asked for this token The gateway at the entry, then whoever left
scope What may be done for the customer Can only shrink
act Who is acting for the customer Grows by one actor per hop
exp When it stops working Never later than the sign-in token

The token the accounts API receives:

{
  "sub": "customer-7f3a…",
  "aud": "accounts",
  "azp": "agent-a",
  "scope": "accounts.read",
  "act": {
    "client_id": "agent-a", "type": "agent",
    "act": { "client_id": "agent-b", "type": "agent" }
  },
  "sid": "0kSezak1…",
  "exp": 1795563127
}

A few details matter more than they seem.

sub is always the customer. No agent ever becomes the subject. The API answers for the customer in sub and for nobody else, so an invented customer number in the request body has no effect.

act is empty at the entry. When the customer calls Agent B through Apigee, nobody is acting for them yet. Agent B appears in act one hop later, when it asks for something in the customer's name. The claim is nested, newest actor first, and says which kind of actor each one is, an agent or a service.

aud is a business domain, not a microservice. The token for the accounts API is addressed to the domain accounts. Another service of the same domain accepts the same token as it is, with no exchange. Leaving the domain, for example to a payments service, means a new exchange by the service that leaves.

exp is inherited. Tokens last five minutes at most, and an exchanged token never outlives the one it came from. The whole chain expires with the sign-in token, so an agent cannot extend its own authorisation by exchanging again.

sid ties everything to the session. When the customer signs out, the CIAM refuses every further exchange, and the gateways refuse every token that was issued before. We tested both. An agent received a request, the customer signed out, and eight seconds later the agent asked for the next token while its own still had four minutes left: the CIAM refused, and nothing left the agent. In the other case the agent had already exchanged its token for the next hop and was holding it when the customer signed out. API Management refused it with customer_session_ended, nine times in a row.

Scopes: the customer may, the agent may not

The customer in our demo may read accounts and order transfers. The agents may only read.

ciam:
  domains:
    accounts: [read, transfer]

agents:
  - name: agent-a
    delegation_scopes: {accounts: [read]}
  - name: agent-b
    delegation_scopes: {accounts: [read]}

The scope of each token is an intersection:

token scope = what the customer may do
            ∩ the ceiling of every actor on the path
            ∩ what was asked for

A token for a service carries exactly the scope that was asked for, and the gateway checks it against the operation, taken from the service's own OpenAPI contract:

paths:
  /accounts:
    get:
      security: [{ciam: [accounts.read]}]
  /transfers:
    post:
      security: [{ciam: [accounts.transfer]}]

So the same transfer has two outcomes. Ordered by the customer, the gateway exchanges the sign-in token at the entry for one with accounts.transfer and no act, and the API accepts it. Asked through the agents, the CIAM refuses Agent A the token it needs, because accounts.transfer is above its ceiling. The request never leaves the agent.

The model can decide to try a transfer as often as it likes. It has no say in the outcome.

Three places decide

No single component enforces all of this, and that is on purpose.

Figure 3: three places decide

The CIAM, at every exchange. It sets the upper bound and signs it into the token: destination, scope, actors, lifetime.

The gateway, at every hop of a customer's request. At the entry it exchanges the sign-in token. On the other hops it checks that the token is addressed to this destination, presented by its actor, and still backed by an open session. It also refuses operations that are not in the service's contract.

The service. It checks the audience and the scope again, answers only for the customer in sub, and applies its own business rules. Ours refuses any transfer with an agent anywhere in act, whatever the scope says. A service acting for the customer can still order it.

All three write down what they decided. A reader of the second post asked how to answer "which agent sent this?" after the fact. The token now carries both halves: act lists who acted, and azp says who asked for each token.

Where this sits among the standards

We kept to what published documents say, and we do not claim more than that.

  • RFC 8693 (Proposed Standard) defines the request, the response and the act claim. It does not say who the client of an exchange should be. That is left to the authorisation server's policy. Its examples show the service that received a token exchanging it, which is what agents and services do here. It also says that prior actors in a nested act are informational only, and must not be used for access control.
  • NIST SP 800-204 (final, 2019) recommends that user session tokens are not passed beyond the gateway, and that a token exchange service sits between gateways. That is the entry exchange.
  • The IETF WIMSE architecture (draft-ietf-wimse-arch-09, working group draft, informational, 17 November 2026) says that in chains of agents, each hop must explicitly scope and re-bind the security context.
  • The IETF AIMS draft (draft-ietf-wimse-aims-01, working group draft, informational, 2 November 2026) calls token forwarding an anti-pattern, and says the language model must not have access to an agent's credentials.
  • The MCP specification forbids token passthrough.

None of these documents compares "exchange at the gateway" with "exchange in the agent" and declares a winner. Where the exchange happens is a design choice. This post explains ours, and the next sections say what it costs.

What we learned the hard way

  • Keycloak's standard delegation needs consent. Delegation with an actor depends on a may_act claim, which only comes from the customer's consent. Agents acting for customers by organisation policy needed a token exchange extension, on an interface that is internal to Keycloak.
  • The entry needed its own rule. Keycloak lets the party a token was issued to exchange it again. Without an extra check, a gateway could re-exchange a token it had obtained for an agent and point it somewhere else. Our tests found it. At the entry the CIAM now accepts only a sign-in token addressed to that gateway, with no act.
  • Exchanges can widen scope by default. In Keycloak the scope comes from the client that asks, not from the token presented. A built-in client policy makes exchanges narrow-only. Turn it on. It also filters the client's own audience mappers, which the extension had to put back.
  • Agent platforms decide what reaches the agent. Foundry passes only x-client-* headers, and only on its Responses endpoint. Agent Runtime strips custom headers, so the gateway puts the customer's token in the A2A message metadata and removes anything the caller planted there.
  • A gateway's own token can be too old. API Management reuses its managed-identity token for about a day. Keycloak accepted client assertions for two hours after they were issued. Everything worked in every test, and failed in the third hour.
  • Introspection is limited to the token's audience. Keycloak only lets a client introspect tokens addressed to it, and the token on an internal hop is addressed to the next agent, not to the gateway. A per-client setting lifts that. We turned it on for the gateways only, and checked that an agent still cannot introspect someone else's token.
  • Measure before you blame the identity layer. Requests that took four to five seconds felt slow, and the exchanges looked like the obvious suspect. Measured hop by hop, the entry exchange took 50 to 100 ms, each agent's own request to the CIAM 35 to 130 ms, and the checks at API Management about 50 ms, almost all of it the session check. Most of the time went to the model calls.

What this design asks of you

The agents and services hold an exchange credential. Each one can ask the CIAM for tokens, so its credential needs the same care as any other. Ours is not stored: it is the platform identity, issued on demand. The Entra token an agent presents to the CIAM lives for about an hour. On its own it is useless. It only exchanges tokens addressed to that same agent, which last five minutes. The exchange runs in the agent's deterministic code. The model never sees a token, in prompts, in history or in logs.

The CIAM is on every hop that acts for a customer. Calls with no customer behind them, agent to agent with their own identity as in the first post, never touch it. A customer's request touches it twice at every handover: the exchange, and the gateway's session check. For those requests the CIAM's availability becomes the chain's availability, and the gateways fail closed when it does not answer. We have not load-tested it.

The act chain is an audit trail, not proof. The CIAM copies the prior actors from a token it signed itself. Services decide with sub, aud, scope and the current actor, as RFC 8693 says. The full chain is for the audit record.

A ceiling limits damage, not misuse. A manipulated agent can still do everything its ceiling allows, for the customer in front of it. Reading a balance is one thing. Operations that move money need the customer's explicit confirmation for that operation, which we have not built. Until then our service refuses them whenever an agent is in the chain.

These are bearer tokens. A stolen token works for at most five minutes, at one destination, and only for a caller that matches its actor at the gateway. The current OAuth security best practice (RFC 9700) recommends binding tokens to their holder. We have not done that.

The rules are our own code. They live in a Keycloak extension that nobody outside the project has reviewed, on an interface Keycloak does not promise to keep stable. Before production, confirm that your CIAM can express the same rules by configuration, and run the same negative tests against it. Ours has 64 checks, from a gateway presenting an actor to a token used after sign-out.

And people inside the organisation?

Not everyone who talks to these agents is a customer. Operators, such as a contact centre, sign in with Entra ID, not at the customer CIAM, and reach the same agents and the same services. The same rules apply to them: every actor named in the token, and a scope that only shrinks. They also bring questions of their own, such as how quickly a person's access can be cut off. That is the next post.

Wrapping up

  • The customer signs in once and never hears about agents.
  • The sign-in token stops at the gateway. Every later hop gets its own token: same customer, one destination, five minutes at most.
  • Whoever hands the request on asks for the next token, as itself, with no secret. A token can only be exchanged by the party it was given to.
  • The token names every actor, says who asked for it, and its scope only shrinks.
  • When the customer signs out, the chain stops, including the tokens already issued.
πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.