· AI dev tools

Amazon Cognito OAuth token exchange delegates user access to AI agents

When an AI agent reserves a table and invites friends for you through a restaurant review application, the process of securely passing permissions across services becomes critically important.


This scenario highlights the growing need for robust mechanisms to manage delegated access for intelligent agents.

In a typical setup using OAuth 2.0, if you use a review app to check ratings, book reservations, and send invitations, the application directs you to your email provider's trusted authorization server. There, you grant permission, specifying only the access needed, such as the ability to read contacts or send messages. The review app never sees your login credentials; instead, it receives a short-lived access token restricted to the approved scopes. This token allows the app to interact with your email provider’s API to send invitations and access your contacts on your behalf.

However, this direct interaction model breaks down when multiple services are involved. For instance, when the app books a table, it calls its reservation service, presenting your token to email details to your friends. The question arises: what token should the reservation service use when it subsequently calls the email API? If the reservation service simply passes along the original access token granted to the review app, the email API mistakenly believes the app itself initiated the request, effectively obscuring the reservation service's involvement. Conversely, if the reservation service uses its own token obtained via an OAuth 2.0 client credentials grant, the user's identity is lost; the email API knows which service made the call but not which end-user authorized it. Figure 1 illustrates this multi-hop interaction.

OAuth 2.0 Token Exchange, as defined in RFC 8693, resolves this second-hop problem. Rather than forwarding your initial access token, the reservation service returns to the authorization server carrying two pieces of information: your access token, which confirms your consent, and its own credentials, which identify the service. The authorization server then issues a new token specifically for the email API, known as the issued token. This new token identifies the user, names the reservation service, and maintains the original scopes or limits them further.

AI agents are making this multi-hop communication more frequent and more vital. Consider the review app integrating an AI agent that handles reservations and invitations. This agent selects its necessary tools at runtime, perhaps a Model Context Protocol (MCP) server for sending invitations, and these tools then interact with APIs on your behalf. With token exchange, every connection point only receives the precise permissions it requires, allowing the email API to clearly see which service is acting on your behalf. This post details how to leverage the new OAuth token exchange capability within Amazon Cognito when services and AI agents invoke your APIs for users, with Cognito acting as the OAuth 2.0 authorization server. We will clarify the distinction between impersonation and delegation and provide examples of how this feature functions with an MCP server and Amazon Bedrock AgentCore Identity.

At its core, every token exchange process begins with a subject token that represents the end-user. In Amazon Cognito, this subject token can be either an access token or an ID token that your user pool provided to the application where the user initially logged in. The service that needs to make the subsequent call sends this subject token to Cognito, and in response, Cognito furnishes a new token for that service to utilize when accessing the downstream resource server.

Token exchange differentiates between two operational patterns: impersonation and delegation. The calling service decides which pattern to employ based on whether the downstream API needs to verify the service performing the action on the user's behalf.

Impersonation means the calling service does not present itself as the primary actor. The resulting issued token represents the user; while its `client_id` claim identifies the application that performed the exchange, it lacks an `act` (actor) claim to indicate a service is acting for the user.

Delegation, conversely, involves the calling service explicitly identifying itself as the actor by also supplying an actor token. Amazon Cognito incorporates an `act` claim that names the calling service, enabling the resource server to log both who authorized the action and who executed it.

Impersonation is suitable when the calling service merely relays the user's request without making independent judgments, such as a backend for frontend (BFF) forwarding web app requests to your APIs. It is also appropriate when a downstream API only needs to authorize the user and has no need to know which service relayed the call. For AI agents, delegation is the crucial pattern. If an agent used its own token, the logs would only show an agent accessing the resource server, not for whom. With impersonation, the logs would show the user but fail to identify the agent acting on their behalf; delegation provides the necessary data for proper auditing and authorization. Figure 2 charts the flow for delegated access across two service hops.

Figure 2 outlines the sequence for delegated access between services: First, the user authenticates with your application via Amazon Cognito, resulting in Cognito issuing the user’s access token to the client application. Next, the client sends the user’s access token to the downstream service, such as an MCP server. This service then calls the Cognito `/oauth2/token` endpoint, authenticating with its own application client. It passes the user’s access token as the subject token and designates the resource server as the audience. For delegation, it additionally sends its own token as the actor token. Cognito validates this request and issues a token. This issued token still identifies the user in the `sub` field; for delegation, it also names the service in the `act` claim. Subsequently, the service calls the resource server, like an email API, using the issued token. The resource server verifies that the token's `aud` claim matches its own identifier, then authorizes the request for the user and returns the user's data to the service. Finally, the calling service relays this outcome back to the client.

Beyond initiating a token exchange at the `/oauth2/token` endpoint on your user pool's domain, you possess the option to utilize the GetDerivedToken API via an AWS SDK, which bypasses the need for a user pool domain. Both methods accept the same subject token, actor token, audience, and scopes, and they both yield an issued token containing identical claims.

Here is an example of an issued token resulting from a delegation token exchange: { "iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_EXAMPLE", "sub": "c4b82478-5678-90ab-cdef-EXAMPLE11111", "username": "you@example.com", "client_id": "invitations-mcp-client-id", "version": 2, "token_use": "access", "scope": "email-api/messages.send", "aud": "email-api", "act": { "sub": "invitations-mcp-client-id", "client_id": "invitations-mcp-client-id", "iss": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_EXAMPLE", "token_type": "urn:ietf:params:oauth:token-type:access_token" }, "exp": 1735689600, "iat": 1735686000 }

The email API authorizes the user identified by `sub`, and the `act` claim informs it which service is operating on behalf of that user. This allows the API to determine if it trusts that specific service, and its logs can consequently record both the user and the service for every request. You should only accept a delegated token if the `act.client_id` corresponds to a service you trust, such as the invitations MCP server, and matches the token’s own `client_id`, confirming the trusted service requested the token itself.

Amazon Cognito enforces several security checks during every exchange before issuing a token. It verifies the signature and expiration of the subject token and will reject any token that has been revoked or belongs to a disabled user within your user pool. If an audience is included in the request, the issued token becomes bound to that specific resource server, and every requested scope must pertain to it. Furthermore, the issued token will only contain scopes permitted by both the subject token and the application client; thus, an exchange can restrict access but never expand it. It also does not possess a lifespan longer than the subject token, and the exchange process does not return a refresh token. For a comprehensive list of these checks, consult the Security model section in the Amazon Cognito Developer Guide regarding Exchanging tokens for downstream services.

Cognito’s checks do not supersede the resource server’s own validation duties; it must still confirm the JWT tokens, including their signature, issuer, expiry, audience, and scopes, and for delegated requests, it must validate the `act` claim. Your user pool may incorporate a pre-token generation AWS Lambda trigger, which has the capability to add or remove custom claims and scopes in your user pool’s tokens. Amazon Cognito also activates this trigger during token exchange, using the `TokenGeneration_TokenExchange` trigger source. However, Cognito does not incorporate any scopes returned by the trigger into an exchange, meaning the calling service cannot gain permissions beyond what the user’s original token already possessed. You retain the ability to use the trigger to strip scopes from the issued token, as detailed in Customize the exchanged token with a Lambda trigger.

To activate token exchange within your Amazon Cognito user pool, you must enable it as an authentication flow for each application client. Refer to the Amazon Cognito Developer Guide for prerequisites. To enable this functionality for an app client: first, access the AWS Management Console for Amazon Cognito and select your Cognito user pool. Within the navigation pane

Why it matters

This feature directly impacts how developers build secure, multi-hop architectures involving AI agents. It resolves the challenge of maintaining user identity while allowing intermediate services to act on the user's behalf for proper authorization and logging.

Earlier coverage

Related coverage

Also worth reading

  1. Node 26.14.0 released, adding child_process.timedOut, fs.mkstemp, and http batch body spans
  2. next.js 16.5.0-rc.1 released, fixing image cache ages and keeping immutable assets
  3. Angular 22.3.1 released with fixes to animations, compiler, and core functionality
  4. Pro teams now use usage-based billing for Standard builds, costing $0.014 per minute
  5. Teleport Beams agent runtime provides sandboxing, identity, access, and auditing for AI agents