0Pricing
Cryptology Academy · Lesson

PKCE: Securing Public Clients

Understand Proof Key for Code Exchange and how it prevents authorization code interception attacks.

PKCE: Securing Public Clients is a free Cryptology Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Authorization Code Interception

Without PKCE, mobile applications are vulnerable to authorization code interception attacks. When the authorization server redirects the authorization code to the app's registered custom URI scheme (e.g., myapp://callback), any malicious app on the same device that registers the same URI scheme can intercept the redirect and steal the code.

How Interception Works

The attack: a malicious app registers the same custom URI scheme as the legitimate app. When the authorization server redirects the code to myapp://callback, the OS may present both apps as handlers. If the user selects or the OS defaults to the malicious app, the attacker receives the authorization code and can exchange it for tokens without knowing the client secret.

PKCE Code Verifier

PKCE (RFC 7636) adds a dynamically generated secret to the authorization code flow. Before initiating the flow, the client generates a cryptographically random string of 43-128 characters called the code verifier. This string is unique to each authorization request and is never transmitted until the token exchange step.

Code Challenge Computation

The client computes a code challenge from the verifier: code_challenge = BASE64URL(SHA256(code_verifier)). Using SHA256 is the required method in RFC 7636 (the "plain" method, which sends the verifier directly, is not recommended). The code challenge is a one-way transform of the verifier, meaning knowing the challenge does not reveal the verifier.

Including Code Challenge in Authorization

The authorization request includes two additional parameters: "code_challenge=BASE64URL(SHA256(verifier))&code_challenge_method=S256". The authorization server stores the code challenge associated with the issued authorization code. No secret is transmitted to the server that could be intercepted at this stage.

Token Exchange with Code Verifier

During the token exchange (POST to the token endpoint), the client includes "code_verifier=ORIGINAL_RANDOM_STRING" alongside the authorization code. The authorization server computes BASE64URL(SHA256(code_verifier)) and verifies it matches the stored code_challenge. Only the legitimate client that generated the verifier can pass this check.

Why Interception Fails with PKCE

If an attacker intercepts the authorization code, they receive only the code and the code challenge (which is public). To exchange the code for tokens, they must provide the code verifier. Since the verifier was generated by the legitimate client and never transmitted until the token exchange (which happens securely), the attacker cannot compute or retrieve the verifier.

PKCE Prevents Code Injection

PKCE also prevents authorization code injection attacks, where an attacker replaces a valid code with a stolen code in the redirect. The stolen code's challenge does not match the verifier the victim's client will present, causing the token exchange to fail. PKCE provides defense-in-depth against multiple attack vectors simultaneously.

PKCE for All Clients

Although RFC 7636 was initially described as a solution for public clients (those without client secrets), the OAuth Security BCP and OAuth 2.1 require PKCE for all clients, including confidential clients with client secrets. PKCE provides protection independent of client authentication, making it universally beneficial.

PKCE in OAuth 2.1

OAuth 2.1 (draft-ietf-oauth-v2-1) consolidates the security best practices from the OAuth 2.0 Security BCP into a single document. It mandates PKCE for all authorization code flows, deprecates the implicit flow, and requires refresh token rotation. PKCE is effectively the required baseline for any new OAuth 2.0 implementation.

Implementation Notes

Implementing PKCE correctly requires: using a cryptographically secure random generator for the verifier (at least 32 random bytes, then base64url-encoded), storing the verifier securely in the client (not in the URL or logs), using S256 method (not plain), and ensuring the verifier is discarded after the token exchange. Most modern OAuth libraries handle PKCE automatically.

PKCE Code Verifier Check

In PKCE, what is the relationship between the code verifier and the code challenge?

Lesson Recap: PKCE Security

PKCE (RFC 7636) prevents authorization code interception by binding each code to a dynamically generated code verifier known only to the legitimate client. The verifier is hashed to produce the code challenge (sent publicly). The token exchange requires the original verifier. PKCE prevents both interception and injection attacks. OAuth 2.1 mandates PKCE for all authorization code flows.

Frequently asked questions

Is the “PKCE: Securing Public Clients” lesson free?

Yes — the full text of “PKCE: Securing Public Clients” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO.

What will I learn in “PKCE: Securing Public Clients”?

Understand Proof Key for Code Exchange and how it prevents authorization code interception attacks. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cryptology Academy?

No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “PKCE: Securing Public Clients” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cryptology Academy lesson?

Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. OAuth 2.0 Flows and Token Types
  2. PKCE: Securing Public Clients
  3. OpenID Connect Claims and ID Tokens
  4. OAuth Vulnerabilities and Attack Patterns
← Back to Cryptology Academy