OAuth 2.0 & OpenID Connect

Difficulty: Advanced

Question

What is the difference between OAuth 2.0 and OpenID Connect? Walk through the Authorization Code flow with PKCE.

Answer

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.

The roles involved

  • Resource owner: the user.
  • Client: your app (a React SPA, a mobile app, or a backend).
  • Authorization server: issues tokens, for example Google, Auth0, Keycloak or Okta. In OIDC it is called the OpenID Provider.
  • Resource server: the API that accepts access tokens, such as Google Drive API or your own backend.

Authorization Code flow with PKCE, step by step

  1. The client creates a random code_verifier and sends its SHA-256 hash as code_challenge with code_challenge_method=S256.
  2. The browser is redirected to the /authorize endpoint with response_type=code, client_id, redirect_uri, scope, a random state and (for OIDC) a nonce.
  3. The user logs in and consents. The server redirects back to redirect_uri with a short lived code and the same state.
  4. The client checks state, then calls the /token endpoint server to server with the code and the original code_verifier.
  5. The server hashes the verifier, matches it to the challenge, and returns an 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 vs ID token

Access tokenID token
Defined byOAuth 2.0OpenID Connect
AudienceThe resource server (API)The client app
PurposeAuthorize API callsProve who logged in
FormatOpaque or JWT, up to the serverAlways a signed JWT
Key claimsscope, expiss, 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.

What OIDC adds beyond tokens

  • The openid scope plus standard scopes profile and email.
  • A /userinfo endpoint for claims.
  • Discovery at /.well-known/openid-configuration, which lists endpoints and the JWKS URL.
  • Standard claims like sub (stable user id). Use sub plus iss as the account key, not email, since emails can change.

Current best practice and common mistakes

  • The Implicit flow and the Resource Owner Password Credentials grant are deprecated by the OAuth 2.0 Security Best Current Practice (RFC 9700) and dropped from the OAuth 2.1 draft. Use Authorization Code with PKCE.
  • Use Client Credentials for machine to machine calls where there is no user, for example a payout service calling a bank API.
  • For SPAs, many teams use a Backend for Frontend so tokens stay in an HttpOnly cookie on the server side instead of localStorage.
  • Always register exact redirect URIs; wildcard redirects are a classic token theft bug.
  • Request the minimum scopes. "Login with Google" only needs openid email profile.
Authorization Code flow with PKCE. The code is useless without the verifier, which never leaves the client until the back-channel token call.
  1. Client app → User browser: Redirect to /authorize with code_challenge
  2. User browser → Auth server: User logs in and consents
  3. Auth server → User browser: Redirect back with code and state
  4. User browser → Client app: Deliver code to redirect_uri
  5. Client app → Auth server: POST /token with code and code_verifier
  6. Auth server → Client app: access_token, id_token, refresh_token
  7. Client app → Resource API: Call API with Bearer access_token

Code examples

Generating PKCE values and the authorize URL

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.

Exchanging the code for tokens

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.

Key points

  • OAuth 2.0 answers 'what can this app do', OIDC answers 'who is this user'
  • Authorization Code with PKCE is the default flow for web, SPA and mobile apps
  • Implicit and password grants are deprecated (RFC 9700, OAuth 2.1 draft)
  • Validate ID tokens: signature, iss, aud, exp and nonce
  • Identify users by iss + sub, not by email

Common follow-up questions

  • Why is PKCE needed if the client already has a client secret?
  • Where should a single page app store its tokens?
  • How do refresh token rotation and reuse detection work?
  • When would you use the Client Credentials grant?
  • What does the state parameter protect against?

Interview tips

  • Say clearly that OAuth is not an authentication protocol; OIDC is what adds login
  • Draw the redirect flow and point out which calls go through the browser and which are back-channel
  • Mentioning that Implicit flow is deprecated and PKCE is now recommended everywhere signals current knowledge

Concepts covered

OAuth 2.0, OpenID Connect, Authorization Code Flow, PKCE, ID Token, Scopes

More Web Security (OWASP, JWT, XSS, CSRF) questions