liveBook · Manning

Proof Key for Code Exchange

As explained in Software Security for Developers

Proof Key for Code Exchange is an OAuth extension in which a client proves possession of a per-request secret when exchanging an authorization code.

Proof Key for Code Exchange (PKCE) strengthens OAuth authorization code exchange for public clients such as mobile apps, web apps, and IoT devices. It addresses a practical risk: an attacker intercepts an authorization code and uses it to obtain an access token. section 13.1 section 13.6

PKCE adds a short proof-of-possession sequence to the authorization flow:

  • The client creates an unpredictable random value called the code verifier. section 13.1
  • The client hashes the verifier, commonly with SHA-256, to create the code challenge. section 13.1
  • The client sends the challenge when redirecting the user for authentication. section 13.1
  • When exchanging the authorization code for tokens, the client presents the original verifier. section 13.5
  • The authorization server can reject an intercepted code because an attacker has the challenge or code, but not the verifier. section 13.1 section 13.5

A stolen authorization code

A mobile shopping app authenticates a user, who then receives an authorization code. Without PKCE, someone who intercepts that code might exchange it for access to the user's protected shopping resources. With PKCE, the interceptor also needs the client's unpredictable verifier, which was not exposed through the challenge. section 13.1 section 13.5

How Software Security for Developers covers it

How deep it goes
Explains interception risk, verifier and challenge generation, hashing, and token exchange, without detailing implementation code.
Examples
Uses an intercepted authorization code, mobile and web clients, and a shopping-list application to illustrate the risk and defense.
What it assumes
Assumes the authorization code flow and token exchange, while teaching PKCE terminology and mechanics from the beginning.
What you can do after
Apply it via an OAuth authorization flow by generating a verifier, deriving its challenge, and presenting the verifier during token exchange.
How it connects
Connects OAuth 2, OpenID Connect, authorization code flow, access tokens, public clients, and cryptographic hash functions.

Also explained in

Compare how 5 books explain Proof Key for Code Exchange →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Security for Developers. The section links point to the parts of the book it draws on.