Not your master key
A print shop offers to print the photos you keep in a hotel. The old way: hand it your master key. Now it can open every room, it can keep a copy, and the only way to stop it is to change all the locks.
The better way: the print shop sends you to the hotel's front desk. You tell the desk "photos only", and the desk gives the print shop a key card that opens just the photo room and stops working in an hour.
Delegated authorization
OAuth 2.0 lets a user grant a third-party application limited access to their resources without sharing credentials. Four roles:
| Role | Example |
|---|---|
| Resource owner | You |
| Client | The photo-printing app |
| Authorization server | Google's or GitHub's sign-in and consent service |
| Resource server | The Photos API |
The client gets an access token with a scope (such as photos.read) and an expiry. You sign in only
at the authorization server, and you can revoke the grant there without touching your password.
OAuth is about authorization (what an app may do), not authentication (who you are). The identity layer on top is OpenID Connect, below.
The claim ticket and the secret word
The desk doesn't hand the key card to you to carry, because you might lose it on the way. It gives you a claim ticket, which opens nothing. The print shop takes the ticket back to the desk itself and swaps it for the card. And because it made up a secret word at the very start, someone who grabbed the ticket on the way couldn't swap it.
Authorization code flow with PKCE
The recommended flow for web, mobile and single-page apps:
- The app generates a random
code_verifierand redirects you to/authorizewithclient_id,redirect_uri,scope,stateandcode_challenge(the verifier's SHA-256 hash). - You sign in and consent at the authorization server.
- It redirects back to
redirect_uriwith a one-time authorization code and thestate, which the app checks to block forged callbacks. - The app posts the code and
code_verifierto/token. The server checks the hash and returns tokens. - The app calls the API with
Authorization: Bearer <token>.
The tokens never pass through the browser's address bar. PKCE (Proof Key for Code Exchange) makes an intercepted code useless. The older implicit flow and the password grant are deprecated.
Short cards, renewal slips
The key card only lasts an hour, so a lost one does little harm. Along with it, the print shop gets a renewal slip it can swap at the desk for a fresh card, without bothering you. When you tell the desk you've changed your mind, the slip is cancelled.
Access and refresh tokens
- Access tokens are short-lived (minutes) and sent with every API call. They may be opaque strings or JWTs.
- Refresh tokens are long-lived, kept only by the client, and sent only to the token endpoint to get new access tokens. Servers often rotate them, issuing a new one each time and detecting reuse of an old one.
Revoking a grant kills the refresh token immediately. An already-issued access token usually keeps working until it expires, which is one reason to keep them short.
For machine-to-machine calls with no user, the client credentials grant gives a service its own token.
OpenID Connect (OIDC) adds sign-in on top of OAuth: request the openid scope and the token response also
includes an ID token, a JWT saying who the user is (sub, email, name) and which app it's for (aud).
"Sign in with Google" is OIDC.
Where to keep tokens: for browser apps, a back end that keeps tokens server-side and gives the browser an
HttpOnly session cookie is safer than storing tokens in JavaScript-readable storage.