Cryptography / OAuth 2.0

OAuth 2.0

How an app gets limited access to your data without ever seeing your password: four roles, one consent screen, a code swapped for tokens, and why none of it proves who you are.

Intermediate7 steps~7 min
Ana — resource owner: the photos are hers
Print App — client: wants access to the photos
Auth server — checks Ana, asks consent, issues tokens
Photo API — resource server: holds the photos
// THE SHORT VERSION

In 30 seconds

OAuth 2.0 lets you give an app limited access to your data on another service without giving it your password.

The app sends you to the service’s authorization server, where you approve specific scopes. The app receives a one-time code and swaps it for an access token, which it shows to the API, and a refresh token for getting new ones. PKCE stops a stolen code being used.

OAuth says what an app may do, not who you are. Logging in is the job of OpenID Connect, built on top.

OAuth 2.0, step by step

STEPS01 / 07

This is the authorization code flow, the one recommended today. Token values are the examples printed in RFC 6749 and RFC 7636; names ending in .example are reserved for documentation.

STEP 01 / 07 Four roles
THE MATH
owner: Ana client: Print App authorization server resource server: Photo API
  1. Step 1: Four roles. Ana keeps her photos at a photo service and wants a printing app to fetch them. Giving the app her password would hand it everything, for ever. OAuth 2.0 names four roles instead: the resource owner (Ana), the client (the app), the authorization server that issues permissions, and the resource server that holds the data.
  2. Step 2: Ask for permission. The app does not ask Ana for her password. It sends her browser to the authorization server with its own ID and the scopes it wants, here only photos.read. Ana logs in there, where the app cannot see, and gets a consent screen listing exactly what the app is asking for. A scope is a named slice of access.
  3. Step 3: A code, swapped for tokens. After Ana approves, the authorization server sends her browser back to the app with a short-lived, one-time authorization code. The app then calls the server directly, away from the browser, and exchanges the code plus proof of its own identity for tokens. The tokens never pass through the browser’s address bar.
  4. Step 4: PKCE: prove you asked. A code can be intercepted on its way back, and apps on phones or in browsers cannot keep a client secret. With PKCE the app invents a random verifier, sends only its hash with the first request, and reveals the verifier when it swaps the code. The server hashes it and compares, so a stolen code alone is worthless.
  5. Step 5: Use the access token. The app sends the access token with each request to the photo API. The API checks that the token is genuine, unexpired and carries the scope the request needs, then answers. This token allows viewing photos, so a request to delete one is refused. Whoever holds a bearer token can use it, which is why it is kept short-lived.
  6. Step 6: Refresh without asking again. Access tokens expire quickly, often within an hour. Rather than send Ana through the consent screen again, the app presents its refresh token to the authorization server and receives a fresh access token. The refresh token is long-lived and powerful, so it goes only to the authorization server, never to the API.
  7. Step 7: Permission, not identity. OAuth 2.0 is about authorization: what the app may do. The access token is meant for the API, and nothing in the standard tells the app who the user is or whether she just logged in. Treating “I got a token” as “this is Ana” has caused real security bugs. OpenID Connect adds a separate ID token for that.
VALUES SO FAR
clientprint-apppublic
scope····asked
code····one-time
access····secret
refresh····secret
verifier····secret
hash····public
expires····access

Spotted a mistake on this page?