OAuth & Authentication

OAuth PKCE Explained: Why It Matters for Secure Connections

How Proof Key for Code Exchange protects the authorization code flow, and why it’s now recommended for web apps as well as mobile.

4 min read
On this page
  1. The problem: intercepted authorization codes
  2. How PKCE works
  3. S256 vs plain
  4. Generating a good verifier
  5. Does a server-side SaaS app need PKCE?
  6. PKCE and the state parameter
  7. Provider support
  8. Common mistakes
  9. Where Pathfix fits
  10. Frequently asked questions

PKCE, short for Proof Key for Code Exchange and pronounced "pixie", is an extension to the OAuth 2.0 Authorization Code flow. It was created to protect mobile and single-page apps, and current security guidance now recommends it for practically every client that uses the authorization code flow, including server-side web applications.

This article explains the problem PKCE solves, how it works, and what it means for SaaS integrations.

The problem: intercepted authorization codes#

In the Authorization Code flow, the provider sends an authorization code back to your redirect URI. Your application then exchanges that code for tokens.

The weak point is the moment between those two steps. If an attacker obtains the code, for example through a malicious app registered for the same custom URL scheme on a phone, a leaked browser history entry, or a misconfigured redirect, they may be able to exchange it for tokens themselves.

Confidential clients, which authenticate with a client secret during the exchange, have some protection: the attacker would also need the secret. Public clients such as mobile apps and browser-based apps can't keep a secret, so a stolen code is enough.

How PKCE works#

PKCE binds the authorization code to the specific client instance that requested it, using a one-time secret that never travels through the browser redirect.

  1. Create a verifier. Before redirecting the user, your application generates a high-entropy random string called the code_verifier and keeps it private.
  2. Derive a challenge. It computes the code_challenge, the base64url-encoded SHA-256 hash of the verifier.
  3. Send the challenge. The authorization request includes code_challenge and code_challenge_method=S256. The authorization server stores the challenge alongside the code it issues.
  4. Send the verifier. When exchanging the code for tokens, your application includes the original code_verifier.
  5. Server checks. The authorization server hashes the verifier and compares it with the stored challenge. If they don't match, the exchange fails.

An attacker who intercepts the code doesn't have the verifier, and can't derive it from the challenge, so the stolen code is useless.

AAAWgmp1bWIAAAAeanVtZGMycGEAEQAQgAAAqgA4m3EDYzJwYQAAABZcanVtYgAAAEdqdW1kYzJtYQARABCAAACqADibcQN1cm46YzJwYTpmOTVjMGM2OC04ZTQ2LTQ1M2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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPZYQIStzc/lJqrWVdC+9xePS2rM+hOLU9GIYsInZxCJG4GEcNEHWtWeg8TfHTLfOsmmt4AJooasErQ8v6Yp9BtasFU=OAuth 2.0 authorization code flow with PKCESequence between the user, the SaaS application, the authorization server and the resource API. 1, the user clicks Connect account. 2, the SaaS application redirects the user to the authorization server with scopes, state and a PKCE challenge. 3, the user approves access. 4, the authorization server redirects back to the SaaS application with an authorization code. 5, the SaaS backend exchanges the code and PKCE verifier for tokens. 6, the SaaS application calls the resource API with the access token. 7, the resource API returns the requested data.UserSaaS applicationAuthorization serverResource API1Clicks “Connect account”2Redirects to authorizescopes · state · PKCE challenge3Approves access4Redirects backauthorization code5Exchanges code for tokenscode + PKCE verifier6Calls API with access token7Returns requested data
The PKCE challenge travels with the authorization request in step 2; the verifier is only sent directly to the token endpoint in step 5.

S256 vs plain#

PKCE defines two challenge methods. plain sends the verifier itself as the challenge, which offers little protection if the authorization request is observed. S256 sends a hash. Use S256 wherever the provider supports it.

Generating a good verifier#

The verifier must be 43 to 128 characters from the unreserved URL character set, and should come from a cryptographically secure random generator. A common approach is 32 random bytes, base64url-encoded without padding.

Store it server-side, tied to the user's session and the specific authorization request, and discard it after the exchange.

Does a server-side SaaS app need PKCE?#

The original specification targeted public clients. Since then, the OAuth 2.0 Security Best Current Practice (RFC 9700) recommends PKCE for confidential clients too, because it also protects against authorization code injection attacks that a client secret alone doesn't prevent. The draft OAuth 2.1 specification makes PKCE part of the standard Authorization Code flow.

In practice:

  • It costs a few lines of code.
  • It defends against attacks a client secret doesn't cover.
  • Some providers already require it.

There's little reason not to use it.

PKCE and the state parameter#

PKCE doesn't replace state. The state parameter ties the callback to the user's session to prevent cross-site request forgery, and lets you carry context through the redirect. PKCE ties the code to the client instance. Use both, unless the provider's guidance says otherwise.

Provider support#

Most major OAuth providers support PKCE with S256, but not all, and some handle it slightly differently. A few require it for certain app types. Check each provider's documentation, and test that the exchange actually fails when you send a wrong verifier. That confirms the provider is enforcing it.

Common mistakes#

  • Reusing a verifier across requests. Generate a new one each time.
  • Storing the verifier in a URL or anywhere the redirect could expose it.
  • Using plain when S256 is available.
  • Assuming PKCE replaces client authentication for confidential clients. Keep using the client secret where the provider requires it.
  • Dropping state because PKCE is in place.

Where Pathfix fits#

When you build OAuth yourself, PKCE is one more piece to implement and test per provider. Pathfix OAuth runs the authorization flow for you across 350+ providers, along with token storage, refresh and authenticated API passthrough, using your own OAuth apps and credentials. Check the documentation for current details of how individual providers are configured.

For a broader walkthrough of the flow, read What Is OAuth 2.0?.

Frequently asked questions#

Is PKCE only for mobile apps?#

No. It started there, but it's now recommended for web applications too.

Does PKCE make the client secret unnecessary?#

For public clients, PKCE replaces what the secret couldn't provide. Confidential clients should still authenticate as the provider requires.

What happens if the verifier doesn't match?#

The token endpoint rejects the exchange, usually with an invalid_grant error.