LanguageEnglish
Build →

Learn · QuorumAuth

What is PKCE?

PKCE, Proof Key for Code Exchange, is an OAuth 2.0 extension that ties an authorization code to the client that requested it.

Updated

The authorization code flow with PKCE

  1. 1App → AppMake verifierSHA-256 of it = challenge
  2. 2App → Authorization serverSend the challenge
  3. 3Authorization server → Authorization serverStore challenge
  4. 4Authorization server → AppCode via redirectInterceptable here
  5. 5App → Authorization serverCode and verifier
  6. 6Authorization server → Authorization serverHash verifier, compare
  7. 7Authorization server → AppTokens only on a match

The app creates a random code verifier and derives the challenge as the SHA-256 hash of it. It sends the challenge in the authorization request. The server stores the challenge with the code it issues and returns the code through a redirect, where it could be intercepted. The app sends the code with the original verifier. The server hashes the verifier, compares it with the stored challenge, and returns tokens only on a match.

The verifier never leaves the app, so a stolen code is useless to anyone else.

The OAuth 2.0 authorization code flow sends a short-lived code back to the client through a browser redirect. On a phone or a desktop app, that redirect can be intercepted by another app registered for the same URL scheme, or captured from logs. Without more protection, whoever holds the code can exchange it for tokens. PKCE, defined in RFC 7636, closes that gap with a simple idea: the client proves at the token step that it is the same client that started the flow.

How PKCE works

PKCE adds two values to the authorization code flow.

  1. Create a verifier. The client generates a random code_verifier, 43 to 128 characters from A-Z a-z 0-9 - . _ ~.
  2. Derive the challenge. With the S256 method: code_challenge = BASE64URL(SHA256(code_verifier)).
  3. Authorize. The client sends the challenge, not the verifier, in the authorization request.
  4. Server stores the challenge alongside the authorization code it issues.
  5. Exchange. The client sends the code and the original code_verifier to the token endpoint.
  6. Verify. The server hashes the verifier and compares it with the stored challenge. Only a match yields tokens.

An attacker who intercepts the code in step 4’s redirect does not have the verifier, which never left the client.

What it looks like on the wire

The authorization request:

GET /authorize?response_type=code
  &client_id=my-app
  &redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
  &scope=openid%20profile
  &state=af0ifjsldkj
  &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
  &code_challenge_method=S256

The token request:

POST /token HTTP/1.1
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
&client_id=my-app
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

The challenge and verifier above are the worked example from RFC 7636 appendix B.

Generating a pair in Python:

import base64, hashlib, secrets

verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode()
challenge = base64.urlsafe_b64encode(
    hashlib.sha256(verifier.encode()).digest()
).rstrip(b"=").decode()

PKCE with and without

Threat Code flow without PKCE Code flow with PKCE (S256)
Code intercepted by a malicious app on the device Attacker redeems it Refused: no verifier
Code leaked through logs or referrer headers Attacker redeems it Refused: no verifier
Code injected into another user’s session Possible Detected: verifier belongs to the original session
Public client with no secret No client proof at all Per-request proof

When to use PKCE

Always, for the authorization code flow. RFC 7636 introduced it for public clients: native apps, single-page apps, CLIs. The OAuth 2.0 Security Best Current Practice, RFC 9700, now recommends it for confidential clients as well, and the OAuth 2.1 draft makes it part of the core flow. If your identity provider exposes OIDC discovery metadata, look for S256 in code_challenge_methods_supported.

PKCE protects the code. It does not decide what the resulting token may do; that is delegated authorization.

Pitfalls

  • Using plain. It exists for clients that cannot compute SHA-256, which today means almost none. Servers should refuse it.
  • Weak verifiers. A short or predictable verifier can be guessed. Use a cryptographic random generator.
  • Reusing a verifier. Generate a fresh one per authorization request.
  • Allowing code replay. Codes must be single-use, whatever PKCE says.
  • Loose redirect URI matching. PKCE does not excuse prefix or wildcard matching. Compare redirect URIs exactly.
  • Skipping state. PKCE helps against code injection, but keep state or the OIDC nonce for CSRF protection.

How QuorumAuth handles PKCE

QuorumAuth’s hosted sign-in runs the authorization code flow with PKCE. In its tested behaviour:

  • Only S256 is accepted; an authorization request with any other challenge method is rejected.
  • An authorization code can be exchanged once; replaying it is rejected.
  • A wrong code_verifier, or a redirect_uri that differs from the one used at authorization, is rejected at the exchange step.
  • A redirect off the product’s own pages is refused, and a code can be redeemed only through the product that obtained it.
  • Authorization must happen on an organization’s own sign-in page; a request with no organization page, or through another organization’s page, is refused.
  • When the organization requires a second factor, a hosted sign-in without it, or with a wrong authenticator code, is refused and issues no code.
  • Each organization’s OpenID Provider advertises the authorization code flow with S256 PKCE and nothing else.
  • Relying parties are declared with exact redirect URIs. A URI must be HTTPS on a named host, or HTTP on loopback; a native client may use its loopback address on any port.
Is PKCE only for mobile apps?

It was designed for public clients such as mobile and single-page apps, but current OAuth security guidance (RFC 9700) recommends it for every client using the authorization code flow.

What is the difference between S256 and plain?

With S256 the challenge is a SHA-256 hash of the verifier, so seeing the challenge does not reveal the verifier. With plain they are identical, which offers far less protection.

Does PKCE replace a client secret?

For public clients that cannot keep a secret, PKCE is the protection. Confidential clients should use both.

How long should the code_verifier be?

RFC 7636 requires 43 to 128 characters from the unreserved URI character set. Generate it from at least 32 random bytes.

Talk to sales about QuorumAuth.

QuorumAuth is sold through our sales team. The Build page shows the drop-in sign-in on your origin and how to reach sales about your stack.

Go to Build and contact sales →