Authentication

Demystifying OAuth Flows

Demystifying OAuth Flows

An OAuth flow (or grant type) is the sequence of steps an application follows to get an access token from an authorization server. Which flow you use depends on the kind of app: web apps, single-page apps and mobile apps should use the authorization code flow with PKCE, backend services use the client credentials flow, and TVs and command-line tools use the device authorization flow. The implicit and resource owner password flows are now discouraged by the IETF’s OAuth security guidance.

This is part of a series of articles about OAuth

Here are some examples of OAuth flows:

  • Authorization code flow: This flow allows a client application to access protected resources like web APIs.  It’s designed for confidential applications, such as regular web applications. The app’s client secret is stored securely on the app’s server. This flow is suitable for server-side web applications where the source code is not exposed publicly.
  • Client credentials flow: This is an OAuth 2.0 grant type where applications request access tokens directly without user intervention. Used when the client is also the resource owner, it authenticates the application using its client ID and secret. Suitable for server-to-server authentication, the returned access token grants access to specific, predefined scopes.
  • Resource owner password credentials: This is an OAuth 2.0 grant type in which the resource owner (typically the user) provides their service credentials (username and password) directly to the client application. The client then exchanges these credentials for an access token from the authorization server. Current guidance says not to use it.
  • Hybrid flow: Suitable for applications that can store client secrets, this flow enables immediate access to ID tokens alongside ongoing access to refresh tokens. 
  • Device authorization flow: This flow is suitable for input-constrained devices that connect to the internet. Instead of authenticating the user directly, the device asks the user to go to a link on their computer or smartphone and authorize the device.
  • PKCE (Proof Key for Code Exchange): An extension to the authorization code flow that stops a stolen authorization code from being used. It’s required for public clients, like mobile apps and single-page apps, which can’t keep a client secret, and recommended for all other apps.

Which OAuth flow should you use?

App type Recommended flow Why
Server-side web app Authorization code (with PKCE) The server keeps the client secret; tokens never pass through the browser
Single-page app (SPA) Authorization code with PKCE No secret in the browser; PKCE stops stolen codes from being used
Native mobile or desktop app Authorization code with PKCE Same reason as SPAs; the app can’t keep a secret
Backend service, job or API integration (no user) Client credentials The app authenticates as itself; see M2M authentication
TV, console, CLI or IoT device Device authorization The user approves on a phone or laptop instead of typing on the device
Legacy app that collects passwords Resource owner password (avoid) RFC 9700 says it MUST NOT be used; migrate to authorization code
Browser app using the implicit flow Implicit (avoid) RFC 9700 says clients SHOULD NOT use it; switch to authorization code with PKCE
Source: OAuth 2.0 Security Best Current Practice, RFC 9700 (January 2025).

The main difference between the authorization code flow and the client credentials flow is that the client credentials flow relies on application authorization rather than involving the user.

OAuth flows use the following roles:

  • Resource Owner — This is basically an entity that grants access to protected resources. More often than not, this is a user.
  • Resource Server — This is the server that holds the resources the Resource Owner needs access to. The Resource Server exposes an API, and the objective of OAuth is to grant secure access to this API.
  • Client — This is an application that requests access to protected resources, typically for a Resource Owner using the application.
  • Authorization Server — This is a server responsible for accepting credentials from the Resource Owner, checking if they are authorized to access a restricted resource, and providing an access token to allow access.

Note that OAuth 2.0 is focused solely on authorization, while OpenID Connect (OIDC) supports authentication and authorization. OIDC has more stringent standards and integrated security features than OAuth2.

This is part of an extensive series of guides about access management.

The OAuth Authorization Process

To add more context, the general OAuth authorization process, which is relevant for all OAuth flows, works as follows. Each OAuth flow may implement these steps a bit differently based on the application’s needs and security requirements.

  1. The user shows intent: This is usually the point where the user clicks on a “Login” button or accesses a restricted resource that requires them to sign in. It signifies the user’s intent to initiate the OAuth process to grant access to their data.
  2. The consumer gets permission: Here, the client (often referred to as the consumer) prepares an OAuth request and directs the user to the OAuth authorization server. The server will usually show a consent screen, listing the kinds of data the client wants to access and what actions it wants to perform.
  3. The user is redirected to the service provider: The user is redirected to the authorization server’s UI, often a web page, where they must log in and approve the permissions requested by the client.
  4. The user gives permission: Once the user has authenticated, they give consent for the permissions requested. This is usually done by clicking an “Approve” button on the authorization server’s UI.
  5. The consumer obtains an access token: After successful authentication and approval, the user is redirected back to the client with an authorization code (or directly with an access token in case of Implicit Flow). The client then exchanges this code for an access token by making a server-to-server request.

The consumer accesses the protected resource: Finally, the client uses the acquired access token to access the protected resources hosted by the resource server. Depending on the OAuth flow, this token can be short-lived and optionally refreshed.

OAuth Flow Types

Without further ado, let’s dive into the OAuth Flow types you need to know before getting started and working on your specific use case/s.

1. Authorization Code Flow

The authorization code flow signs the user in at the authorization server, sends a one-time code back to the app, and the app exchanges that code for tokens over a direct back-channel request. A server-side app also sends its client secret, which it keeps on the server. Add PKCE to this flow for every app type; it’s required for apps that can’t keep a secret.

Use Cases: Server side web applications where the source code is not exposed publicly.

How this OAuth flow works:

  1. The user clicks on a login link in the web application.
  2. The user is redirected to an OAuth authorization server, after which an OAuth login prompt is issued.
  3. The user provides credentials according to the enabled login options.
  4. Typically, the user is shown a list of permissions that will be granted to the web application by logging in and granting consent.
  5. The user is redirected to the application, with the authorization server providing a one-time authorization code.
  6. The app receives the user’s authorization code and forwards it along with the Client ID and Client Secret, to the OAuth authorization server.
  7. The authorization server generates an ID Token, Access Token, and an optional Refresh Token, before providing them to the app.
  8. The web application can then use the Access Token to gain access to the target API with the user’s credentials.

2. Client Credentials Flow

The client credentials flow lets an application get an access token for itself, using its own client ID and secret (or a private key or certificate), with no user involved. It’s the standard flow for machine-to-machine (M2M) authentication, and it doesn’t issue refresh tokens: the app requests a new access token when the old one expires.

Relevant for: M2M apps (daemons, back-end services, and CLIs). In these types of apps, the system authenticates and grants permission behind the scenes without involving the user, because the “user” is often a machine or service role. It doesn’t make sense to show a login prompt or use social logins.

How this OAuth flow works:

  1. The application authenticates with the OAuth authorization server, passing the Client Secret and Client ID.
  2. The authorization server checks the Client Secret and Client ID and returns an Access Token to the application.
  3. The application sends the Access Token to the target API, which checks it and allows the actions in its scopes.

For the methods and best practices behind it, see our guide to machine-to-machine authentication.

3. Resource Owner Password Flow

The resource owner password flow has the app collect the user’s username and password and send them to the authorization server in exchange for a token. Because the app sees the user’s password, and the flow can’t support MFA, single sign-on or passkeys, the IETF’s OAuth security guidance (RFC 9700) says it MUST NOT be used. Existing apps should move to the authorization code flow.

Use Cases: Legacy first-party apps that haven’t migrated yet. Don’t use it for new apps.

How this OAuth flow works:

  1. The user clicks a login link in the application and enters credentials into a form managed by the app.
  2. The application passes the credentials to the OAuth authorization server. It must never store them.
  3. The authorization server validates credentials and returns the Access Token (and an optional Refresh Token).
  4. The app can now access the target API with the user’s credentials.
Resource Owner Password Flow: the resource owner gives credentials to the client, which sends them to the authorization server for an access token and optional refresh token
Resource Owner Password Flow

The following flows are less commonly used, so we’ll only cover them briefly.

4. Implicit Flow with Form Post

The implicit flow returns tokens directly in the browser redirect, with no code exchange. Returning access tokens this way (response_type=token) is discouraged: RFC 9700 says clients SHOULD NOT use it, because tokens can leak through browser history and logs. The form post variant, which only returns an ID token to sign the user in to a server-side web app (much like SAML or WS-Federation), is still used, but the authorization code flow is the better default.

Use Cases: Legacy sign-in to server-side web apps. For new apps, use the authorization code flow with PKCE.

Related: OAuth vs SAML

5. Hybrid Flow

The hybrid flow is an OpenID Connect flow that returns an ID token right away in the redirect and an authorization code that the app’s server exchanges for access and refresh tokens. This flow can benefit apps that can securely retain Client Secrets. It lets your app obtain immediate access to an ID token, while enabling ongoing retrieval of additional access and refresh tokens. This is useful for apps that need to immediately gain access to data about the user, but must perform some processing prior to gaining access to protected resources for a long time.

Use Cases: Apps that need immediate access to data about the user, but also need to use this data on an ongoing basis.

6. Device Authorization Flow

The device authorization flow (RFC 8628) lets a user sign in on a device with no keyboard or browser, such as a smart TV, game console, command-line tool or IoT device, by approving the sign-in on their phone or laptop.

How it works:

  1. The device asks the authorization server for a device code and gets back a short user code (like WDJB-MJHT) and a verification URL.
  2. The device shows the code and URL, often as a QR code.
  3. The user opens the URL on another device, signs in and approves.
  4. Meanwhile, the device polls the token endpoint every few seconds. Once the user approves, it receives its tokens.

Use Cases: Smart TVs, streaming devices, consoles, CLIs and IoT devices that are online but hard to type on.

Device flow in Frontegg: Frontegg supports the device authorization flow. The device requests a code from Frontegg’s device authorization endpoint and polls Frontegg’s token endpoint, while you host a verification page in your web app where the signed-in user sees the app name and requested scopes and approves or denies the request.

7. Authorization Code Flow with PKCE

PKCE (Proof Key for Code Exchange, RFC 7636) adds a one-time secret to the authorization code flow so that only the app that started the sign-in can exchange the code for tokens. The app creates a random code verifier, sends a hash of it (the code challenge) with the authorization request, and sends the verifier itself when it exchanges the code. The authorization server checks that they match.

Use Cases: Required for single-page apps and mobile and desktop apps, and recommended for server-side web apps too.

Related: OAuth Grant Types

Which OAuth Flow Is the Right One?

The OAuth 2.0 Authorization Framework powers various authorization flows and grants. Flows are means of obtaining Access Tokens. Selecting the right flow for your use case depends on your app type, but you should also consider other parameters like the client’s level of trust and the user experience.

When choosing an OAuth Flow, take into account the following:

  • Resource owner — This is an entity that may grant access to resources that are protected. Generally, this is an end-user.
  • Client — This is an application asking for access to restricted resources in the name of the Resource Owner.
  • Resource server — This is a server hosting the safeguarded resources. This is the API you need to access.
  • Authorization server — This is the server that authenticates the Resource Owner and provides Access Tokens once it’s authorized.
  • User agent — This entity enables interaction between a Resource Owner and Client (for instance, a native or a browser application).

Is the Client the Resource Owner?

The type of authorization flow is determined by the type of client, and whether it is a human or machine. In purely machine authorization, the Client is the same as the Resource Owner, so the end-user doesn’t need to provide any additional authorization. In this case, use Client Credentials Flow.

Is the Client a Web App Executing on the Server?

When the Client is a typical web app running via a server, you should apply the Authorization Code Flow. It provides an Access Token (and an optional Refresh Token) directly to the Client. This is a safer approach than having the Access Token pass through the user’s web browser, which may lead to exposure. Add PKCE as well.

Is the Client Completely Trusted with User Credentials?

Even if it is, don’t use the resource owner password flow for new apps. RFC 9700 says it MUST NOT be used. Use the authorization code flow with PKCE, which also lets you add MFA, passkeys and single sign-on.

Is the Client a Single-Page Application?

Use the authorization code flow with PKCE. The implicit flow was once the SPA default, but current guidance says not to use it. Many teams also keep tokens out of the browser entirely with a backend-for-frontend that holds them on the server.

Is the Client a Native Mobile Application?

When using native applications, you should probably opt for the PKCE (Proof Key for Code Exchange) Authorization Code Flow. Learn more about the Authorization Code Flow with PKCE.

Is the Client a Device Without a Browser or Keyboard?

Use the device authorization flow. The user approves the sign-in on their phone or computer.

Summing it Up

As you must have already realized by now, the OAuth Flow needs to be picked wisely to optimize your use case and address specific challenges. For example, you may have a single app that requires access tokens for various resource servers. Multiple calls to /authorize will be needed. Learn more in the IETF draft on OAuth 2.0 audience information. Making the wrong choice can have serious security and compliance implications today. 

Before getting started, you may also want to check out RFC 6749 and RFC 7636 to better understand the steps involved in implementing these flows.

The IETF’s OAuth 2.1 draft consolidates this guidance: it requires PKCE for the authorization code flow and leaves out the implicit and password grants.

FAQ

What are the main OAuth flows?

Authorization code (with PKCE) for apps where a user signs in, client credentials for machine-to-machine access, and device authorization for devices without a keyboard or browser. The implicit and resource owner password flows still exist but are discouraged.

What is the difference between the authorization code flow and the client credentials flow?

The authorization code flow gets a token on behalf of a signed-in user. The client credentials flow gets a token for the application itself, with no user involved, and is used for service-to-service calls.

Which OAuth flow should a single-page app use?

The authorization code flow with PKCE. The implicit flow, once common for SPAs, is no longer recommended.

Is the implicit flow deprecated?

The IETF’s OAuth 2.0 Security Best Current Practice (RFC 9700) says clients should not use the implicit grant, and the OAuth 2.1 draft removes it. Use the authorization code flow with PKCE instead.

What is PKCE in OAuth?

PKCE (Proof Key for Code Exchange) is an extension to the authorization code flow. The app proves that it started the sign-in by sending a secret code verifier when it exchanges the authorization code, so a stolen code is useless on its own.

See Additional Guides on Key Access Management Topics

ABAC

Authored by Frontegg

RBAC

Authored by Frontegg

User Management

Authored by Frontegg