Why Users and Agents Need Three Legs: Intro to 3LO and OBO

(Part 1 of the “Why Users and Agents Need Three Legs” series)

About this series

This series explains how agents can call other services as the user, and what it takes to build that safely:

  • This post: the concepts of OBO and 3LO, and how they help you trust AI agents.
  • Post 2: what a credential broker is, how it simplifies 3LO, and how Google Cloud’s credential broker ‒ Agent Identity Auth Manager helps you.
  • Post 3: building the consent flow, error handling, and token and consent expiry with that broker.
  • Post 4: adding central governance and policy enforcement with an agent gateway.
  • Post 5: what to do when a gateway isn’t an option.

Why is it so hard to trust AI agents?

We want AI agents to handle our busywork, but we hesitate when that means letting an agent act on our behalf. Picture Alice using an AI assistant to implement a feature. She asks: “Implement the feature X according to the design document I wrote and consult recent email and chat threads.” The agent follows the instructions and writes the code. Behind the scenes, the agent accesses Alice’s Google Drive to read the design document (HLD) and scans her emails and chat history. However, assuming the newly generated code makes the old design obsolete, the agent silently overwrites the original design document in Drive without Alice’s knowledge.

What went wrong here is simple: Alice asked the agent to read her workspace, not to change it, and nothing stopped the agent from crossing that line. Letting an agent manipulate Alice’s workspace for one request shouldn’t require granting long-term access or worse, granting the agent access to the whole organization’s workspace. Alice can overwrite her own HLD. But doing it with the help of the agent requires a conscious choice instead of an unexpected accident. You might think Alice could avoid this with stricter instructions in her prompt or a tighter agent harness. That can help in a toy setup, but it breaks down in real-world usage where an agent reads untrusted emails and documents, or where you don’t control the agent’s code. Prompts can be misunderstood or tricked, and a harness cannot reliably police access to third-party (3P) services and other agents.

What is needed to trust the agents

Before speaking about solutions, let’s see how the AI assistant from the previous example could access Alice’s workspace in the first place. There are mainly two options:

  1. The AI assistant had permissions to read and modify Google Drive documents and at least read emails and chats of everyone in Alice’s organization.
  2. Alice (somehow) gave the assistant access to her documents, emails and chats.

The first option has obvious issues. In the above example the agent could read or modify documents mentioned in the chat thread that Alice herself has no permission to access. This option also violates the principle of least privilege because it requires granting the agent access to the broadest set of permissions across all users just in case another user wants to do something that requires extra access.

The second option addresses most of the issues of the first but it also raises additional questions. How does Alice explicitly consent to the agent accessing her workspace and limit the agent to read-only permissions for this task? How does she revoke that consent later if she changes her mind? We may want to ask questions: which identity does the agent use to call the downstream APIs? Is the call registered as made by the agent or by Alice?

Clearly the agent must not have full access to all APIs, and also it must not receive a blanket credential with all of Alice’s privileges. Having said that, if the agent could act on behalf of Alice it would solve many problems. What we need is:

  • R1. Securely issue and deliver a temporary, scoped access token for calling 3P services without exposing Alice’s password or session.
  • R2. Allow Alice to review and approve (consent to) a request for issuing a temporary access token through a UI.
  • R3. Avoid granting a temporary access token with broader permissions than the agent itself is permitted to have.
  • R4. Control how long the access lasts, with a way for Alice to revoke it at any time.

Together, these requirements give Alice clear control over the permissions she grants. When an authorization flow enforces them, she can trust that the agent cannot exceed what she approved, even if a request to the agent goes sideways.

When you search the internet for solutions, you will come across two terms: 3-legged OAuth (3LO) and On-behalf-of (OBO).

The rest of this post walks through how 3LO, OBO, and token lifecycle flows work together through Alice’s example.

3LO and OBO: similarities and differences

Modern APIs authorize calls using access tokens, which are short-lived authorization credentials attached to each API request to prove what the caller may do. Both 3LO and OBO put a user-delegated access token into the agent’s hands, but start from different inputs: 3LO starts with no user token, whereas OBO starts with an incoming user token (for example, from Alice’s sign-in session) plus the agent’s own workload token and trades both for a narrower downstream token.

3LO and OBO refer to OAuth 2.0 flows described in RFC 6749 (3LO) and RFC 8693 (OBO token exchange). You may also run into 2-legged OAuth (2LO), which is strictly service-to-service with no user involved. We’ll skip it here.

As we will see in the following sections, 3LO and OBO partially overlap in implementing R1 - R4 requirements with these differences:

  1. 3LO addresses a requirement to support explicit user approval (consent) for the access (R2). OBO doesn’t include user consent acquisition.
  2. 3LO and OBO differ in narrowing access permissions. In 3LO the agent would request access using explicitly defined scopes (R1). OBO supports downscoping based on the requested scopes and the scopes of the user and the agent (R1 and R3).
  3. OBO can improve auditability. Both flows identify the user whose access is used, whereas OBO can also include the caller’s identity (in our example, the AI assistant) acting on the user’s behalf. Support for this is optional and depends on the OAuth 2.0 implementation (RFC 8693 §4.1).

In the above example, 3LO would allow Alice to see a dialog window asking her to approve the AI assistant’s access to her workspace. However, the AI assistant would have to be programmed to request read-only access to Alice’s workspace. Using OBO, Alice will not be notified that the AI assistant will access the workspace on her behalf. Additionally, OBO expects the agent to have permissions to Alice’s workspace with at least the same scope as requested; otherwise, the flow fails.

Let’s review how each of the flows works in theory.

3LO: the three legs and one-time setup

3LO allows a service to get a token that represents a user without ever touching the user’s credentials. Let’s look at the flow through the lens of our example:

  sequenceDiagram
    actor A as Alice (browser)
    participant AI as AI Assistant (agent)
    participant ID as Google OAuth 2.0 Server
    participant WS as Workspace API (service)

    A->>AI: 1: "Read HLD, my mail and chat"
    AI-->>A: 2: Redirect to Google OAuth 2.0 Server (client ID, scope, state, PKCE challenge)
    A->>ID: 2: Follow redirect
    ID->>A: 3: Sign in & approve access scope to Workspace
    A->>ID: 3: Approve
    ID-->>A: 4: Redirect back to AI Assistant (one-time code, state)
    A->>AI: 4: Deliver code to callback URL
    AI->>ID: 5: Exchange code (code, client ID, client secret, PKCE verifier)
    ID-->>AI: 5: Access token, refresh token
    AI->>WS: 6: Call API with Authorization: Bearer <access_token>
    WS-->>AI: 6: Content of Alice's HLD, mail and chat
  1. AI Assistant spots a need for Alice’s token. Alice asks the agent to perform an operation that makes the agent call Workspace. The agent sees it has no access token for Alice to call the Workspace API, and it starts a 3LO flow.
  2. AI Assistant sends Alice’s browser to Google OAuth 2.0 Server. The agent builds an authorization URL containing its client_id, its registered redirect_uri, the scopes such as documents.readonly, gmail.readonly etc. and a random state value (tying the callback to Alice’s session), and a PKCE challenge (RFC 7636, proving the same app redeems the code). Before redirecting, the agent saves state and the secret PKCE verifier in Alice’s session so it can verify the return trip.
  3. Alice signs in and approves at Google OAuth Server. Alice is now interacting directly with Google OAuth Server. She signs in (if she isn’t already) and sees a dialog saying something like: “AI Assistant wants to read your documents, emails and chat.” When she clicks Approve, the OAuth server records her grant.
  4. Google OAuth Server sends a one-time code back through the browser. The OAuth server redirects Alice’s browser to AI Assistant’s registered redirect_uri with a short-lived authorization code and the original state. AI Assistant checks that state matches what it stored in step 2, proving the code came from the flow Alice just started.
  5. AI Assistant trades the code for tokens behind the scenes. The code on its own can’t call the Workspace API. AI Assistant makes a direct server-to-server call to Google OAuth Server’s token endpoint (RFC 6749 §4.1.3), presenting the code, its client secret, and the PKCE verifier. The OAuth server returns a short-lived access token and (usually) a long-lived refresh token, which AI Assistant stores under Alice’s user record.
  6. AI Assistant calls the API as Alice. AI Assistant attaches Authorization: Bearer <access_token> (RFC 6750) to its Workspace API request. Workspace verifies that the token is valid, unexpired, and carries the correct scope, then returns content of Alice’s HLD, emails and chat. Workspace records the call as made by Alice.

Before any user runs this flow, AI Assistant completes a one-time registration with Google OAuth Server, registering its exact callback URL (redirect_uri) and the scopes it may request. It receives a client_id and client_secret from Google OAuth Server reused across all users. Enforcing an exact match on redirect_uri stops attackers from redirecting Alice’s code elsewhere.

Warning

Workspace API only verifies that the token covers the requested scope and is authorized to access resources ‒ the API can’t tell whether the assistant is serving Alice or another user with Alice’s token, or overstepping Alice’s prompt. Isolate stored tokens strictly per authenticated user, and follow the principle of least privilege on scope to limit blast radius.

The three “legs” in this flow are:

  • Alice (human user)
  • AI Assistant (service that wants to access an external API on behalf of Alice)
  • Google OAuth Server and Workspace API server (together forming the third leg)

OBO: narrowing access through token exchange

OBO supports acquiring an access token on behalf of the user, just like 3LO, but without user consent. It also enforces scope narrowing (downscoping). The downscoping takes place when the flow exchanges two tokens for one ‒ it sends the user’s and agent’s tokens and receives a new user token with a scope equal to the intersection (∩) of the requested, agent’s and user’s scopes: Requested scopes ∩ Agent's scopes ∩ User's scopes.

  sequenceDiagram
    actor A as Alice (browser)
    participant AI as AI Assistant (agent)
    participant ID as Google OAuth 2.0 Server
    participant WS as Workspace API (service)

    A->>AI: 1: "Read HLD, my mail and chat"
    AI->>ID: 2: Exchange tokens (Alice's token, agent's token, scope)
    Note over ID: Validates agent's + Alice's permissions<br/>Enforces scope intersection
    ID-->>AI: 2: Downscoped access token
    AI->>WS: 3: Call API with Authorization: Bearer <access_token>
    WS-->>AI: 3: Content of Alice's HLD, mail and chat
  1. Alice sends a request to AI Assistant. Alice asks the agent to perform an operation that makes the agent call Workspace. AI Assistant already holds an incoming token representing Alice (subject_token) along with its own workload token (actor_token), and needs a Workspace-scoped token to make the downstream call.
  2. AI Assistant trades both tokens for a downscoped token. Without redirecting Alice’s browser, AI Assistant sends a server-to-server POST /token request to Google OAuth Server (RFC 8693 §2.1) with Alice’s and its own tokens, and the documents.readonly, gmail.readonly etc. scopes. The OAuth server validates both tokens, intersects their permissions with the requested scope, and returns a short-lived, downscoped access_token for the Workspace API.
  3. AI Assistant calls the API as Alice using the downscoped token. AI Assistant attaches the downscoped access_token to its Workspace API request. Workspace verifies that the token is valid, unexpired, and carries the correct scope, then returns content of Alice’s HLD, emails and chat. Workspace records the call as made by Alice. If the OAuth server includes an actor claim (RFC 8693 §4.1), downstream services can also record AI Assistant as the acting caller.

In practice, this prevents privilege escalation: although Alice has full access to her workspace, an AI assistant with read-only permissions cannot gain write access when acquiring Alice’s token through OBO. If the agent has no Workspace permissions at all, the exchange fails instead of letting it borrow Alice’s.

3LO, OBO and more: all four requirements flow

We saw that 3LO and OBO separately implement R1, R2 and R3 requirements. Note that neither 3LO nor OBO covers the implementation of R4 requirement. The OAuth server enforces the expiration of temporary access tokens and supports manual token invalidation. However, these operations aren’t part of 3LO and OBO. Before looking into how R4 is implemented, let’s review how 3LO and OBO can be combined into a single flow.

The combined flow is literally two flows: 3LO and OBO going one after another:

  sequenceDiagram
    actor A as Alice (browser)
    participant AI as AI Assistant (agent)
    participant ID as Google OAuth 2.0 Server
    participant WS as Workspace API (service)

    %% 3LO part of the flow to acquire consent
    rect rgb(240, 250, 255)
        Note over A, WS: acquiring access consent

        A->>AI: 1: "Read HLD, my mail and chat"
        AI-->>A: 2: Redirect to Google OAuth 2.0 Server (client ID, scope, state, PKCE challenge)
        A->>ID: 2: Follow redirect
        ID->>A: 3: Sign in & approve access scope to Workspace
        A->>ID: 3: Approve
        ID-->>A: 4: Redirect back to AI Assistant (one-time code, state)
        A->>AI: 4: Deliver code to callback URL
        AI->>ID: 5: Exchange code (code, client ID, client secret, PKCE verifier)
        ID-->>AI: 5: Access token, refresh token
    end

    %% OBO part of the flow to narrow scope
    rect rgb(250, 240, 255)
        Note over A, WS: intersect scopes for request, Alice and agent

        AI->>ID: 6: Exchange tokens (Alice's token, agent's token, scope)
        ID-->>AI: 6: Downscoped access token
        AI->>WS: 7: Call API with Authorization: Bearer <access_token>
        WS-->>AI: 7: Content of Alice's HLD, mail and chat
    end

The OBO flow begins at step 6. Both flows run in deterministic code outside the LLM, so they aren’t exposed to AI unpredictability. Even if there’s a bug in that code, the scope Alice approves in step 3 is still a hard ceiling—and step 6 can only narrow it further.

Acquiring a scoped access token is only the beginning. The access tokens (in steps 5 and 6) are issued with an expiration time. A default time is usually set to 3600 seconds (or 1 hour). It’s done to mitigate attacks such as session hijacking or token replay when a stolen token is used in another client or service. However, Alice and other users of the AI assistant wouldn’t be happy if they are asked to sign in again and repeat consent every hour (or more often). To prevent this, the agent uses the refresh token to refresh the expired access token without bothering Alice again.

  sequenceDiagram
    participant AI as AI Assistant (agent)
    participant ID as Google OAuth 2.0 Server
    participant WS as Workspace API (service)

    Note over AI: Holds access token and refresh token
    AI->>WS: 1: Call API with Authorization: Bearer <expired_access_token>
    WS-->>AI: 1: 401 Invalid token error
    AI->>ID: 2: Renew access token (refresh token, client ID, client secret)
    ID-->>AI: 2: New access token, optional new refresh token
    opt OBO does not return refresh token
        AI->>ID: 3: Exchange tokens (new access token, agent's token, scope)
        ID-->>AI: 3: Downscoped access token
    end
    AI->>WS: 4: Call API with Authorization: Bearer <new_access_token>
  1. AI Assistant calls API with an expired access token. Just like in the previous flows, AI Assistant sends the temporary access token to the Workspace API, which rejects it with a 401 invalid token error.
  2. AI Assistant sends a renewal request to Google OAuth Server. AI Assistant sends a server-to-server POST /token request to Google OAuth Server (RFC 6749 §6) with its stored refresh token, client ID, and client secret. The OAuth server validates the token and returns a new access token and optionally a new refresh token.
  3. AI Assistant downscopes the new access token. According to RFC 8693 §2.2.1, a token exchange response typically does not return a refresh token (it is optional and up to server policy). When it doesn’t, the refresh token used in step 2 is the one received in step 5 of 3LO. Google OAuth Server returns a new access token that is not downscoped. So, AI Assistant has to repeat step 6 from the previous flow to downscope the new access token.
  4. AI Assistant retries the API call as Alice. AI Assistant attaches the new (downscoped) access token to its Workspace API request and receives Alice’s HLD, emails, and chat.

The second part of R4, proactively revoking the access, works a bit differently. Most OAuth servers provide a UI allowing account holders to revoke access they granted to a particular client application (or agent). Alice can open her Google Account’s settings and in the Third-party apps & services section find and remove access granted to the AI assistant. Revoking directly in her Google Account requires no cooperation from the agent. Once revoked, the refresh token (and on servers like Google’s, the active access tokens) stops working; AI Assistant’s next refresh fails with invalid_grant (RFC 6749 §5.2), forcing Alice back through step 2 if she wants to reconnect. It’s also possible that the AI assistant provides an in-application UI that follows RFC 7009 §2.1 and sends the POST /revoke request to the Google OAuth server.

What’s next?

We looked into a couple of standards that describe how OAuth 2.0 helps to make access on behalf of the user secure and transparent. That took a bit of the theory: defining four requirements and walking through the full flows. Now what you may ask? There are a few practical pieces we haven’t covered yet:

  • Secret and token storage: We saw that the agent needs to manage a pair of access and refresh tokens per user. And there is an authorization code and state and PKCE verifier. How does the agent manage all this data?
  • Callback endpoint and code exchange: Hosting a redirect endpoint on the agent, validating state and PKCE, exchanging authorization codes for tokens, and redirecting the user back to the caller. How do you implement all of this cleanly without opening subtle security holes?
  • Chained callers and agent-to-agent flows: We didn’t cover how the flow works when between a user and the agent there are more callers. Consider the flow when a user interacts with a frontend app in the browser which calls some backend API which invokes some AI agent that invokes another AI agent and that last agent now needs to access the user’s data via some API. How do all these redirect URIs, codes and tokens travel back and forth?
  • Enterprise scale and multi-tenancy: How do these implementation challenges scale? Multiply those tasks across dozens of tools, thousands of users, and chains of agents, and maintaining the OAuth plumbing quickly rivals building the agents themselves.

In Post 2, we’ll look at how to use a credential broker to take the OAuth client role and token lifecycle off your plate so you can focus on building the agent. Specifically, we’ll explore Google Cloud’s Auth Manager and show examples of AI agent integrations.