OAuth 2.0 and OpenID Connect Security Best Practices
On this page
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:
- The client creates a random code verifier and its SHA-256 hash, the code challenge.
- It redirects the user to the authorization server with the challenge, a
statevalue, and itsredirect_uri. - The user signs in and consents. The server redirects back with a short-lived authorization code.
- 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:
const verifier = 'dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk';
crypto.createHash('sha256').update(verifier).digest('base64url');PKCE challenge: E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cMThat matches the value published in RFC 7636, Appendix B.
OAuth checks that prevent account takeover
| Risk | Prevention |
|---|---|
Open redirect / code theft via a manipulated redirect_uri | Authorization server requires an exact match with pre-registered URIs (no wildcards, no prefix matching) |
| Login CSRF: attacker logs the victim into the attacker's account | Random state per request, verified on return (PKCE also helps) |
| ID token replay | Random nonce in the request, checked in the ID token |
| Token for the wrong client | Verify the ID token's signature, iss, aud (your client ID) and exp (Day 20) |
| Leaked tokens in browsers | Avoid the deprecated implicit flow; keep tokens out of URLs and localStorage |
| Over-broad access | Request minimal scopes; resource servers check scope on every call |
| Stolen refresh tokens | Rotation (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: trueand 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.