OAuth 2.0, OpenID Connect and API keys
We'll cover the following
OAuth 2.0 and OpenID Connect
- OAuth 2.0 is delegated authorization: "let this app read my Google Calendar" without sharing your password.
- "Sign in with Google" is OpenID Connect (OIDC), a thin layer on top of OAuth that adds an ID token saying who you are.
- The benefit: users do not keep a password per app. They log in once at the identity provider (IdP).
Authorization code flow in 5 steps
- The app redirects you to the IdP (Google) with
client_id,scope,redirect_uriand astatevalue. - You log in and consent at Google, never at the app.
- Google redirects back to the app with a short-lived code.
- The app's backend swaps the code plus its client secret for tokens over a direct server-to-server call (the back channel).
- The app receives an access token (plus an ID token and a refresh token) and uses them.
Why a code and not the token directly? The browser URL can leak (history, logs, referrers), so the real token never travels through it. Mobile and single-page apps have no safe place for a client secret, so they add PKCE: a one-time random verifier whose hash is sent up front and proven at the swap, so a stolen code is useless.
IdP, OIDC, JWT, SSO: four different levels
| Term | What it is | Level |
|---|---|---|
| IdP | The server that knows who you are and logs you in (Google, Okta, Auth0) | A *who* |
| OIDC / OAuth 2.0 | The rules for how an app talks to the IdP: redirect, code, token exchange | A *protocol* |
| JWT | The signed format a token can have | A *format* |
| SSO | The experience: log in once at the IdP, and many apps accept it without asking again | A *goal* |
ID token vs access token
| ID token | Access token | |
|---|---|---|
| Answers | "Who is this user?" | "May the bearer call this API?" |
| Meant for | The app (client) | The API (resource server) |
| Format | Always a JWT (OIDC requires it) | OAuth does not say. Opaque at Google, often a JWT at Auth0, Okta and Azure. |
| App checks | Signature via the IdP's JWKS keys, iss, aud (is it for me?), exp | The app treats it as an opaque string and just forwards it |
How does an API validate an opaque token? It cannot alone. It calls the IdP's introspection endpoint ({active, sub, scope, exp}, often cached briefly) or looks it up in a shared store. A JWT is verified locally with the public key. So: JWT = fast, stateless validation but hard to revoke. Opaque = a lookup per request but instant revocation.
API keys
- Identify an application or project, not a user. Mostly server-to-server.
- A static secret with no expiry by default, so a leaked key is a compromise until it is rotated. Keep keys server-side, never in client code, and rotate them.
- Typical uses: rate limiting, billing, quotas.
- Stronger options for service-to-service: the OAuth client credentials flow, or mTLS.
- A term heard in passing: "offline ticket". It is probably the refresh token, which OAuth issues with the
offline_accessscope so an app can get new tokens while the user is away.
Summary: say it in the interview
- "Social login via OAuth/OIDC authorization code flow with PKCE. API keys only for app identification and quotas, not for user identity."
- Full one-liner: "Short-lived JWT plus revocable refresh tokens verified at the gateway, social login via OAuth/OIDC code flow with PKCE, API keys only for app identification and quotas."