Skip to the page

Authentication and authorization · senior

OAuth 2.0, OpenID Connect and PKCE

OAuth 2.0 lets an app get limited access to an API on a user's behalf, OpenID Connect adds a signed ID token that says who the user is, and every client, especially SPAs and mobile apps, should use the authorization code flow with PKCE.

The notes

What you must know

  • Roles: resource owner (user), client (your app), authorization server (Google, Auth0, Cognito, Keycloak), resource server (the API).
  • Authorization code + PKCE: the app makes a random code_verifier (43-128 chars) and sends its SHA-256 hash (code_challenge, method S256) to /authorize; the user logs in; the app gets a one-time code at the redirect URI; it exchanges code + code_verifier at /token for tokens. A stolen code is useless without the verifier.
  • RFC 9700 (OAuth 2.0 Security Best Current Practice, January 2025): public clients MUST use PKCE (confidential clients should too); do not use the implicit grant; the password grant MUST NOT be used; exact redirect URI matching; audience-restricted access tokens.
  • state protects the redirect against CSRF; nonce (OIDC) ties the ID token to this login.
  • OIDC ID token = who logged in (for the client). Access token = permission to call an API (for the resource server). Do not send ID tokens to APIs as access tokens.
  • Mobile: use the system browser (ASWebAuthenticationSession / Custom Tabs via AppAuth or expo-auth-session), never an embedded WebView; prefer universal/app links for the redirect.
  • 'Login with Google' on your backend: verify the ID token signature against Google's JWKS, check iss, aud (your client id) and exp, then create your own session.

The red pen

Common traps: mistake → fix

  • Mistake: OAuth is an authentication protocol

    Fix: OAuth is delegated authorization; OIDC adds authentication.

  • Mistake: PKCE is only for mobile apps

    Fix: RFC 9700 recommends it for all clients and requires it for public ones.

  • Mistake: The implicit flow is fine for SPAs

    Fix: it is deprecated; use code + PKCE (often through a BFF).

Easy to hard

Questions interviewers ask, easy to hard

  1. Walk me through 'Login with Google' from click to session.
  2. OAuth vs OIDC? Access token vs ID token?
  3. What is PKCE and what attack does it stop?
  4. Why not the implicit flow anymore?
  5. How do you do OAuth safely in a React Native app?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview