OAuth 2.0

Also known as: OAuth

On this page 7

OAuth 2.0 is an authorization framework that lets a client application obtain limited access to a protected resource on behalf of a resource owner, by means of access tokens issued by an authorization server instead of the owner’s password. In identity & access management it is the dominant standard for delegated API access, defined in RFC 6749 with bearer token usage in RFC 6750.

OAuth 2.0 answers the question “what may this application do”, not “who is this user”. Authentication is layered on top by OpenID Connect, which adds an ID token. Confusing the two is one of the most persistent sources of design flaws in systems that use OAuth.

Roles and tokens in OAuth 2.0

OAuth 2.0 separates the parties involved in access into four defined roles. This separation is what allows a user to grant an application access without revealing credentials to it.

RoleDescriptionExample
Resource ownerThe entity that can grant access, usually a userA person with a calendar account
ClientThe application requesting accessA scheduling app
Authorization serverAuthenticates the owner and issues tokensThe identity provider’s token service
Resource serverHosts the protected API and validates tokensThe calendar API
The four OAuth 2.0 roles

The central artifact is the access token, a credential representing granted permissions, typically valid for 5 to 60 minutes. It may be an opaque string checked through token introspection or a signed JSON Web Token validated locally. A longer-lived refresh token lets the client obtain new access tokens without involving the user again.

Permissions are expressed as OAuth scopes, such as calendar.read. The user consents to the requested scopes, and the resource server must enforce them. Requesting broad scopes violates least privilege and enlarges the damage if a token leaks.

Grant types and when to use them

A grant type is the method a client uses to obtain tokens. The original specification defined several, but current best practice, consolidated in the OAuth 2.0 Security Best Current Practice and the draft OAuth 2.1, removes the insecure ones.

Grant typeTypical clientCurrent recommendation
Authorization code with PKCEWeb, mobile and single-page appsDefault choice for any user-facing client
Client credentialsServer-to-server servicesRecommended for machine access without a user
Device authorizationTVs, consoles, command-line toolsRecommended for input-constrained devices
Refresh tokenAny client with offline accessUse with rotation or sender constraint
ImplicitLegacy browser appsDeprecated; tokens exposed in URLs
Resource owner passwordLegacy first-party appsDeprecated; client sees the password
OAuth 2.0 grant types and current status

PKCE, Proof Key for Code Exchange, protects the authorization code flow. The client creates a random code verifier, sends its SHA-256 hash with the authorization request, and proves possession of the verifier when redeeming the code. An attacker who intercepts the code cannot exchange it without the verifier.

How the authorization code flow works

  1. The client redirects the user’s browser to the authorization endpoint with client_id, redirect_uri, scope, state and the PKCE code challenge.
  2. The authorization server authenticates the user, possibly with MFA, and asks for consent.
  3. The server redirects back to the registered redirect_uri with a short-lived authorization code.
  4. The client verifies the state value and sends the code with the code verifier to the token endpoint over a back channel.
  5. The server returns an access token and optionally a refresh token, which the client presents to the resource server.

The state parameter protects against cross-site request forgery on the redirect, and exact redirect URI matching prevents an attacker from diverting codes to a domain they control. Authorization codes are single-use and usually expire within about a minute.

OAuth 2.0 is frequently mentioned alongside other identity protocols, and each solves a different problem. The table separates authorization, authentication and federation.

StandardPurposeToken formatTypical use
OAuth 2.0Delegated authorizationAccess token, any formatAPI access for apps
OpenID ConnectAuthentication on top of OAuth 2.0ID token as JWTLogin with an identity provider
SAML 2.0Federated authentication and SSOXML assertionEnterprise web SSO
OAuth 1.0aDelegated authorization with signed requestsToken plus signaturesLegacy APIs
OAuth 2.0 and neighboring identity standards

Using an OAuth access token as proof of login is a classic flaw: a token issued to one application can be replayed against another that accepts it. OpenID Connect fixes this with an ID token containing an audience claim that names the intended client.

Security risks and best practices

Most OAuth incidents result from implementation and configuration errors rather than flaws in the framework itself. Standard access tokens are bearer tokens: whoever holds one can use it, so protecting tokens is critical.

WeaknessConsequenceMitigation
Loose redirect URI matchingCodes or tokens sent to attacker domainsRegister and match exact URIs
Missing PKCEAuthorization code interceptionRequire PKCE for all clients
Overly broad scopesLarge blast radius after token theftRequest minimal scopes
Long-lived tokensExtended misuse windowShort expiry and refresh token rotation
Tokens in browser storageTheft through XSSBackend-for-frontend pattern, HttpOnly cookies
Consent phishingUsers grant malicious apps accessAdmin consent policies and app verification
Frequent OAuth 2.0 weaknesses

Stronger deployments use sender-constrained tokens through DPoP or mutual TLS, so a stolen token is useless without the client’s private key. Token revocation endpoints and short token expiration limit exposure further.

A plain-language summary

OAuth 2.0 is like a valet key for a car. The owner gives the valet a key that can start the car and park it, but cannot open the trunk or glove box. The valet never receives the owner’s full key.

In the same way, when an app asks to read a person’s calendar, the person approves it on the calendar provider’s own page. The app receives a limited, temporary pass instead of the password, and the person can cancel that pass at any time without changing the password.

An example from practice

A company builds a reporting tool that needs to read files from employees’ cloud storage. The developers register the tool with the company’s identity provider and implement the authorization code flow with PKCE, requesting only a read-only files scope. Access tokens expire after 30 minutes, and refresh tokens rotate on every use.

A nightly export job that runs without users uses the client credentials grant with its own narrowly scoped service registration. During a later security review, a leaked log file is found to contain an expired access token. Because of the short lifetime and read-only scope, the token can no longer be used and could never have modified data, so the review records the issue as low impact and the team removes token values from logging.