The problem OAuth solves
Ava wants a printing application to make a book from one of her photo albums. The printer needs to read those photos, and the obvious shortcut, handing it her password, gives it far more than the job needs. OAuth gives the printer access of its own instead.
Connecting two services
Giving another application limited access introduced the situation this series follows. Ava keeps her photos with an online photo service, and she wants a separate printing application, the printer, to turn her album "Summer trip" into a photo book. Before following OAuth's answer message by message, it helps to look closely at the problem itself, because every part of the answer exists to fix one piece of it.
Ava could download the photos herself and upload them to the printer. That works once, but she would rather connect the two services, so the printer can show her albums and fetch the photos she picks, and fetch them again if she reorders the book next year. So the printer needs some way to make requests to the photo service on Ava's behalf.
The oldest answer was to ask Ava for her photo account's email address and password, store them, and sign in as her whenever it needed something. Many early integrations worked exactly that way. Look at what that gives the printer and what it costs Ava:
- Everything Ava can do. Her password lets the printer delete albums, change her settings, read photos she never meant to print, and do anything else her account allows. Nothing limits it to reading one album.
- No separate way to stop it. If Ava wants the printer to stop, the only reliable step is changing her password, which also cuts off every other application she gave it to and means typing the new one everywhere she signs in.
- Another copy to protect. The printer now stores Ava's password. If the printer is broken into, her photo account goes with it.
- No way to tell who acted. When the printer signs in with Ava's password, the photo service sees Ava signing in. It cannot tell her own requests from the printer's, so its records cannot say which one deleted an album.
The sign-in methods from Authentication methods and MFASigning in with more than one type of factor, such as a password plus a code from an enrolled authenticator app. Two passwords are still one factor type, however many screens they appear on. make the shortcut worse. If Ava's account asks for a one-time code or a passkeyA credential that your phone, computer or password manager creates for one website and keeps for you, unlocked with a fingerprint, face or PIN. Nothing you could type is sent, and it works only for the site it was made for, which helps it resist phishing., the printer cannot complete her sign-in at all. A passkey from Security keys, passkeys, and biometrics only works for the photo service's own site, which is the point of it. And an application that asks people to type their password for one service into another teaches exactly the habit phishingTricking someone, usually with a message and a look-alike sign-in page, into handing over the evidence that opens their account, such as a password or a one-time code. depends on.
What Ava needs is a way to authorize one particular application to do one particular kind of work, without telling it her password, and to take that back without changing anything else.
Delegated access
This is the delegated accessLetting an application do a limited task with someone else's authority, such as a printing service reading your photos, without giving it your password. OAuth 2.0 is the usual framework for arranging it. problem from Fundamentals: Ava wants the printer to act with some of her authority, not all of it. OAuth 2.0 is the framework most services use to arrange that. In the flow this series studies first, the printer sends Ava's browser to the photo service, where she signs in on the photo service's own page and approves the request. The photo service then issues the printer an access token, and the printer presents that token, not Ava's password, each time it asks the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. for photos.
Here is what such a request looks like. The printer asks the photo API for album 4420 and puts the token in the Authorization request header, after the word Bearer:
GET /albums/4420 HTTP/1.1
Host: api.photos.example
Authorization: Bearer mF_9.B5f-4.1JqM
HTTP/1.1 200 OK
Content-Type: application/json
{"id": "4420", "title": "Summer trip", "photos": ["9011", "9012"]}The token value here is a short placeholder; real tokens are much longer. The photo API answers because it accepts the token, not because it saw Ava's password. Every piece of the problem above now has an answer:
- The token carries the access the photo service granted, for example permission to read photos, so it can be far narrower than Ava's account.
- The token belongs to the printer's connection alone. Ava can end that connection from her account at the photo service without changing her password, and the service can also let the token expire after a set time.
- The printer stores a token, not Ava's password, so a break-in at the printer exposes the printer's access, not her account.
- The photo service issued the token to the printer, so its records can say the printer made a request on Ava's behalf.
When Ava disconnects the printer, the photo service stops honoring the token, and the same request is refused. The WWW-Authenticate response header says why, using the error code invalid_token:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token"The token is still a credential, so it needs protecting. Most access tokens are bearer tokens, as Giving another application limited access explained: whoever holds a copy can use the access it represents until it expires or the service stops honoring it. OAuth makes the printer's access smaller and separable; it does not make a leaked token harmless.
How small the access is depends on the photo service. One service might let Ava grant access to the single album "Summer trip". Another might offer only permission to read her whole library. Both use OAuth, but Ava would be granting very different authority, and the photo API has to enforce whichever one it offers on every request. Using OAuth does not by itself mean the printer can see only the photos Ava picked.
What the printer holds
- Read photos
- Granted by the photo service
- Ava's password
- Never seen
- Which albums
- Up to the photo service
- Ending it
- Ava disconnects the printer
- Who signed in
- Not established
Whoever holds a copy can use the access it represents.
Access and sign-in
Ava signs in to the photo service while she approves the connection, so the photo service knows whose account is involved. The printer does not learn that in any standard way. It holds an access token meant for the photo API, and an access token answers "may this request read these photos?", not "who is using the printer right now?".
That matters if the printer also wants a "Sign in with your photo account" button for its own customer accounts. Treating "I received an access token" as "Ava signed in to me" would be a mistake: a token issued to some other application, presented to the printer, says nothing about who is in front of it. The printer needs a result made for it, about a sign-in, which an access token is not.
OpenID Connect adds that authentication layer on top of OAuth 2.0, with an ID tokenIn OpenID Connect, a signed JWT from the provider, addressed to one client, saying which account authenticated, when, and in answer to which request. It reports a sign-in and is not used to call APIs. addressed to the printer itself, as Connecting OAuth and OpenID Connect outlined. It comes after the OAuth exchange, in From delegated access to sign-in. Until then the task stays specific: the printer reads the photos Ava allows, and never collects her photo account's password.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Delegate access to photos without sharing a passwordLet an application read Ava's album with a token instead of her password, then disconnect it from her account page while her password keeps working.