Catalog My learning Blog
Application Security for Developers: Secure Coding Course · Phase 3 · Lesson 21 · 3 min read

OAuth 2.0 and OpenID Connect Security Best Practices

On this page
  1. OAuth 2.0 vs OpenID Connect
  2. Authorization code flow with PKCE, step by step
  3. OAuth checks that prevent account takeover
  4. OpenID Connect sign-in pitfalls
  5. Practical OAuth and OIDC advice
  6. Check yourself

OAuth 2.0 vs OpenID Connect

OAuth 2.0 lets an application act on a user's behalf without their password: "allow this calendar app to read your contacts". OpenID Connect (OIDC) builds on it to answer "who is this user?", adding an ID token. Together they power "Sign in with Google/Apple/Microsoft" and most single sign-on. They're secure when configured correctly, and most real breaches come from configuration mistakes.

Authorization code flow with PKCE, step by step

The recommended flow for every client type (web apps, single-page apps, mobile, CLIs). PKCE (Proof Key for Code Exchange, RFC 7636) protects the authorization code from being stolen and used by someone else:

  1. The client creates a random code verifier and its SHA-256 hash, the code challenge.
  2. It redirects the user to the authorization server with the challenge, a state value, and its redirect_uri.
  3. The user signs in and consents. The server redirects back with a short-lived authorization code.
  4. The client exchanges the code plus the original verifier for tokens. The server hashes the verifier and checks it matches the challenge from step 2.

An attacker who intercepts the code (through a malicious app registered for the same redirect scheme, or a leaked log) can't redeem it without the verifier. Computing the challenge with the RFC's own test vector:

JavaScript
const verifier = 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk';
crypto.createHash('sha256').update(verifier).digest('base64url');
Code
PKCE challenge: E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM

That matches the value published in RFC 7636, Appendix B.

The client sends a code challenge, receives an authorization code at an exact registered redirect URI, and redeems it with the verifier; state and nonce bind the response to the request.
The client sends a code challenge, receives an authorization code at an exact registered redirect URI, and redeems it with the verifier; state and nonce bind the response to the request.

OAuth checks that prevent account takeover

RiskPrevention
Open redirect / code theft via a manipulated redirect_uriAuthorization server requires an exact match with pre-registered URIs (no wildcards, no prefix matching)
Login CSRF: attacker logs the victim into the attacker's accountRandom state per request, verified on return (PKCE also helps)
ID token replayRandom nonce in the request, checked in the ID token
Token for the wrong clientVerify the ID token's signature, iss, aud (your client ID) and exp (Day 20)
Leaked tokens in browsersAvoid the deprecated implicit flow; keep tokens out of URLs and localStorage
Over-broad accessRequest minimal scopes; resource servers check scope on every call
Stolen refresh tokensRotation (each use issues a new one and invalidates the old), sender-constrained tokens with DPoP or mTLS

Sender-constrained tokens are worth a sentence, because they change what "stolen" means. With DPoP (RFC 9449), the client holds a private key and signs a small proof for every request; the token is bound to that key. An attacker who copies the token from a log or a compromised proxy can't use it, because they can't produce the proofs. A normal bearer token works for whoever holds it, like cash; a sender-constrained one works like a credit card that also needs your PIN.

The OAuth 2.0 Security Best Current Practice (RFC 9700, 2025) and the consolidated OAuth 2.1 draft collect these rules; the implicit and password grants are deprecated.

OpenID Connect sign-in pitfalls

  • Identify users by iss + sub, never by email. Emails change, and some providers let users set unverified ones, which has enabled account takeovers when apps linked accounts by email.
  • If you must use email, require email_verified: true and be careful linking existing accounts.
  • The access token is for the API, the ID token is for the client. Don't send ID tokens to APIs as bearer tokens.

Practical OAuth and OIDC advice

  • Use a certified library (openid-client, MSAL, AppAuth, Spring Security, Auth.js) and a reputable identity provider rather than implementing flows yourself.
  • For browser apps, consider a backend-for-frontend that handles the OAuth flow and keeps tokens server-side, giving the browser only an HttpOnly session cookie.

Check yourself

An intercepted authorization code being redeemed by an attacker. The client sends a hash of a random verifier at the start and must present the verifier to redeem the code, which an interceptor doesn't have.

Loose matching, such as prefix or wildcard rules, lets attackers register or craft URLs they control that still pass the check, so codes or tokens are delivered to them, leading to account takeover.

Emails can change or be unverified, while iss plus sub is a stable, unique identifier for the account at that provider.

It binds the token to a key the client holds, so every request needs a fresh signed proof; a stolen copy of the token alone is useless.