
OAuth 2.0 PKCE: code_verifier & code_challenge Explained with Examples
When building applications that need to authenticate users via OAuth 2.0, especially using the Authorization Code flow, you might encounter the term code_verifier. If you’re like me, you might have wondered, “What is this code_verifier and why is it important?” This post will demystify code_verifier, explain its role in Proof Key for Code Exchange (PKCE), and provide practical examples to help you implement it correctly. Visual Overview: sequenceDiagram participant User participant App as Client App participant AuthServer as Authorization Server participant Resource as Resource Server User->>App: 1. Click Login App->>AuthServer: 2. Authorization Request AuthServer->>User: 3. Login Page User->>AuthServer: 4. Authenticate AuthServer->>App: 5. Authorization Code App->>AuthServer: 6. Exchange Code for Token AuthServer->>App: 7. Access Token + Refresh Token App->>Resource: 8. API Request with Token Resource->>App: 9. Protected Resource The Problem: Authorization Code Flow Vulnerability The Authorization Code flow in OAuth 2.0 is widely used because it balances security and usability. However, it has a known vulnerability: if an attacker intercepts the authorization code, they can exchange it for an access token. This is particularly problematic in public clients, like single-page applications (SPAs) and mobile apps, where you can’t store a client secret securely. ...