Identity & authorization for agents

In one sentence

An agent acts for a user: give it short-lived, narrowly scoped, delegated tokens capped by the user's rights; authorize every tool call in code, confirm risky ones, and audit each action to the human.

Analogy: A valet key: it starts the car and opens the door, but not the trunk or the glovebox, and you get it back when you leave.

The agent never holds the user's master credential: it gets a short-lived token naming both parties, and the tool decides.

How it works

  • Every call answers two questions: who asked (the human) and who acts (the agent).
  • Delegate, don't impersonate: token exchange yields a token naming the user as subject, the agent as actor.
  • Effective access = user rights ∩ agent scopes ∩ task needs; never a shared admin service account.
  • A rule in the prompt is not a control: prompt injection rewrites prompts, not your policy engine.
  • This prevents the confused deputy: a privileged program tricked into using its authority for someone else.

Check yourself

Answer to mark it learned; your answers aren't scored.

In a design review, an agent forwards the user's SSO access token unchanged to the CRM, calendar and payments APIs. The team argues it's least privilege, since the agent can only do what the user can. Your main objection?

Go deeper

Full explanation

An agent is software acting on behalf of a user, so every call answers two questions: who asked (the human) and who acts (the agent).

  • Delegate, don't impersonate. The agent gets a credential derived from the user's: OAuth consent, or token exchange (RFC 8693, Entra's on-behalf-of) yielding a token that names the user as subject and the agent as actor.
  • The user's rights are the ceiling. Effective access = user rights ∩ agent scopes ∩ what the task needs. Never a shared admin service account.
  • Short-lived and narrow. Minutes to an hour, one audience, minimal scopes (calendar.read) to shrink any leak.
  • Authorize every tool call in code, at the tool. A rule in the prompt is not a control: Prompt injection (direct & indirect) can rewrite the prompt, not your policy engine.
  • Confirm high-risk actions with the human (Human-in-the-loop & approvals): payments, deletes, external messages.
  • Workload identity for the agent (SPIFFE, cloud IAM roles): no static keys in prompts or code.
  • Audit: log user, agent, tool, arguments and decision on every call.

Remote Model Context Protocol (MCP) servers follow this model: each is an OAuth 2.1 resource server, tokens are bound to it (RFC 8707), and token passthrough is forbidden.

The bug this prevents is the confused deputy (Hardy, 1988): a privileged program tricked into using its authority for someone else.

Code RFC 8693 token exchange + authorization enforced in the tool
pythonRFC 8693 token exchange + authorization enforced in the tool
import httpx

TOKEN_URL = "https://auth.example.com/oauth2/token"

def token_for_tool(user_token: str, agent_token: str, scope: str, audience: str) -> str:
    """Short-lived token for one tool, naming the user (subject) and the agent (actor)."""
    r = httpx.post(TOKEN_URL, auth=(CLIENT_ID, CLIENT_SECRET), data={
        "grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
        "subject_token": user_token,
        "subject_token_type": "urn:ietf:params:oauth:token-type:access_token",
        "actor_token": agent_token,           # the agent's own workload identity
        "actor_token_type": "urn:ietf:params:oauth:token-type:jwt",
        "scope": scope,                       # e.g. "refunds:write", nothing more
        "audience": audience,                 # valid only at this API
    })
    r.raise_for_status()
    return r.json()["access_token"]

# Inside the tool: the model only *proposed* this call; code decides.
def refund(order_id: str, amount: float, claims: dict) -> dict:
    user, agent = claims["sub"], claims["act"]["sub"]      # who asked, who acts
    if "refunds:write" not in claims["scope"].split():
        raise PermissionError("missing scope")
    if not owns_order(user, order_id):                       # user's rights are the ceiling
        raise PermissionError("not your order")
    if amount > 100 and not approvals.verify(user, order_id, amount):
        raise NeedsConfirmation(order_id, amount)            # human approves high risk
    audit.log(user=user, agent=agent, tool="refund", order=order_id, amount=amount)
    return payments.refund(order_id, amount)
Real example

A sales agent books meetings and updates the CRM for 3,000 reps. Version 1 used one integration user with org-wide admin rights; in a red-team test, a single injected email made it export every account. Version 2: each rep's session exchanges their SSO token for 15-minute tokens scoped to their own calendar and the accounts they own, the CRM tool re-checks ownership in code, external emails need a click, and every call logs rep, agent and arguments. The same email now gets a 403 and raises an alert.

Trade-offs +3 · −3

+ You gain

  • Caps a prompt injection's blast radius at what one user could already do.
  • The audit trail answers 'who did this?' for every agent action, as compliance requires.
  • Reuses standard IAM (OAuth, OIDC, cloud roles) instead of inventing agent security.

− You pay

  • Token exchange and per-tool scopes add identity-provider work, integration effort and a little latency.
  • Many SaaS APIs still lack fine-grained scopes or delegation, forcing broad tokens.
  • Too many confirmations and users approve without reading.

Use it when

Any agent that reads private data or acts in other systems for a user, especially in multi-tenant or enterprise products with audit requirements.

Avoid it when

Pure generation with no tools and no private data. Keep provider keys server-side anyway; you just don't need delegation.

Numbers to know 5
  • RFC 8693 (Jan 2020) defines OAuth token exchange; its act claim records who is acting for whom.
  • AWS STS role sessions last 15 min to 12 h, default 1 h; role chaining caps them at 1 h.
  • Microsoft Entra access tokens default to a random 60–90 minute lifetime (75 on average).
  • MCP spec: authorization is optional; HTTP servers that use it are OAuth 2.1 resource servers and must reject tokens not issued for them.
  • Confused deputy: named by Norm Hardy in 1988, still the core agent-security bug.
Common pitfalls 4
  • One shared service account with broad rights for all users: every injection becomes an org-wide breach.
  • Writing rules in the system prompt ('never refund over $100') instead of in the tool's code.
  • Passing the user's token straight through to downstream APIs: wrong audience, no actor, no audit.
  • Long-lived refresh tokens stored in agent memory, transcripts or logs.
Interview questions 3

Answer out loud first, then reveal.

How do you authorize an agent that acts on behalf of a user?Show answer

I separate who asks from who acts. The user signs in normally; the agent has its own workload identity. When it needs a tool, it exchanges the user's token for a short-lived one scoped to that tool and audience and carrying both identities, via RFC 8693 or the IdP's on-behalf-of flow. Each tool authorizes in code against the user's rights, so the agent can never exceed them. Irreversible actions need confirmation, and every call is logged with both identities. The trade-off is integration work, but it caps an injection at one user's blast radius.

What is the confused deputy problem in an agent context?Show answer

A deputy is a program with its own authority acting on someone's request; it's confused when the requester tricks it into using that authority for them. With agents the classic case is a shared service account: a user can't see another team's data, but asks the agent, or plants an instruction in a document, and the agent fetches it with its own broad rights. The fix is that the agent's authority never exceeds the requester's: delegated, audience-bound tokens and checks at the resource. That's why MCP forbids token passthrough.

Why not just tell the model in the system prompt what it's allowed to do?Show answer

Because the prompt is not a security boundary. Prompt injection can override instructions and has no complete fix. The model proposes a tool call; deterministic code holding the user's scoped credentials decides. I still describe permissions in the prompt so the model makes fewer doomed calls, but enforcement lives in the tool or a policy engine like OPA or Cedar, and denials return clear errors the agent can explain to the user.