Course contentsShow
← System Design
Lesson 922 of 1,91924. Authentication and Authorization at ScaleFree lesson

Authorization Code Flow

The most secure OAuth2 flow for server-side applications with confidential clients.

Authorization Code Flow

What you'll learn: How the most secure OAuth2 flow protects user credentials by exchanging authorization codes for tokens behind the scenes.

What Is Authorization Code Flow?

Authorization Code Flow is OAuth2's most secure flow, designed for server-side applications (confidential clients) that can safely store secrets. Instead of giving tokens directly to the browser, it uses a two-step exchange that keeps sensitive credentials server-side.

How It Works

Think of this like picking up concert tickets:

  1. User Authorization: Your app redirects the user to the authorization server (e.g., Google login page). The user approves access.

  2. Code Exchange: The authorization server sends back a temporary authorization code to your app's redirect URL. This code is useless by itself—it's like a claim ticket, not the actual tickets.

  3. Token Request: Your backend server exchanges this code for actual access tokens by making a server-to-server request, including your app's secret credentials (client ID + client secret). The authorization server validates these credentials before issuing tokens.

  4. Secure Token Storage: Tokens stay server-side, never exposed to the browser or user device.

Why It's Most Secure

The code is only valid once and expires quickly (typically 60 seconds). Even if intercepted, an attacker can't redeem it without your client secret—which only your backend knows. Tokens never pass through the browser where they could be stolen by malicious JavaScript.

This contrasts with flows that deliver tokens directly, exposing them to potential theft.

Key Takeaway: Authorization Code Flow keeps tokens server-side by exchanging one-time authorization codes for access tokens, making it the gold standard for web applications where you control the backend infrastructure.