OAuth 2.0 Grant Types: Authorization Code Grant
You'll learn: How the authorization code grant provides the most secure OAuth 2.0 flow for web applications with backend servers.
What Is the Authorization Code Grant?
The authorization code grant is OAuth 2.0's most secure flow, designed specifically for confidential clients—web applications that can safely store secrets on a backend server. Unlike simpler flows, it uses a two-step exchange that keeps tokens away from the user's browser, minimizing exposure to attacks.
Think of it like a valet parking system: instead of handing your car keys (access token) directly to the valet in public view, the attendant gives you a claim ticket (authorization code). You then exchange that ticket privately at the booth for your keys.
How It Works
- User Authorization: The client redirects the user to the authorization server with a request containing client ID, requested scopes, and a redirect URI
- Code Issuance: After the user grants permission, the authorization server redirects back to the client with a short-lived authorization code (not a token!)
- Token Exchange: The client's backend server sends this code to the authorization server along with the client secret, receiving an access token in return
The critical security feature: the authorization code itself is nearly useless to attackers. It's single-use, expires quickly (typically 1-10 minutes), and can only be exchanged when combined with the client secret, which never leaves the backend server.
Why This Grant Type?
This flow prevents access tokens from ever passing through the user's browser or browser history, where malicious JavaScript or browser extensions could intercept them. The client secret adds an authentication layer that public clients (mobile apps, SPAs) cannot provide.
Key Takeaway: The authorization code grant is the gold standard for web applications with backends—it separates public user authorization from private token exchange, keeping your access tokens secure on the server side.