Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
LABS
Glossary

Authorization Code Flow

An OAuth 2.0 authorization grant flow where a client exchanges an authorization code for an access token, commonly adapted for secure, user-centric authentication in decentralized identity (DID) systems.
Chainscore © 2026
definition
OAUTH 2.0

What is Authorization Code Flow?

The Authorization Code Flow is a server-side OAuth 2.0 grant type designed for secure user authentication and resource access delegation.

The Authorization Code Flow is an OAuth 2.0 grant type where a client application obtains an authorization code from an authorization server and then exchanges it for an access token and often a refresh token. This two-step exchange, which separates the user's consent from the token issuance, is a critical security feature. It is the recommended flow for server-side web applications and native mobile apps that can securely store a client secret, as it prevents the access token from being exposed to the user's browser or mobile operating system.

The flow begins when the client redirects the user to the authorization server's /authorize endpoint. The user authenticates and grants consent for the requested permissions, or scopes. Upon approval, the server redirects the user back to the client's pre-registered redirect URI, appending a short-lived, single-use authorization code to the URL. A key security aspect is that this code is transmitted via the browser but is useless without the subsequent server-to-server exchange.

The client application then makes a back-channel request directly from its server to the authorization server's /token endpoint. This request includes the authorization code, the client's client_id and client_secret, and the same redirect URI. The authorization server validates these credentials and, if successful, responds with a JSON payload containing the access_token, refresh_token, expires_in, and token_type. This server-to-server communication ensures sensitive credentials are never exposed to the public client-side environment.

A primary advantage of this flow is the support for refresh tokens. When the short-lived access token expires, the client can use the refresh token to obtain a new access token without requiring the user to log in again. This maintains a persistent, secure session. The flow also enables the use of the Proof Key for Code Exchange (PKCE) extension, which adds a cryptographic challenge to protect the authorization code even in public clients like single-page applications (SPAs) that cannot securely store a client secret.

In practice, the Authorization Code Flow is foundational for integrating with major identity providers like Google, GitHub, and Microsoft Entra ID. It is the standard method for implementing "Sign in with X" functionality, allowing users to grant a third-party application access to their profile data or other protected resources hosted by the provider. Its design prioritizes security over simplicity, making it the most robust OAuth 2.0 flow for authenticating users and delegating API access.

how-it-works
OAUTH 2.0 PROTOCOL

How Authorization Code Flow Works

The Authorization Code Flow is a server-side OAuth 2.0 grant type designed to securely obtain an access token for a user, particularly in web and mobile applications. It separates the user authentication step from the client's request for access, preventing the exposure of sensitive credentials.

The Authorization Code Flow is initiated when a user clicks a login button, redirecting their browser to the authorization server (e.g., Google, GitHub). This server authenticates the user, often via a login form, and requests their consent for the client application to access specified resources. Upon approval, the server redirects the user back to the client application with a single-use authorization code appended to the URL as a query parameter. This code is not an access token itself but a short-lived credential used in the next step.

The client application, running on its secure backend server, then exchanges the received authorization code for an access token and often a refresh token. This exchange is a direct, server-to-server POST request to the authorization server's token endpoint, where the client must authenticate itself using its client_id and client_secret. This backend exchange is critical for security, as it ensures the sensitive access token is never exposed to the user's browser or mobile app, mitigating risks like token theft through browser history or malicious JavaScript.

Once the client possesses the access token, it can use it to make authorized API calls to the resource server (e.g., a user's Gmail or cloud storage) on behalf of the user. The access token is typically included in the Authorization header of HTTP requests. If the access token expires, the client can use the refresh token to obtain a new one without requiring the user to log in again, providing a seamless user experience for long-lived sessions.

key-features
OAUTH 2.0

Key Features

The Authorization Code Flow is a server-side, multi-step OAuth 2.0 grant designed for confidential clients to securely obtain access tokens.

01

Server-Side Security

This flow is designed for confidential clients (e.g., web servers) that can securely store a client secret. It keeps the access token and refresh token on the server, never exposing them to the user's browser, which mitigates token theft risks inherent in single-page applications.

02

Multi-Step Exchange

The flow involves a two-step token exchange:

  • The client first redirects the user to obtain an authorization code.
  • The client then exchanges this short-lived code, along with its client secret, for an access token and a refresh token via a secure back-channel request. This separation enhances security.
03

PKCE Extension

The Proof Key for Code Exchange (PKCE, pronounced 'pixy') extension (RFC 7636) is now recommended for all clients, including confidential ones. It protects against authorization code interception attacks by having the client create and verify a cryptographic code challenge.

04

Primary Use Case

This is the standard flow for traditional web applications where the server-side code can handle the full sequence. It is the most secure OAuth 2.0 flow for applications that can maintain client credential confidentiality and is mandated by many identity providers for sensitive data access.

05

Sequence Diagram

The canonical flow follows these steps:

  1. Authorization Request: User is redirected to the Authorization Server.
  2. User Consent: User authenticates and grants consent.
  3. Authorization Code: Server redirects back to the client with a code.
  4. Token Request: Client exchanges the code + secret for tokens.
  5. API Access: Client uses the access token to call the Resource Server.
06

Contrast with Implicit Flow

Unlike the deprecated Implicit Flow, which returned tokens directly in the URL fragment (risky for SPAs), the Authorization Code Flow uses a back-channel exchange. This makes it more secure and allows for the use of refresh tokens, enabling long-lived sessions without re-prompting the user.

did-auth-adaptation
DID AUTH ADAPTATION

Authorization Code Flow

An adaptation of the OAuth 2.0 authorization code flow for decentralized identity systems, enabling secure, user-centric authentication and authorization without centralized identity providers.

The Authorization Code Flow in a Decentralized Identity (DID Auth) context is an OAuth 2.0-compatible protocol where a Relying Party (RP) obtains an authorization code from a user's DID Controller (often via a wallet) and exchanges it for Verifiable Credentials (VCs) or access tokens. This flow, sometimes called SIOPv2 (Self-Issued OpenID Provider v2), allows users to authenticate directly from their personal digital wallet, which acts as both the identity provider and authorization server. The core innovation is replacing the traditional OAuth client_id with the user's own Decentralized Identifier (DID), shifting control of the authentication process to the user's device.

The flow begins when the Relying Party sends an Authorization Request to the user's wallet. This request is a JWT that specifies the requested credentials (scope), the RP's own DID, and a challenge for security. The user's wallet verifies the request, presents the consent screen to the user, and upon approval, creates an Authorization Response. This response is a signed JWT containing the authorization code, which is sent back to the RP's callback endpoint. The entire exchange is designed to be phishing-resistant, as the RP is authenticated via its DID and the requests are cryptographically signed.

In the final step, the Relying Party exchanges the received authorization code for the actual credentials. It sends the code to the wallet's token endpoint and receives a response containing Verifiable Presentations (VPs) or Access Tokens in JWT format, signed by the user's DID. These tokens can then be used to access protected APIs or prove specific user attributes. This adaptation preserves the security benefits of the original OAuth flow—such as preventing exposure of tokens to the user's browser—while enabling a fully decentralized, user-held identity model that is interoperable with existing OAuth 2.0 and OpenID Connect infrastructure.

ecosystem-usage
AUTHORIZATION CODE FLOW

Ecosystem Usage

The Authorization Code Flow is a secure, server-side OAuth 2.0 protocol for obtaining access tokens. It is the standard method for web and mobile applications to authenticate users and access their data on behalf of a client application.

01

Core Protocol Steps

The flow involves a series of redirects between the user's browser, the client application, and the authorization server.

  1. Authorization Request: The client redirects the user to the authorization server with parameters like client_id, redirect_uri, and requested scope.
  2. User Authentication & Consent: The user authenticates and grants permission to the requested scopes.
  3. Authorization Grant: The server redirects back to the client with a one-time-use authorization code.
  4. Token Exchange: The client's backend server exchanges the code for an access token and optionally a refresh token via a secure, server-to-server POST request.
02

Primary Use Case: Web Server Apps

This flow is the gold standard for traditional web applications with a backend server. The critical security feature is that the sensitive access token is never exposed to the user's browser; it is received and stored securely on the backend server. This prevents token theft through browser-based attacks. Common implementations include:

  • User login on a company's internal dashboard.
  • Connecting a third-party SaaS tool to a user's Google Drive or GitHub account.
03

PKCE Extension for Mobile/SPAs

The Proof Key for Code Exchange (PKCE, pronounced "pixie") extension secures the flow for public clients that cannot store a secret, such as Single-Page Applications (SPAs) and native mobile apps.

  • The client creates a cryptographically random code_verifier and a derived code_challenge.
  • The challenge is sent with the initial request.
  • During the token exchange, the client must present the original verifier. The server validates it matches the challenge, proving the same client that initiated the flow is redeeming the code, thwarting authorization code interception attacks.
04

Security Advantages

Compared to simpler flows like the Implicit Flow, the Authorization Code Flow provides superior security:

  • Tokens Never in Browser: Access tokens are issued directly to the confidential client backend.
  • Client Authentication: The backend can authenticate itself to the authorization server using a client_secret.
  • Short-Lived Codes: The authorization code is single-use and has a very short lifespan (often minutes).
  • Refresh Token Support: Allows obtaining new access tokens without user re-authentication, while keeping refresh tokens server-side.

This makes it the recommended flow for most applications by standards bodies like the IETF and OpenID Foundation.

05

Key Components & Parameters

Understanding the essential parameters is crucial for implementation:

  • response_type=code: Indicates the Authorization Code Flow is being used.
  • client_id & client_secret: Identify and authenticate the client application.
  • redirect_uri: The URI to which the authorization server sends the user back with the code. Must be pre-registered.
  • scope: Defines the permissions being requested (e.g., read:user, openid).
  • state: A cryptographically random string used to maintain state between the request and callback, preventing CSRF attacks.
  • code_challenge & code_verifier: PKCE parameters for public clients.
security-considerations
AUTHORIZATION CODE FLOW

Security Considerations

The Authorization Code Flow (OAuth 2.0) is a robust, server-side mechanism for obtaining access tokens, but its security depends on proper implementation of several key protections.

02

State Parameter & CSRF Protection

The state parameter is a critical defense against Cross-Site Request Forgery (CSRF) attacks. It is an opaque, cryptographically random value that must be:

  • Generated by the client and included in the initial authorization request.
  • Returned unchanged by the authorization server in the redirect.
  • Validated by the client to ensure the response matches the original request. Failure to use and validate the state parameter can allow an attacker to trick a user into authorizing a malicious application.
03

Client Authentication & Secrets

Confidential clients (with a backend server) must authenticate to the token endpoint. Methods include:

  • Client Secret Basic: Sending client_id and client_secret in the HTTP Authorization header (most common).
  • Client Secret Post: Sending credentials in the POST body.
  • Private Key JWT: Using signed JWTs for stronger authentication. Public clients cannot securely store a secret and must use PKCE instead. Secrets must be stored securely (e.g., environment variables, secret managers) and never exposed in client-side code.
04

Redirect URI Validation

Strict redirect URI validation prevents authorization code leakage. The authorization server must:

  • Pre-register the exact, full redirect URIs for each client.
  • Compare the redirect_uri parameter in the authorization request against the pre-registered list with exact string matching.
  • Reject requests using non-registered URIs, open redirectors, or wildcards that could allow an attacker to intercept the authorization code.
05

Token Storage & Lifetime

Secure handling of tokens after issuance is essential:

  • Access Tokens: Should have short lifetimes (e.g., minutes/hours) to limit exposure if leaked. Use refresh tokens for long-lived sessions.
  • Refresh Tokens: Must be stored with maximum security (server-side, encrypted) as they grant long-term access. Implement refresh token rotation to detect and prevent replay attacks.
  • ID Tokens: Are for the client only and should not be sent to resource servers. Validate the signature, aud (audience), and exp (expiration) claims.
06

Common Implementation Pitfalls

Avoid these frequent security flaws:

  • Insufficient Entropy: Using weak random number generators for state or code_verifier.
  • Mixed Client Types: Treating a Single-Page App as a confidential client by embedding a secret.
  • Missing PKCE: Using the base flow for public clients.
  • Lax Redirect URI Matching: Allowing subdomain or path variations that aren't explicitly registered.
  • Ignoring Token Binding: Not using mechanisms like Demonstration of Proof-of-Possession (DPoP) or mTLS to bind tokens to a specific client, mitigating token replay.
PROTOCOL COMPARISON

Comparison with Other OAuth 2.0 Flows

Key security and use case differences between the Authorization Code Flow and other primary OAuth 2.0 grant types.

Feature / CharacteristicAuthorization CodeImplicitResource Owner Password CredentialsClient Credentials

Primary Use Case

Server-side web apps, mobile/native apps

Single-page applications (SPAs)

Legacy migration (highly discouraged)

Machine-to-machine (M2M), service accounts

Access Token Exposure

Back-channel only (server-to-server)

Front-channel (browser history, logs)

Back-channel only

Back-channel only

Issues Refresh Token

Client Authentication

User Authentication Required

PKCE Support (RFC 7636)

OAuth 2.1 Compliance

Security Recommendation

Recommended for confidential & public clients

Deprecated in OAuth 2.1

Not recommended

Recommended for M2M

AUTHORIZATION CODE FLOW

Common Misconceptions

The Authorization Code Flow is a cornerstone of OAuth 2.0, but its implementation in decentralized contexts is often misunderstood. This section clarifies key technical points and corrects frequent errors in understanding.

No, the Authorization Code is a short-lived, intermediate credential exchanged for the Access Token; they are distinct tokens with different purposes and security properties. The code is returned to the client application via a redirect URI after the resource owner grants consent, and it must be immediately exchanged server-side for the access token. This two-step process prevents the access token from being exposed to the user's browser or the redirect URI, which is a critical security measure. The authorization code itself grants no API permissions, whereas the access token is the bearer credential used to access protected resources.

AUTHENTICATION

FAQ

The Authorization Code Flow is a robust, server-side OAuth 2.0 protocol for securely authenticating users and obtaining access tokens. These FAQs address its core mechanics, security benefits, and implementation details for developers.

The Authorization Code Flow is an OAuth 2.0 grant type where a client application exchanges an authorization code for an access token and often a refresh token. It works through a multi-step, server-to-server handshake designed to keep tokens secret from the user's browser. The flow begins when the application redirects the user to an authorization server (e.g., Auth0, Okta, or a custom OIDC provider). After the user authenticates and consents, the server redirects back to the app with a short-lived, single-use authorization code. The application's backend then exchanges this code, along with its client secret, for the access token via a secure, direct POST request to the token endpoint. This server-side exchange is the critical security feature that prevents token leakage.

ENQUIRY

Get In Touch
today.

Our experts will offer a free quote and a 30min call to discuss your project.

NDA Protected
24h Response
Directly to Engineering Team
10+
Protocols Shipped
$20M+
TVL Overall
NDA Protected Directly to Engineering Team