OAuth 2.0, OpenID Connect and API keys
We'll cover the following
Visual summary

Download imageFree to share, please link back to this lesson.
The problem: use my data without my password
A calendar app wants to show your Google Calendar. The naive way is to ask for your Google password and log in as you. That is terrible: the app could read your mail, change your password, and you could never take back just the calendar access.
The second case is the same idea. A new site offers "Sign in with Google", so you never invent yet another password for it. In both cases you prove yourself to Google, and Google hands the app something limited to use instead of your password.
- OAuth 2.0 is delegated authorization: "let this app read my 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), here Google.
The authorization code flow, step by step
Three parties take part: the browser (you), the app backend (the calendar app's server) and the IdP. Read it top to bottom. Token basics (JWT, signing, expiry) are in the previous lesson, so they are not repeated here.
Browser App backend IdP (Google)
| click 'Connect' | |
|--------------------->| |
| 302 redirect to IdP with: |
| client_id=calapp scope=calendar.readonly |
| redirect_uri=https://calapp.com/cb |
| state=x7f3 (random, remembered by app) |
|---------------------------------------------->|
| you log in and click 'Allow' AT GOOGLE |
|<----------------------------------------------|
| 302 to https://calapp.com/cb?code=AbC&state=x7f3
|--------------------->| app checks state == x7f3
| | POST /token (back channel):
| | code=AbC + client secret
| |----------------------->|
| |<-----------------------|
| | access token (+ ID token, refresh token)
| | GET calendar API with access token
| your events | |
|<---------------------| |What each piece is for:
client_id: tells Google which app is asking.scope: exactly what access is requested, so the consent screen can say "read your calendar" and nothing more.redirect_uri: where Google may send you back. It must match one the app registered in advance, so a code cannot be sent to an attacker's site.state: a random value the app remembers and checks on return. If it does not match, the request was not started by this user's session (a CSRF defense).code: a short-lived, one-time voucher, not a token.
Why a code and not the token directly?
Everything in the browser redirect travels in a URL, and URLs leak (history, logs, referrers). So the real token never goes through it. Only the worthless-on-its-own code does, and the backend swaps it for tokens over a direct server-to-server call (the back channel), proving itself with its client secret.
Mobile and single-page apps have no safe place for a client secret, so they add PKCE. The app makes a random verifier and sends only its hash (the code_challenge) in the first redirect. At the swap it sends the verifier itself, and Google checks that it hashes to the earlier value. A thief who steals the code cannot redeem it without the verifier.
IdP, OIDC, JWT, SSO: four different levels
These words get mixed up because they are not alternatives. They sit at 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
The flow returns more than one token, and they have different readers.
| 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.
Refresh tokens and offline access
Access tokens are short-lived. If the app wants to keep syncing your calendar at 3 a.m. while you are away, it needs a refresh token: a long-lived credential it swaps at the IdP for a new access token, with no user present. The app asks for it by including the offline_access scope (Google uses an access_type=offline parameter for the same purpose) and you see that on the consent screen. Keep refresh tokens server-side, rotate them, and revoke them on logout, as in the previous lesson.
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.
How to choose for machine callers
- API key: you only need to know which app is calling, for quotas or billing. Simplest, weakest.
- OAuth client credentials flow: a service gets short-lived, scoped tokens by proving itself with its own id and secret. Use it when you want expiry and scopes, or already run an IdP.
- mTLS: both sides present certificates, so identity is proven at the connection and there is no bearer secret to leak. Use it for high-trust internal service-to-service traffic.
Check yourselfYour backend receives an access token from a third-party IdP and it is not a JWT. A user's account was just disabled. How can your API both validate the token and honour the disable quickly?
Treat it as opaque and call the IdP's introspection endpoint, which returns active: false once the token is revoked. Cache results only for a short time (seconds) so the disable takes effect quickly while load on the IdP stays manageable. Local JWT verification would not notice until exp.
Summary: say it in the interview
- "Social login via the OAuth/OIDC authorization code flow with PKCE, short-lived JWT access tokens plus revocable refresh tokens verified at the gateway, and API keys only for app identification and quotas, never user identity."