Coderhouse goes global

🌍

Read more

OAuth 2.0 for AI Agents: How Engineers Connect Agents to Enterprise Apps Securely

AI Engineering

Build and ship real applications with LLMs, agents and automation.

8 weeks · Live online · View course →

Data Analytics

Go from zero to job-ready analyst with SQL, Python and BI tools.

10 weeks · Live online · View course →

Dan Patiño

AI Strategy & Innovation at Coderhouse

Artificial Intelligence

OAuth 2.0 for AI Agents: How Engineers Connect Agents to Enterprise Apps Securely

OAuth 2.0 for AI Agents: How Engineers Connect Agents to Enterprise Apps Securely

OAuth 2.0 for AI agents is the standard way to let an agent act on behalf of a real user inside enterprise apps, such as a CRM, an email suite or a ticketing system, without ever handling that user's password. Instead of a shared API key with full access, the agent receives a short-lived access token limited to specific permissions (scopes) that the user or an administrator approved. It matters because the moment an agent can call tools that read customer data or send messages, identity and permissions become the main thing standing between a helpful assistant and a security incident.

This guide explains how delegated authorization works, which OAuth flows engineers use for agents, and the guardrails that make enterprise teams comfortable putting an agent into production.

Why AI agents need delegated authorization

An agent becomes useful when it can take actions: look up an account in the CRM, create a ticket, draft a reply in the user's mailbox. Each of those is an API call to a system that has its own users and permissions. There are three common ways teams wire this up, and only one scales safely:

  • A shared admin API key. Fast for a demo, dangerous in production: every user gets the same powers, and the logs cannot tell you who actually asked for an action.

  • Storing user passwords. Never acceptable. It breaks single sign-on, multi-factor authentication and most security policies.

  • Delegated access with OAuth 2.0. Each user grants the agent a limited token. The agent can do only what that user is allowed to do, within the scopes granted, and every call is attributable.

The last option is what enterprise security teams expect, and it is defined in the OAuth 2.0 framework (RFC 6749).

The core concepts in plain terms

  • Resource owner: the user whose data the agent will touch.

  • Client: your agent application (the backend that runs the agent, not the language model itself).

  • Authorization server: the identity provider that issues tokens, such as the company's SSO provider or the SaaS vendor's OAuth endpoint.

  • Resource server: the API the agent calls, for example the CRM API.

  • Scopes: named permissions such as read-only contacts or send mail. They define the ceiling of what the token can do.

  • Access and refresh tokens: the access token is short-lived and sent with each API call; the refresh token lets the backend get a new access token without asking the user again.

One rule follows from this: the model never sees the token. The language model decides which tool to call; your backend attaches the right token and makes the request. If you are new to how models trigger tools, our explainer on function calling and tool use in AI agents covers that layer.

Which OAuth flow to use for an agent

Authorization code with PKCE: acting for a user

When the agent works on behalf of a specific person, use the authorization code flow with PKCE (RFC 7636). The user is redirected to sign in with their normal company login, sees a consent screen listing the scopes, and your backend exchanges the resulting code for tokens. PKCE protects that exchange from interception and is now recommended for all clients, not only mobile apps.

Client credentials: service-to-service work

For background jobs that do not belong to any one user, such as nightly enrichment of records, the client credentials flow gives the agent its own identity. Treat it like a service account: give it the narrowest scopes possible and monitor it closely, because there is no human consent step.

Token exchange: passing identity across services

In multi-service architectures, an agent may need to call a downstream API with a token that still represents the original user. OAuth 2.0 Token Exchange (RFC 8693) describes how to swap one token for another with a different audience or narrower scope, instead of forwarding the user's original token everywhere.

Agents that use MCP servers

If your agent connects to tools through the Model Context Protocol, the MCP authorization specification builds on OAuth 2.0, so the same concepts of scopes, tokens and consent apply.

Step by step: connecting an agent to an enterprise API

  1. Register the agent as an OAuth client with the identity provider or SaaS vendor, including exact redirect URIs.

  2. Map each tool to the minimum scopes it needs. A "summarize my open deals" tool needs read access, not write access to every object.

  3. Run the consent flow the first time a user enables the agent, and show clearly what it will be able to do.

  4. Store tokens server-side, encrypted, keyed to the user, never in the prompt, the conversation history or client-side storage.

  5. Attach tokens at execution time. When the model requests a tool call, your backend checks the user, the tool and the scopes, then calls the API.

  6. Refresh and revoke. Handle expired tokens gracefully and let users or admins revoke access at any time.

  7. Log every action with the user, tool, parameters and result so security teams can audit what the agent did.

Agent-specific risks and how to reduce them

OAuth solves who the agent is acting for, but agents add new risks on top of the classic ones covered in the OAuth 2.0 Security Best Current Practice (RFC 9700).

  • Prompt injection triggering privileged actions. A malicious email or document can instruct the agent to forward data or delete records. The token makes the call technically authorized, so you need extra checks. The OWASP Top 10 for LLM applications lists prompt injection and excessive agency among the main risks, and our guide to prompt injection in AI agents explains the attack in detail.

  • Over-broad scopes. Requesting everything up front makes demos easier and incidents worse. Start read-only and add write scopes per tool.

  • Destructive actions without confirmation. Require human approval for sending, deleting, paying or sharing externally.

  • Token leakage through logs and traces. Redact authorization headers and personal data in observability tools.

  • Confused deputy problems. Make sure the agent never uses one user's token to serve another user's request, especially in shared channels.

What this looks like in an engineering role

Applied and forward deployed AI engineers spend a lot of time inside client organizations where the AI part is only half the job. The other half is integration: connecting to identity providers, respecting data governance rules and proving to a security review that the agent cannot do more than it should. Being able to explain delegated identity, scopes and audit trails in plain language is often what gets an agent approved for production.

FAQ

Is an API key ever fine for an AI agent?

For internal prototypes or tools that only read public data, sometimes. As soon as the agent touches user-specific or sensitive data, delegated OAuth access is the safer default.

Does the language model ever handle the token?

It should not. The model chooses the tool and arguments; your backend validates the request and attaches the token.

What is the difference between authentication and authorization here?

Authentication proves who the user is, usually through SSO. Authorization, which OAuth handles, decides what the agent may do on that user's behalf.

Keep learning with Coderhouse

Our AI Engineering Course (Forward Deployed AI Engineering) is designed for developers comfortable with Python. Over 8 weeks, with one 2-hour live class per week and about 6 to 8 hours of work weekly, it covers RAG, evaluation, tool-using agents and enterprise integration, including legacy systems, webhooks, delegated identity with OAuth2 and data governance, ending with a capstone you hand off. If you also want to measure the business impact of the systems you build, the Data Analytics Course covers SQL, Python, statistics and experimentation.

Dan Patiño

I'm Dan Patiño, head of AI Strategy & Innovation at Coderhouse. My day-to-day work involves merging the tactical management of e-commerce (CRO, Email Marketing and SEO) with the development of disruptive solutions. I specialize in building internal AI-powered apps to automate tasks and boost innovation within the team. I firmly believe that technology is strategy's best ally. To dive deeper into my professional journey, I'll be waiting for you on my LinkedIn profile.