Track 07 · Lesson 13 of 18Identity and login

Track 07 · Identity and login

“Log in with Google”: OAuth and PKCE

Lesson 13 of 18· 8 min

OAuth 2.0 lets an app get limited access to your account on another service without ever seeing your password. OpenID Connect (OIDC) adds an ID token that says who you are. Modern apps use the authorization code flow with PKCE.

Log in with Google, step by step

Then let a malicious extension steal the code

You and your browser
app.example.com
accounts.google.com

1. You click “Log in with Google”

The app prepares the request. For PKCE it creates a random secret, the code verifier, and keeps it to itself. It sends Google only a hash of it, the code challenge. It also creates a random state value to recognize the reply later.

code_verifier (stays in the app): 
code_challenge = SHA-256(verifier): 
state: 

Access versus identity#

OAuth is about access: an access token that lets the app call another service’s API, limited by the scope you approved. OIDC is about identity: an ID token, which is a JWT saying who you are. “Log in with Google” uses both. The app verifies the ID token (signature, issuer, audience, expiry) and then creates its own session, exactly like a normal login.

state and PKCE#

Two random values protect the flow. state is generated when the flow starts, stored, and compared when the provider sends you back: a mismatch means the callback wasn’t started by this user, so refuse it. PKCE (Proof Key for Code Exchange) creates a secret verifier, sends only its SHA-256 hash at the start, and must present the original when exchanging the code. A stolen code is useless without the verifier, which never left the app. Delete both values once they’re used.

The old “implicit flow” put tokens straight into the URL, where history, logs and extensions could read them. Public clients, like single-page and mobile apps, should always use the authorization code flow with PKCE.

Check yourself

An attacker steals an authorization code from the callback URL, but the app uses PKCE. What happens when they try to exchange it?