Highlights

Contents

Part IV
04

OAuth 2.0 and PKCE

We give one application limited access without sharing the user’s password. A token response alone does not prove the intended login identity. We will trace OAuth roles, the authorization-code flow, PKCE, and consent and refresh boundaries.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Authentication vsauthorization2Sessions and cookies3JWT and API keys4OAuth 2.0 and PKCE5RBAC, ACLs, and resourcepolicy6Encryption and secrets7Password hashing andsigned URLs8CORS, CSRF and abuseprevention9Case study: securing oneoperation

#Section 13Delegation is a different problem from login

Mira wants a clip-analysis application to read selected Heron media without learning her Heron password. OAuth 2.0 is an authorization framework for delegated access. A client obtains a credential granting a defined scope to a resource service under an authorization server’s policy.

The resource owner controls the relevant access, the client requests delegated access, the authorization server issues the grant and tokens, and the resource server enforces access to the API. These roles can share deployment infrastructure but have distinct responsibilities. A client identity is not the same as the user identity it represents.

OAUTH: THE OWNER APPROVES, THE AUTH SERVER ISSUES, THE CLIENT USES, THE API ENFORCESOAUTH: THE OWNER APPROVES, THE AUTH SERVER ISSUES, THE CLIENT USES, THE API ENFORCESresource ownerclient appauthorization serverresource APIwants accessapproves scopetokencallsthe client never sees the owner's password
Figure 18.13Illustrative OAuth role relationships. The authorization server’s policy controls grants, while the API enforces the resulting permitted use.

Scope describes the permitted use of a grant, with semantics defined by the service. A token with read scope still needs resource ownership and tenant checks. Scope is not a universal string whose name automatically enforces a policy. The API must map it to actual allowed actions.

#Section 14The authorization code is exchanged at another boundary

The client sends the user to the authorization server with a registered redirect URI and flow context. The user authenticates there and approves the requested access under policy. The server returns an authorization code, a short-lived one-use value, through the redirect. The client exchanges that code at the token endpoint under the flow’s client and proof rules.

The browser redirect carries a code rather than treating the redirect URL as a permanent API credential. The authorization server checks that the redemption matches the intended client, redirect URI, and grant context. A confidential client authenticates as required; a public client cannot keep a secret merely because its source code contains one.

THE REDIRECT CARRIES A ONE-TIME CODE; ONLY THE CODE EXCHANGE YIELDS THE ACCESS TOKENTHE REDIRECT CARRIES A ONE-TIME CODE; ONLY THE CODE EXCHANGE YIELDS THE ACCESS TOKENclientauthorization serverresource APIrequest scoperedirect + codecode + proofaccess tokenpermitted call
Figure 18.14Illustrative authorization-code flow. Exact client authentication and proof rules follow the deployment and current OAuth security guidance.

The code exchange should occur through the intended secure endpoint and registered flow context. A client must not send the code to an arbitrary token URL supplied by untrusted input. The browser-facing redirect and server-facing exchange therefore have different validation responsibilities. Logging their full URLs can also expose sensitive one-use values during the flow.

OAuth’s security best current practice, RFC 9700, documents secure flow choices and redirect, token, and client concerns. Follow current provider documentation for implementation. Do not invent a homegrown redirect matching rule or reuse the user’s password as a client credential.

#Section 15PKCE binds redemption to the initiating client

An attacker intercepts an authorization code. If that code can be redeemed alone, the attacker can obtain the resulting credential. Proof Key for Code Exchange, or PKCE, binds the code exchange to a secret value generated for this authorization attempt. The client creates a verifier and sends a derived challenge in the authorization request.

At redemption, the client supplies the verifier. The authorization server recomputes the challenge under the required method and compares it with the value associated with the code. Someone holding only the intercepted code lacks the verifier. The challenge is not a reusable client secret and must be created and bound correctly for each attempt.

PKCE: THE CLIENT KEEPS A SECRET VERIFIER V; ONLY V REDEEMS THE CODEPKCE: THE CLIENT KEEPS A SECRET VERIFIER V; ONLY V REDEEMS THE CODEclient createsverifier V (kept)authorizationsends H(V); bound to coderedemptioncode + V → recompute H(V)stolen codeno V → failsH is the method the spec requires, not any hash you like
Figure 18.15Illustrative PKCE mechanism. H denotes the required challenge derivation, not an arbitrary hash choice.

The client also binds the browser response to its initiating context and validates expected authority and redirect behavior. A state parameter can carry a protected unpredictable correlation value for the flow under a reviewed protocol. OpenID Connect adds identity-specific rules, including nonce where applicable. Do not treat any one parameter as a complete defense for every flow confusion.

#Section 16Grants, refresh, and revocation need policy

Mira removes the clip-analysis application’s access. The authorization server stops renewal and revokes the relevant grant according to policy. Existing access tokens may remain accepted until expiry unless the resource server checks current grant validity. That is the same local-validation tradeoff discussed earlier, now applied to delegated access.

A consent record captures approved access under the service’s user and client policy. A refresh credential should remain scoped to that grant and protected against copying. Rotation or sender-constraining mechanisms can limit replay under the chosen design. A long-lived credential in a public repository or browser log defeats the separation delegation was meant to provide.

REVOKING A CLIENT ENDS THE GRANT AND ITS REFRESH PATH; LIVE ACCESS TOKENS RUN OUTREVOKING A CLIENT ENDS THE GRANT AND ITS REFRESH PATH; LIVE ACCESS TOKENS RUN OUTuser revokesclient Xinvalidate grantconsent recordreject refreshno new tokensbound accessexpiry or check
Figure 18.16Illustrative delegated-access revocation. Existing access validity follows the resource server’s declared expiry or online-check rule.

Least-scope requests improve user comprehension and limit damage. Do not request all media writes when the app only reads a chosen clip. Client registration, redirect restrictions, token storage, and resource enforcement are part of the architecture. In an interview, draw the browser, client server, authorization server, and API separately so their credentials do not become interchangeable.