TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

All concepts ยท web ยท security ยท auth

OAuth 2.0

How one app gets limited, revocable access to your data in another service without ever seeing your password. An authorization server issues scoped, short-lived access tokens after you approve.

13 min3 animations

๐Ÿ“š Builds on๐Ÿ“ฎ HTTP

๐Ÿ–จ๏ธ Cheat sheet

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:

RoleExample
Resource ownerYou
ClientThe photo-printing app
Authorization serverGoogle's or GitHub's sign-in and consent service
Resource serverThe 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.

Don't hand over your house key4 steps
๐Ÿง’You๐Ÿ–จ๏ธPrint shop๐Ÿ–ผ๏ธ Your photo room
Press play and watch it happen!

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:

  1. The app generates a random code_verifier and redirects you to /authorize with client_id, redirect_uri, scope, state and code_challenge (the verifier's SHA-256 hash).
  2. You sign in and consent at the authorization server.
  3. It redirects back to redirect_uri with a one-time authorization code and the state, which the app checks to block forged callbacks.
  4. The app posts the code and code_verifier to /token. The server checks the hash and returns tokens.
  5. 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.

The authorization code flow5 steps
๐Ÿง’You๐Ÿ–จ๏ธPrint shop๐Ÿ›Ž๏ธFront desk๐Ÿ–ผ๏ธ Photo room
Press play and watch it happen!

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.

Short cards and renewals4 steps
๐Ÿง’You๐Ÿ–จ๏ธPrint shop๐Ÿ›Ž๏ธFront desk๐Ÿ–ผ๏ธ Photo room
Press play and watch it happen!
๐Ÿ”ง Going deeper

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.

๐Ÿงฉ Quick quiz

Quick quiz! 3 questions about OAuth 2.0, told the same way as the story.
  1. Why not hand the print shop your master key?
  2. Why does the desk give you a claim ticket rather than the key card itself?
  3. The key card expires after an hour. How does the print shop get a new one without bothering you?
Answer every question to check.Passing marks OAuth 2.0 as done.
Got the hang of OAuth 2.0? Mark it as done, and your paths show how far youโ€™ve got.

Saved in this browser.

๐Ÿ›ก๏ธ Signing in safelyStep 5 of 5

โ† Previous on the path๐ŸŽŸ๏ธ JWTLast step ๐ŸŽ‰Back to the path