Agent Governance 01: OAuth for infra people

This is a series of posts on AI Agent Governance

Part Title
Post 00 Why I’m building a tiered governance lab for AI agents
Post 01 OAuth for the infrastructure person
Agents work on behalf of humans.

Where we are

Post 00 set out the problem: AI agents need tiered identity and governance, not just API keys. This post covers the mental model I had to build before any of that made sense — OAuth and OIDC, explained for someone who understands routing and load balancers but doesn’t love token flows. This is what clicked for me.

The four characters

Every OAuth flow involves four roles:

  • The user. A human who wants to do something. In this lab: a support rep who wants to look up a customer ticket.
  • The client. The application or agent acting on the user’s behalf. In this lab: the ticket agent, running as a pod in the cluster. The client doesn’t have the user’s password. It gets a token instead.
  • The authorization server. The thing that issues tokens. It knows who the user is, it knows what the client is allowed to do, and it stamps a token that the resource server can verify. In this lab: Keycloak for agents, with APM in the human login path.
  • The resource server. The thing the client actually wants to call. It doesn’t authenticate the user itself — it trusts the token the authorization server issued. In this lab: the ticket mock service.

Here’s the basic flow for a human user:

  1. The user opens the app (client) and clicks “log in.”
  2. The client sends the user to the authorization server’s login page (APM, in our case).
  3. The user authenticates. The authorization server issues an access token and hands it back to the client.
  4. The client calls the resource server, putting the token in the Authorization: Bearer <token> header.
  5. The resource server verifies the token (by checking its signature or calling the authorization server) and either allows or denies the request.

The client never sees the user’s credentials. The resource server never does authentication itself. Everything is delegated through tokens.

What OIDC adds

OAuth 2.0 is about authorization — “what is this client allowed to do?” It doesn’t tell the client who the user is.

OpenID Connect (OIDC) is a thin layer on top of OAuth 2.0 that adds identity. It introduces an ID token — a JWT that contains the user’s identity (their name, email, a stable identifier). The client gets both an access token (for calling resource servers) and an ID token (for knowing who logged in).

For human users, OIDC is what lets an application say “logged in as alice@example.com.” For agents, it’s less central — agents typically care about what they’re authorized to do, not who they are. But the authorization server still uses OIDC-style trust when federating between Keycloak and APM.

Scopes: what the token is allowed to do

A token isn’t a skeleton key. It’s scoped. The scope is a list of permissions the client asked for and the authorization server agreed to grant. A token with scope: read:tickets is not valid for write:tickets.

This is central to tiered governance. A tier 1 agent gets a token scoped to read-only operations on the docs service. If it tries to call the billing service with that token, the resource server (or the gateway in front of it) says no. The token doesn’t cover it.

Grant types: how you get a token

OAuth defines several ways to get a token, called grant types. Two matter most for this lab:

Authorization code — for human users. The user logs in interactively, the authorization server redirects back to the client with a short-lived code, the client exchanges the code for a token. This is the standard browser login flow.

Client credentials — for machines. No user involved. The client presents its own identity (a client ID and secret, or a certificate) directly to the authorization server and gets a token back. Agents use this. There’s no human in the loop, so there’s nothing to redirect to.

1
2
3
4
5
6
7
POST /token
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials
&client_id=ticket-agent
&client_secret=<secret>
&scope=read:tickets write:tickets

The authorization server checks the client’s identity, checks whether it’s allowed to request those scopes, and returns a token.

Dynamic client registration (DCR) — RFC 7591

If client credentials is how an agent gets a token, dynamic client registration is how the agent gets its client_id and client_secret in the first place — without an admin creating it by hand.

A client calls the registration endpoint, describes itself, and the authorization server mints a credential on the spot:

1
2
3
4
5
6
7
8
POST /register
Content-Type: application/json

{
  "client_name": "ticket-agent-pod-7f9d",
  "grant_types": ["client_credentials"],
  "token_endpoint_auth_method": "client_secret_basic"
}

Response: a client_id, a client_secret, and an expiry.

This is powerful for automation — agents can register themselves without manual intervention. It’s also a risk if you don’t protect the registration endpoint. Mitigations: require an initial access token to register, cap what a self-registered client can request (default to the lowest tier), and require explicit authorization to move to a higher tier. More on this in the tier posts.

What APM supports: BIG-IP APM at version 21.1.0 introduced native support for DCR per RFC 7591. IMPORTANT: I haven’t configured this yet, so I’ll confirm this when I get to it, but this is what I understand from reading so far.

In this lab: agent DCR will happen at Keycloak. APM acts as a network-level front door (TLS, rate limiting) but doesn’t have token-level involvement in agent registration.

Token exchange — RFC 8693

This is the hardest piece, and the most important for tier 3.

The problem: an agent gets a client credentials token for itself. That’s fine for tier 1. But for a tier 3 action — say, issuing a refund — the downstream billing service needs to know both who the agent is and which user asked for it. A client credentials token only carries the agent’s identity.

Token exchange solves this. The agent presents the user’s access token (the one APM issued when the support rep logged in) to the authorization server and asks for a new token that is:

  • Scoped narrowly (just the refund operation)
  • Audience-restricted (valid only for the billing service)
  • Carrying both the user’s identity and the agent’s identity in the token claims
1
2
3
4
5
6
7
8
9
POST /token
Content-Type: application/x-www-form-urlencoded

grant_type=urn:ietf:params:oauth:grant-type:token-exchange
&subject_token=<user's access token>
&subject_token_type=urn:ietf:params:oauth:token-type:access_token
&requested_token_type=urn:ietf:params:oauth:token-type:access_token
&audience=billing-service
&scope=billing:refund

The authorization server validates the subject token, makes a policy decision about whether this agent is allowed to exchange it for this scope and audience, and returns a new short-lived token. That new token is what the agent presents to the billing service. The billing service can verify it and see both the user and the agent in the claims.

What APM supports: BIG-IP APM at 21.1.0 does not support RFC 8693 token exchange as a native grant type. IMPORTANT: this is my understanding today, but I still need to verify this.

In this lab: Keycloak handles token exchange. APM is not in the token-level path for agents.

JWTs vs opaque tokens — why the gateway cares

The gateway validates tokens on every request. How it does that depends on the token format.

Opaque token: a random string that means nothing on its own. To validate it, the gateway has to call the authorization server’s introspection endpoint (RFC 7662). That’s an extra network hop on every request.

JWT (JSON Web Token): a signed JSON object containing the claims directly. The gateway can verify the signature locally using the authorization server’s public key. No round trip, faster, works even if the authorization server is temporarily unreachable.

For this lab, we want JWT access tokens so the gateway can verify them without calling back to Keycloak or APM on every agent request. The tradeoff: a JWT can’t be revoked immediately (you have to wait for it to expire). We’ll keep token lifetimes short — minutes, not hours — and use introspection only for sensitive tier 3 operations where we need a revocation check.

How this maps to the lab

OAuth concept This lab
Authorization server Keycloak (agents, token exchange, DCR) + APM (human login path)
Resource server Mock services (docs, ticket, billing) + gateway enforces before the call reaches them
Client (agent) Knowledge agent, ticket agent, refund agent — pods in the cluster
Client credentials grant How agents get tokens from Keycloak
DCR (RFC 7591) How new agents register themselves at Keycloak
Authorization code grant How humans log in through APM
Token exchange (RFC 8693) How the refund agent gets a user+agent-scoped token for the billing service
Scopes Tier controls — what a given token is allowed to call
JWT access tokens What the gateway validates locally on each request

The thing that tripped me up

My initial design had APM as the primary authorization server, with Keycloak as a downstream user store. I was going to have APM issue tokens, with Keycloak just holding user identities and passing them upstream.

It’s actually the other way around. Keycloak is the IdP. APM is the relying party for human logins — users authenticate through APM’s login page, but the user store is Keycloak, and APM federates to Keycloak via OIDC. For agents, Keycloak is the authorization server directly.

The reason: APM doesn’t support RFC 8693 token exchange as a native grant type (at the version we’re running), and token exchange is not negotiable for tier 3. Keycloak does. So Keycloak handles everything token-related for agents; APM adds network-level controls and the human authentication experience.

This took me a while to work out. If you’re designing something similar and you have both a network access policy tool and a dedicated authorization server, check what each one actually supports before you decide which does which job.

There is real value in using F5 APM, even when you have Keycloak purpose-built for modern authentication. BIG-IP has advantages: integrated platform for additional services like WAF, TLS offload, rate limiting, hardware offload, routing checks, L2/3 firewall, device posture check, add legacy methods, existing skillsets, existing user experience, etc.

What’s next

Post 02: Cluster, Keycloak and workload identity — standing up the cluster, deploying Keycloak with the right realm and clients, and getting SPIFFE working so agent pods have identity without static secrets.

Updated: