OAuth Security Pitfalls

OAuth 2.0 gives an app an access token (a key card to certain data) without the app knowing the user's password. The flow to use: authorization code (a…

You click "Log in with Google" in a learning app you're trying for the first time. One click and you're in, and the app never sees your Google password. Nice for the user. But behind that click there are several steps, and if even one is loose, someone's account can fall into the wrong hands.

The way it works is called OAuth 2.0: an app gets permission to use your account on another service without ever knowing your password. Picture a hotel. You show your ID card only to the front desk. Then the desk gives a key card to a guest you allow in, for example a friend. The card opens only certain doors and only works for a few days, and your friend never sees your ID card. In OAuth, the ID card is your password, the front desk is the login service, and that guest is the app. The key card is called an access token.

The players and their names

The authorization code flow, step by step

This is the flow the Lab runs when a user named Rina logs in to MyClass, with the example values from the OAuth specs (RFC 6749 and RFC 7636). The state, code_verifier and code_challenge are explained in the next sections:

Step Who What happens
1 MyClass makes a random state and a random code_verifier, then computes the code_challenge
2 Browser is sent to login.example/authorize with the state and the code_challenge
3 login.example matches the redirect_uri, Rina logs in and agrees, a code is made and stored with its challenge
4 Browser comes back to myclass.example/callback?code=...&state=..., and MyClass matches the state
5 MyClass's server exchanges the code + code_verifier at /token
6 login.example checks the verifier, then hands out an access token

The requests in steps 2 and 5, exactly as in the Lab:

GET https://login.example/authorize?response_type=code&client_id=myclass-web
    &redirect_uri=https%3A%2F%2Fmyclass.example%2Fcallback&scope=profile
    &state=af0ifjsldkj&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
    &code_challenge_method=S256

POST https://login.example/token
grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https%3A%2F%2Fmyclass.example%2Fcallback&client_id=myclass-web
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

The proof used at /token depends on the kind of client (RFC 9700, January 2025). PKCE in this table is explained in the PKCE section below, and an SPA is an app that runs entirely in the browser:

Client Example Proof at /token
Confidential an app with a server, like MyClass client_secret, plus PKCE (recommended)
Public an SPA in the browser, a mobile app PKCE only, required, because a secret in the code can be read by anyone

"Log in with Google" really uses OpenID Connect (OIDC), a layer on top of OAuth made for login. Besides the access token, the client receives an ID token: a JWT that says who the user is. The client must check its signature, iss (the issuer), aud (who the token is for, must be its own client_id), exp, and nonce (a random text the client adds to the step 2 URL in the OIDC flow). Use a tested OIDC library for these checks.

state: the CSRF guard

state is a random text MyClass makes in step 1 and keeps in Rina's session. When the callback arrives, MyClass compares the state in the URL with the state in the session. If it doesn't match, or is missing, this flow wasn't started by Rina. For example, another site might have made Rina's browser open the callback with the attacker's code, so that Rina's MyClass account gets linked to the attacker's account. That attack is CSRF (Cross-Site Request Forgery), and the parameter that prevents it is state. MyClass must reject that callback and cancel the login.

PKCE: proof that you started it

PKCE (Proof Key for Code Exchange, said "pixy") adds one more proof. Before the flow starts, the client makes a code_verifier: 32 random bytes in base64url (43 characters). Only its fingerprint goes in the URL: code_challenge = base64url(SHA-256(verifier)). SHA-256 can't be reversed, so someone who sees the challenge still doesn't know the verifier. The code below makes both:

function base64url(bytes) {
  return btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
}

async function makePkce() {
  const verifier = base64url(crypto.getRandomValues(new Uint8Array(32))); // 43 random characters
  const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier));
  return { verifier, challenge: base64url(new Uint8Array(digest)) };
}

Now imagine an attacker manages to intercept the authorization code from the callback URL. Can they exchange it for an access token? Guess first.

The answer: no, they can't. At /token, the server computes the SHA-256 of the verifier it receives, then compares it with the stored challenge. An attacker holding only the code doesn't have the verifier, so the server answers 400 {"error":"invalid_grant"} and gives no token. PKCE is now recommended for every client, not just mobile apps.

redirect_uri must match exactly

If the authorization server doesn't validate the redirect_uri, an attacker can make a login link whose redirect_uri points to their own domain. Rina logs in as usual, and her code is sent to the attacker. This server has an open redirect: it sends the browser to any address it is given. The fix: the server matches the redirect_uri exactly against the address the client registered, character by character. If it doesn't match, the server shows an error and doesn't redirect (send the browser) anywhere. In the Lab, https://attacker.example/callback is rejected at step 3.

⚠️ OAuth checks done halfway

TL;DR: OAuth 2.0 gives an app an access token (a key card to certain data) without the app knowing the user's password. The flow to use: authorization code (a single-use code exchanged for a token) + PKCE. A random state is checked at the callback (the client address opened after login) to prevent CSRF, a login started by another site. PKCE sends code_challenge = base64url(SHA-256(code_verifier)) at the start, and the code_verifier (a secret random text) when exchanging the code. A code thief doesn't have the code_verifier, so a stolen code is rejected with invalid_grant. The redirect_uri (the callback address the code goes to) must exactly match the registered address. Otherwise the code can be sent to an attacker (an open redirect). Avoid the Implicit Flow (an old flow that puts the token in the URL), ask for as little scope (permission limit) as you need, and keep tokens where JavaScript can't reach them.

⚡ A key card, not a password

Meaning: OAuth 2.0 gives an app an access token without the app ever knowing your password. An access token is like a hotel key card: it opens only certain doors. The scope limits the permission; for example, the scope profile only allows reading the profile. The flow to use: authorization code + PKCE (an extra proof, explained in the PKCE step). An authorization code is a single-use code sent through the callback URL (the client address opened after login), then exchanged for an access token.

⚡ state guards against CSRF

Meaning: The client (the app asking for permission) makes a random state at the start and keeps it in the session. When the callback arrives, the state in the URL is compared with the state in the session. No match, or missing? Reject it, because the user didn't start this flow. The attack this stops is called CSRF: another site makes the user's browser open the callback with the attacker's code.

⚡ PKCE: proof that you started it

Write:

const verifier = base64url(crypto.getRandomValues(new Uint8Array(32))); // 43 random characters
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier));
Guess first: at the start of login, the client sends only the fingerprint of the verifier. To exchange the code, the client must send the real verifier. An attacker intercepts the authorization code from the callback URL. Can they exchange it for an access token?

No. The server compares the SHA-256 of the verifier with the fingerprint sent at the start. The attacker doesn't have the verifier, so they get 400 {"error":"invalid_grant"}.

Meaning: This step is called PKCE. The verifier is a secret random text, and its fingerprint (SHA-256) is called the code_challenge. Only the code_challenge goes in the URL. SHA-256 can't be reversed, so the code_challenge doesn't give away the verifier.

⚡ redirect_uri must match exactly

Meaning: The redirect_uri is the address the code goes to after login. If the server doesn't validate it, the code can be sent to an attacker's domain. That's an open redirect. Match it exactly, character by character, against the registered address.

What you will learn