Skip to content

Authorization Code Flow

The Authorization Code Flow is the industry-standard implementation for secure server-side applications. It creates a robust, multi-stage “Handshake” that ensures sensitive tokens remain exclusively on your secure backend server, never touching the user’s browser in their final, usable form. By decoupling the initial user consent from the final token issuance, this flow effectively eliminates the risk of token exposure through browser history, referrer headers, or local storage.

AUTH-CODE

Handshake Process
Core Mission
Secret-Based Delegation. Protecting the connection between the application and the identity provider via a private, server-to-server exchange.
Like a Bank Safe Receipt: At the teller window (The Browser), you present your ID and receive a small, serialized receipt (The Auth Code). This code by itself has no value—it's not money. You then take that receipt to a secure back office (The Backend), where you swap it for the actual cash (The Access Token). Because the cash never crosses the lobby, an observer in the lobby can never steal your funds.
Server-Side Web Apps / Confidential Clients / Legacy Migration

The security of the process depends on the strict separation of the Front-Channel (User’s Browser) and the Back-Channel (Server-to-Server).

sequenceDiagram
    participant User
    participant Browser
    participant Server as Client Backend
    participant IdP as Auth Server

    User->>Browser: Click "Login"
    Browser->>IdP: GET /authorize (state, redirect_uri)
    IdP->>User: Authenticate & Consent
    User-->>IdP: Approved
    IdP-->>Browser: Redirect with code & state
    Browser->>Server: Deliver code to Redirect URI
    Server->>IdP: POST /token (code, client_secret)
    IdP-->>Server: Issue Access Token
1

Initiate (Front-Channel)

The client redirects the user to the Authorization Server. Crucially, it includes a `state` parameter—a unique, unguessable string—that the server will return to prevent Cross-Site Request Forgery (CSRF).

2

Redirect (Front-Channel)

After consent, the Auth Server redirects the user back to the application with a short-lived "Authorization Code." At this stage, the Client Backend must verify that the `state` returned matches the one sent.

3

Exchange (Back-Channel)

The Backend performs a server-to-server POST request to the Token endpoint. It presents the Auth Code and its private `client_secret`. The Auth Server validates both and returns the final Access Token directly to the backend.


Every parameter in an Authorization Code request is a critical security control. Improper configuration results in high-risk vulnerabilities.

ParameterPurposeStrategic Requirement
stateCSRF Protection.Unique per-session, unguessable nonce.
redirect_uriDelivery Target.Must be pre-registered; exact match.
client_secretBackend Auth.NEVER expose to the frontend/browser.
codeSwap Key.Single-use only; 5-10 minute lifespan.

Implementing the exchange requires a robust, error-handling HTTP client on your backend.

// Server-Side Token Exchange Implementation
async function exchangeAuthCode(code, state) {
// 1. Verify CSRF state from session
if (state !== session.oauth_state) throw new Error("State mismatch");
// 2. Perform back-channel POST
const response = await fetch("https://auth.example.com/token", {
method: "POST",
body: new URLSearchParams({
grant_type: "authorization_code",
code: code,
redirect_uri: process.env.REDIRECT_URI,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET // Secure env var
})
});
return await response.json();
}

Master the technical details of the Authorization Code flow and its extensions.