What Is OAuth, and How Does It Work?

OAuth is an open standard authorization protocol that lets applications access user data from third-party services without ever seeing or storing the user's password. If you're building AI agents that pull data from Slack, Google Drive, Notion, or a CRM, OAuth is the mechanism that makes secure, delegated access possible.

TL;DR

  • OAuth is an authorization protocol that lets applications access user data from third-party services without handling user passwords, using scoped, time-limited access tokens instead
  • The authorization code flow with PKCE is the recommended approach for most use cases, including AI agents that need to access user data from platforms like Slack, Google Drive, or Notion
  • Key security practices include storing tokens securely (never in localStorage), using HTTPS everywhere, validating state parameters for CSRF protection, and requesting only minimum necessary scopes
  • OAuth handles authorization only. For authentication ("Login with Google"), you need OpenID Connect (OIDC). For compliance (HIPAA, SOC 2), OAuth is one component requiring additional legal agreements and technical controls


What Problem Does OAuth Solve?

Before OAuth, the only way for a third-party application to access user data on another platform was to collect the user's password directly and log in on their behalf. This creates compounding problems for AI agents that connect to customer data across multiple services.

An agent holding raw credentials has complete, unscoped access to each account with no mechanism to limit what it can do. OAuth replaces this with time-limited, scoped access tokens that authorize specific actions. An agent can request only channels:read from Slack, and the authorization server blocks any attempt to use chat:write permissions that were never granted.

Credential storage also turns every agent deployment into a breach surface. An agent storing passwords for 500 enterprise users means a single database compromise yields working credentials across email systems, cloud storage, and internal networks. As RFC 6749 states: "Compromise of any third-party application results in compromise of the end-user's password and all of the data protected by that password."

OAuth's token model also solves revocation. A user can revoke a specific access token without changing their password, leaving other applications completely unaffected.

One important distinction: OAuth 2.0 is an authorization framework, not an authentication protocol. It grants permission to access resources but provides no mechanism to verify who the user is. Applications that need both, like "Login with Google" flows, require OpenID Connect (OIDC), which layers identity verification on top of OAuth.

What Are the Key OAuth Components?

OAuth 2.0 defines four roles in the authorization process.

Resource Owner

The person who owns the data: the employee with a Slack account or the user who owns files in Google Drive. They grant or deny access to their protected resources.

Client

The application requesting access. An AI agent that reads Slack messages acts as the client in the OAuth flow. Clients can be confidential (server-side apps that securely store secrets) or public (browser or mobile apps that cannot). This distinction determines which grant types and token handling strategies are appropriate.

Authorization Server

The server that authenticates the user and issues access tokens after approval. For Slack, this is Slack's OAuth infrastructure. For Google Drive, it's Google's authorization server at accounts.google.com.

Resource Server

The API hosting the protected data: the Slack API serving messages or the Google Drive API providing file access. It validates access tokens and returns data if the token grants sufficient permission.

How Does the OAuth Authorization Code Flow Work?

The authorization code flow is the most widely used OAuth flow. It involves six steps between your application, the user, and the authorization server.

Step What Happens
1. Redirect Your AI agent redirects the user's browser to the authorization endpoint with your client ID, requested scopes, a redirect URI, and a random state value for Cross-Site Request Forgery (CSRF) protection.
2. Authenticate The authorization server presents a login page, then a consent screen listing requested permissions like channels:read and channels:history. The user reviews and approves.
3. Authorization code The server redirects the user's browser back to your redirect URI with a short-lived authorization code that expires within 10 minutes and can only be used once.
4. Token exchange Your backend makes a server-to-server POST request to the token endpoint with the authorization code, your client ID, client secret, redirect URI, and a PKCE code verifier.
5. Token response The authorization server validates everything and responds with an access token, a refresh token (if you requested offline_access), token expiration, and granted scopes.
6. API access Your AI agent includes the access token in the HTTP Authorization header (Authorization: Bearer <access_token>). The resource server validates the token, checks scopes, and returns data.

What Are the Different OAuth Grant Types?

Authorization Code with PKCE

Use authorization code with PKCE when an AI agent accesses data on behalf of a specific user. The user authenticates, approves specific scopes, and your agent receives tokens tied to that user's permissions. Security best practices now recommend PKCE for all client types, and single-page and mobile apps require it.

Client Credentials

This grant type handles machine-to-machine communication with no user involved. Your AI agent authenticates as itself using a client ID and secret, receiving a token representing application-level permissions. This is appropriate for autonomous operations like background data processing or system monitoring, not for user-specific data access.

Refresh Tokens

Refresh tokens are not a standalone grant type but a mechanism that extends other flows. When an access token expires, your application uses the refresh token to get a new access token without requiring user interaction. For AI agents that need persistent access, like summarizing a week of Slack messages or continuously monitoring a CRM, refresh tokens are critical. Confidential clients should use refresh tokens with rotation, where each refresh invalidates the previous token. Public clients should not receive refresh tokens and should instead request new access tokens from the authorization server.

Deprecated Flows to Avoid

The implicit flow and password credentials grant are both deprecated per RFC 9700. The implicit flow exposes tokens in URL fragments, and the password grant requires collecting user credentials directly. Use authorization code with PKCE instead.

How Do You Implement OAuth for AI Agents?

Consider an AI assistant that summarizes a user's week: it reads Slack messages, pulls documents from Google Drive, and checks tasks in Notion. With OAuth, the user authenticates once with each platform, grants specific scopes, and your agent receives tokens for each service.

LangChain and other frameworks handle AI agent integrations with OAuth-authenticated services. GoogleDriveLoader handles Drive access, NotionAPILoader works with OAuth tokens for Notion pages and databases, and Slack's OAuth flow supports incremental authorization where tokens accumulate scopes across multiple flows. 

Token Vault Architecture

The emerging pattern for production AI agent platforms involves token vault architecture, where the agent never directly handles tokens. According to Auth0's LangChain SDK documentation, the agent references tokens by identifier, and the vault retrieves and uses the token internally. This keeps tokens out of LLM context windows, agent logs, and debugging outputs.

Agent-Specific Pitfalls

Authorization drift occurs when an agent retains credentials long after the original task completes. Implement time-bounded agent access controls to prevent this.. Permission intersection gaps arise when an agent accesses data using one user's permissions but outputs results to a shared workspace where users with different permission levels can see them. Restrict shared output to data the viewing user has independently authorized. 

Libraries and Code Example

For Python, Authlib supports full OAuth 2.0 and OIDC with built-in PKCE, async/await, and automatic token lifecycle management. For JavaScript/Node.js, Passport.js with its OAuth2 strategy handles Express integration.

from authlib.integrations.requests_client import OAuth2Session
from authlib.common.security import generate_token

# Initialize OAuth2Session with PKCE
client = OAuth2Session(
    client_id='your_client_id',
    scope='openid profile',
    code_challenge_method='S256',
    code_verifier=generate_code_verifier()
)

# Generate code verifier (43-128 characters) for PKCE
code_verifier = generate_token(48)

# Create authorization URL with PKCE
# The code_challenge is automatically generated from code_verifier
uri, state = client.create_authorization_url(
    'https://authorization-server.com/oauth/authorize',
    code_verifier=code_verifier
)

# After user authorization, fetch token using the code_verifier
token = client.fetch_token(
    'https://authorization-server.com/oauth/token',
    authorization_response='https://your-app.com/callback?code=AUTH_CODE&state=STATE',
    code_verifier=code_verifier
)


What Security Best Practices Should You Follow?

Token Storage

Never store access tokens in browser localStorage or sessionStorage, since both are vulnerable to Cross-Site Scripting (XSS) attacks. Access tokens belong in memory, with refresh tokens in HttpOnly, Secure, SameSite=Strict cookies. Server-side AI agents should use encrypted storage with a token vault architecture to keep credentials out of application logs and LLM context windows.

Scope Limitation

Request only the minimum scopes the agent needs. Over-scoped tokens are a common source of data leaks in agent applications, where a single token with broad permissions can expose data across an entire workspace. According to the OWASP OAuth Cheat Sheet, resource servers must check that incoming tokens contain required scopes and reject requests with insufficient permissions using HTTP 403 responses.

Callback Validation

The redirect URI in the token exchange must exactly match the one in the authorization request, or the server will reject it. This is a frequent source of silent failures in agent platforms that manage OAuth flows for multiple data sources, since each source has its own redirect configuration. Generate a cryptographically random state parameter (at least 32 bytes of entropy), store it in the user's session, and validate it when the authorization server redirects back. Reject any callback with a missing or mismatched state value.

Token Lifetimes

Keep access tokens short-lived (15 minutes for browser apps, 1 hour maximum for confidential clients). Authorization codes should expire within 10 minutes. Agents running long tasks, like indexing a full Google Drive or processing a backlog of support tickets, need refresh token logic built into the pipeline rather than assuming the original access token will last.

How Does OAuth Support Enterprise Compliance?

OAuth provides authorization controls that support compliance efforts, but achieving SOC 2, HIPAA, or PCI-DSS requires additional AI agent security controls at the authentication, encryption, and governance levels.

OAuth is one layer in a broader framework. HIPAA, for example, requires a signed Business Associate Agreement (BAA) with your identity provider before handling Protected Health Information (PHI). Row-level security requires propagating the authorizing user's identity through the entire request chain so the data layer enforces policies using the authenticated user's identity, not the agent's service account. Log all OAuth token issuance, usage, refresh, and revocation events with enough detail to answer "who accessed what data, when, and through which agent" during audits.

What's the Best Way to Implement Secure Data Access for AI Agents?

OAuth gives AI agents scoped, time-limited, revocable access to user data across platforms without credential sharing. The harder problem is everything underneath: building and maintaining the connectors, managing token lifecycles across dozens of services, and enforcing permissions that don't drift as agents scale.

Airbyte's Agent Engine handles this infrastructure layer. It manages OAuth token storage and refresh across hundreds of data sources, maps fine-grained scope definitions to specific data types, and logs all access with attribution to both users and agents so teams can answer audit questions without rebuilding their own token management.

Request a demo to see how Airbyte powers production AI agents with reliable, permission-aware data access.

You build the agent. We'll bring the data.

Authenticate once. Fetch, search, and write in real-time.

Try Agent Engine →
Airbyte mascot

Frequently Asked Questions

What is the difference between OAuth and OpenID Connect?

OAuth 2.0 grants applications permission to access resources through scope-limited access tokens. OpenID Connect (OIDC) builds on top of OAuth and adds identity verification. When applications need to both authenticate users and access their data, they use OIDC for authentication combined with OAuth for authorization.

Can you use OAuth for machine-to-machine communication without a user?

Yes. The client credentials grant type handles this case. Your application authenticates using its own client ID and secret and receives a token representing application-level permissions rather than a specific user's access.

What happens when an OAuth access token expires mid-request?

The API returns an HTTP 401 Unauthorized response. Your application should catch this, use its refresh token to request a new access token, and retry the original request. Most OAuth libraries handle this automatically with token refresh interceptors.

How should agents handle OAuth across multiple services simultaneously?

Store tokens per service and per user in a centralized token vault keyed by a combination of user ID, service name, and scope set. Each service has its own token lifecycle, so your agent needs independent refresh logic for each connection. Avoid sharing tokens across services even when the same identity provider issues them, since scopes and expiration policies differ.

Does implementing OAuth make an application HIPAA-compliant?

No. OAuth is a technical authorization framework, not a compliance certification. HIPAA compliance requires a signed Business Associate Agreement (BAA) with your identity provider, data encryption in transit and at rest, audit logging, and access controls enforcing minimum necessary standards. Auth0 and Okta offer BAAs only on enterprise plans. OAuth handles only the authorization layer.

Loading more...

Try the Agent Engine

We're building the future of agent data infrastructure. Be amongst the first to explore our new platform and get access to our latest features.