Skip to content

Managed OAuth

When you protect an application with Cloudflare Access, by default non-browser clients — such as CLIs, AI agents, SDKs, and scripts — cannot complete the browser-based login redirect. They receive a 302 redirect with no usable token or authorization endpoint.

Managed OAuth solves this by turning Access into a standard OAuth 2.0 authorization server for your application. Access enforces the same policies as a browser login, and your origin sees no difference.

Prerequisites

Enable managed OAuth on a self-hosted application

  1. In the Cloudflare dashboard, go to Zero Trust > Access controls > Applications.
  2. Find the application you want to configure, then select the three dots on the right > Edit.
  3. Go to the Advanced settings tab and turn on Managed OAuth.
  4. (Optional) Configure Managed OAuth settings.
  5. Select Save application.

To test, open an RFC 8707-compliant OAuth client and make a request to your application. The client should open a browser window prompting you to log in to Access. Refer to the Authorization flow section for more details.

Enable managed OAuth on an MCP server portal

Managed OAuth is available on MCP server portals and is the mechanism that allows MCP clients to authenticate users through the portal without a browser cookie flow.

  1. In the Cloudflare dashboard, go to Zero Trust > Access controls > AI controls.
  2. Find the portal you want to configure, then select the three dots on the right > Edit.
  3. Go to the Advanced settings tab, turn on Managed OAuth.
  4. (Optional) Configure Managed OAuth settings.
  5. Select Save application.

To test, open an MCP client and connect to the MCP portal. The client should open a browser window prompting you to log in to Access. Refer to the Authorization flow section for more details.

Managed OAuth settings

Configure these settings in the Advanced settings tab of your self-hosted app or MCP server portal.

  • Allow localhost clients: Allow any client with redirect URIs on localhost.
  • Allow loopback clients: Allow any client with redirect URIs on 127.0.0.1.
  • Allowed redirect URIs: Redirect URIs allowed for dynamically registered clients (for example, https://playground.ai.cloudflare.com/*). The URL must use https. Paths may end in /* to match all sub-paths.
  • Grant session duration: How long the OAuth refresh token remains valid.
  • Access token lifetime: How long an OIDC Access token can be used to authenticate with your application. Cloudflare recommends configuring a short Access token lifetime (default 15 minutes) in conjunction with a longer Grant session duration. When the access token expires, Cloudflare uses the refresh token to issue a new one after re-evaluating the user against your Access policies. When the refresh token expires, the user must re-authenticate with the identity provider.

Authorization flow

When managed OAuth is enabled, Access returns a 401 response instead of a 302 redirect to non-browser clients. The 401 includes a WWW-Authenticate header that points the client to Access's OAuth discovery metadata.

The authorization flow proceeds as follows:

  1. The client fetches the OAuth authorization server metadata from the /.well-known/ endpoint:

    https://<your-app-domain>/.well-known/oauth-authorization-server

    This endpoint conforms to RFC 8414 and RFC 9728 and returns the authorization and token endpoint URLs for the application.

  2. The client initiates an authorization code flow. It opens the user's browser to the Access authorization endpoint, where the user logs in to their IdP as usual.

  3. Access issues an OAuth access token to the client. The client uses this token in subsequent requests to the protected application.

Managed OAuth vs service tokens

Both managed OAuth and service tokens allow non-browser clients to authenticate with Access-protected applications, but they serve different use cases:

Managed OAuthService tokens
Authentication modelUser-based — the end user logs in through their identity providerMachine-based — a shared secret authenticates the service itself
Best forInteractive CLI tools, AI agents, SDKs where a human initiates the requestFully automated systems, cron jobs, CI/CD pipelines, server-to-server communication
User identityAccess knows which user made the requestNo user identity — requests are attributed to the service token
Policy enforcementCan use identity-based policies (for example, require specific groups or emails)Requires a Service Auth policy action
Credential managementNo shared secrets to distribute — users authenticate with their own credentialsRequires distributing and rotating Client ID and Client Secret

Use managed OAuth when you want non-browser clients to authenticate users the same way a browser would — the user logs in once, and the client receives an OAuth token to make requests on their behalf.

Use service tokens when no human is involved and you need a machine identity to access your application programmatically.