Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 2 OF 5 · 2 MIN READ

OAuth 2.0, OpenID Connect and SSO

OAuth 2.0 lets an app act on a user's behalf without their password; OpenID Connect adds login on top of it. "Sign in with Google" is the example everyone knows, and interviewers ask you to walk through the Authorization Code flow with PKCE.

What you will learn

  • Explain the roles in OAuth 2.0
  • Walk through the Authorization Code flow with PKCE
  • Know what OIDC adds (ID token, userinfo)
  • Know which flows are deprecated

Key concepts

Roles

Resource owner (the user), client (your app), authorization server (Google, Auth0, Keycloak) and resource server (the API holding the data).

Authorization Code + PKCE

The app redirects to the authorization server, the user logs in and consents, the server redirects back with a short-lived code, and the app exchanges the code for tokens. PKCE (code_verifier and code_challenge) proves that the app that started the flow is the one finishing it.

OIDC ID token

A JWT about the user (sub, email, name) for the client to read. The access token is for calling APIs; the ID token is not.

Scopes

What the app asks permission for: openid profile email, or calendar.read. Ask for the minimum.

Step by step

The flow

  • App creates code_verifier (random) and code_challenge = SHA256(verifier), plus a random state
  • Redirect to /authorize?response_type=code&client_id&redirect_uri&scope&state&code_challenge
  • User logs in; server redirects to redirect_uri?code=...&state=...
  • App checks state, then POSTs code + code_verifier to /token
  • App receives access token, refresh token and ID token; verifies the ID token and creates its own session

Deprecated

The Implicit flow (tokens in the URL fragment) and the Resource Owner Password flow (app collects the password) are removed in OAuth 2.1. Use Authorization Code with PKCE everywhere, and Client Credentials for machine-to-machine.

Examples

Generating PKCE values in Node.js

import { randomBytes, createHash } from 'node:crypto'
const verifier = randomBytes(32).toString('base64url')
const challenge = createHash('sha256').update(verifier).digest('base64url')
const state = randomBytes(16).toString('base64url')
// store verifier and state in the session, send challenge + state in the redirect

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Draw the Authorization Code + PKCE flow from memory.
  • Explain what the state parameter protects against.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

What is the difference between OAuth and OIDC?

OAuth 2.0 is about authorization: giving an app access to an API. OpenID Connect is an identity layer on top that standardises login with an ID token and a userinfo endpoint.

Which flow do services use to talk to each other?

Client Credentials: the service authenticates with its own client id and secret (or a signed assertion or mTLS) and gets an access token with no user involved.

Summary

Use Authorization Code with PKCE for user login, Client Credentials for services, validate state and the ID token, request minimal scopes, and avoid the implicit and password flows.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus