Difficulty: Advanced
What is the difference between OAuth 2.0 and OpenID Connect? Walk through the Authorization Code flow with PKCE.
OAuth 2.0 is an authorization framework: it lets a user grant an app limited access to their resources on another service, and the app receives an access token scoped to that permission. OpenID Connect (OIDC) is an identity layer built on top of OAuth 2.0 that adds authentication: alongside the access token, the app receives a signed ID token (a JWT) that says who the user is. Use OAuth when you need to call an API on the user's behalf, and OIDC when you need to log the user in.
code_verifier and sends its SHA-256 hash as code_challenge with code_challenge_method=S256./authorize endpoint with response_type=code, client_id, redirect_uri, scope, a random state and (for OIDC) a nonce.redirect_uri with a short lived code and the same state.state, then calls the /token endpoint server to server with the code and the original code_verifier.access_token, optionally a refresh_token, and with the openid scope an id_token.PKCE stops an attacker who intercepts the code (via a malicious app on the same phone, or logs) from redeeming it, because they do not have the verifier. It is now recommended for every client type, including confidential server apps.
| Access token | ID token | |
|---|---|---|
| Defined by | OAuth 2.0 | OpenID Connect |
| Audience | The resource server (API) | The client app |
| Purpose | Authorize API calls | Prove who logged in |
| Format | Opaque or JWT, up to the server | Always a signed JWT |
| Key claims | scope, exp | iss, sub, aud, exp, nonce |
The client must validate the ID token: signature against the provider's JWKS, iss, aud equal to its own client_id, exp, and nonce. It must never send the ID token to an API as a bearer token, and it should not treat a bare access token as proof of login, since an access token issued to some other app could be replayed.
openid scope plus standard scopes profile and email./userinfo endpoint for claims./.well-known/openid-configuration, which lists endpoints and the JWKS URL.sub (stable user id). Use sub plus iss as the account key, not email, since emails can change.localStorage.openid email profile.import crypto from 'node:crypto';
const b64url = (buf) => buf.toString('base64url');
const codeVerifier = b64url(crypto.randomBytes(32));
const codeChallenge = b64url(
crypto.createHash('sha256').update(codeVerifier).digest()
);
const state = b64url(crypto.randomBytes(16));
const nonce = b64url(crypto.randomBytes(16));
// Save verifier, state, nonce in the server session before redirecting
const url = new URL('https://accounts.google.com/o/oauth2/v2/auth');
url.search = new URLSearchParams({
response_type: 'code',
client_id: process.env.GOOGLE_CLIENT_ID,
redirect_uri: 'https://app.example.in/auth/callback',
scope: 'openid email profile',
code_challenge: codeChallenge,
code_challenge_method: 'S256',
state,
nonce,
}).toString();
console.log(url.toString());
The verifier stays in the server session. Only its SHA-256 hash travels through the browser, so an intercepted code cannot be redeemed by anyone else.
POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=4/0AdQt8qh...
&redirect_uri=https%3A%2F%2Fapp.example.in%2Fauth%2Fcallback
&client_id=1234.apps.googleusercontent.com
&client_secret=GOCSPX-...
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
This back-channel call is made from your server. The id_token is present only because the openid scope was requested.
OAuth 2.0, OpenID Connect, Authorization Code Flow, PKCE, ID Token, Scopes