Why use the authorization code flow?
Connecting the printer to Ava's photos takes several messages, and some of them have to pass through her browser. The authorization code flow sends only a short-lived code along that route and delivers the access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in. in a direct request the browser never sees.
Carrying the result back
Ava has chosen a photo book in the printing application. The next step is to choose photos from her photo account, so she selects Connect photo account.
That button starts several conversations. Ava's browser visits the photo service, the photo service sends a result back to the printer, and the printer asks for the credential it will use at 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.. The authorization code flow decides which of those messages travel through the browser and which do not, and following who sends each one explains why OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. uses more than one endpoint.
The printer needs an access tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret., and only Ava can approve one, at the photo service. Her approval happens in her browser, on the photo service's own pages, as Grants, scopes, and consent described. Its result then has to travel back to the printer, and the only route from those pages to the printer runs through her browser: the photo service redirects the browser to the printer's redirect URI, one of the endpoints introduced in Roles and endpoints, and puts the result in that URLA web address, such as https://photos.example/api/albums/42?limit=10. It names how to connect (https), the domain, the path to a particular page or resource, and any extra parameters after the question mark..
A browser is a poor place to carry a token, and a URL is a poor place to put one.
How addresses leak
Imagine the photo service sent the access token itself in the redirect, so Ava's browser opened an address like this:
https://printer.example/oauth/callback?access_token=demo-access-token-7That address does not stay in one place. Several ordinary features of the web copy it.
- Browser history. The browser saves every address it visits. Anyone who later uses the same computer can find it, and many browsers sync history to other devices and to the browser maker's account service.
- Server logs. Web servers usually record the full address of every request they receive, query included. The printer's logs, and those of any proxy or load balancer in front of it, are kept for weeks or months, read by staff, and copied into monitoring tools that were never designed to hold credentials.
- The
Refererheader. When the printer's callback page loads an image, a script or a font from another site, or Ava follows a link from it, the browser can send that site the page's address inReferer, as What browsers add showed. Many browsers now trim it to the site name by default, but a page can ask for more, and a client cannot rely on every browser and setting. - Shared links. People copy addresses from the address bar to share a page, report a problem or paste into a support ticket, and screenshots capture the address bar along with everything else.
Any of those copies would put the token in someone else's hands, and a bearer tokenA token that works for whoever presents it: the API checks that the token is genuine and current, not who is holding it. That is why bearer tokens must be kept from being copied. works for whoever holds it. Nothing in a URL ties the token to the application that asked for it.
Splitting the delivery
The authorization code flow splits delivery into two messages. The browser carries back only an authorization code to the printer's redirect URI. The code is the intermediate credential Roles and endpoints described: the photo API does not accept it, it works once, and it expires within minutes. The printer then sends that code straight to the photo service's token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect., in a request of its own, and the tokens arrive in the response to that request rather than in any address.
How applications communicate named these two routes. The redirect travels the front channel, through the browser, where addresses get copied. The token request travels the back channel, directly from the printer to the photo service. The back channel also gives the photo service something a redirect cannot: a chance to check who is asking before it hands over a token. The printer proves it started this attempt with the one-time PKCEProof Key for Code Exchange, pronounced pixie: the client creates a secret for each authorization attempt, sends only a hash of it at the start, and proves it holds the secret when redeeming the code, so a stolen authorization code is useless to anyone else. verifier from Public and confidential clients, and as a confidential clientAn OAuth client, typically running on a backend, that can keep a credential such as a client secret or private key protected, and so can authenticate itself to the authorization server. it also authenticates with its own credentials. A code copied from a log or a shared link arrives with neither.
The address holds an access token
- Callback address
- ?access_token=...
- Photo API, given a copy
- Accepts it
- To use a copy
- Nothing more
- Token arrives
- In the address
A leaked copy works for whoever holds it
The address holds an authorization code
- Callback address
- ?code=...
- Photo API, given a copy
- Refuses it
- To use a copy
- Verifier and client authentication
- Token arrives
- In a direct response
Works once and expires within minutes
A code copied from a log or a shared link arrives with neither.
Where the token response ends up depends on the deployment. A server application such as the printer receives it on its backend and can keep the token out of the browser altogether. A public browser application has no backend, so it receives the response in JavaScript and still needs protection against injected scripts. A code flow with PKCE does not make browser token storage disappear. It keeps bearer tokens out of the authorization redirect and binds redemption of the code to the attempt that started the request.
Preparing the connection
Now follow one connection from the start. Before Ava ever selects Connect, the printer has a client registration with the photo service. That registration includes a client ID and an allowed return address. In this fictional example, they are:
Client ID: photo-printer
Redirect URI: https://printer.example/oauth/callbackThe printer also knows the photo service's authorization and token endpoints from trusted configuration. It does not discover where to send credentials by trusting an address supplied in a returning browser request.
When Ava selects Connect, the printer creates a pending transaction: a record tied to her current sessionThe ongoing connection between later requests and an earlier sign-in, so you are not asked for your password on every click. It is often tracked with a cookie holding an unpredictable session identifier. that holds what the printer needs to finish this particular attempt. It holds two fresh random values. One, called state, travels out with the request and must come back unchanged, so the printer can recognize the response as the answer to this attempt. The other is the PKCE verifier, which never leaves the printer until it redeems the code. Proof Key for Code Exchange (PKCE) and Correlating requests and responses examine each value in detail.
Visiting the photo service
The printer directs Ava's browser to the photo service's authorization endpoint. Its request identifies the printer, asks for permission to read photos, and names the registered return address. Everything in it is visible in the address bar, so it carries nothing secret, and the verifier stays behind in the pending transaction.
The photo service checks the request and establishes which photo account is involved. Ava might need to sign in, or she might already have a session there. The photo service then decides whether to authorize the requested access, and depending on earlier approval and its policy, Ava may see a consent screen. If she types a password, she types it on the photo service's own page, never the printer's.
When the request succeeds, the photo service redirects Ava's browser back to the printer's callback with the authorization code. This is the one moment in the flow when a credential sits in an address, which is why the code is designed to be worth so little there.
Finishing the exchange
The callback carries the code, the state value, and an iss parameter naming the issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts., the authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. that answered. The printer first checks that state matches the pending attempt for Ava's session and that iss names the photo service it sent her to. It claims that attempt once, so a second copy of the same callback finds nothing to finish, and it moves her browser on to a clean address so the code does not stay in the address bar. Only then does its backend send the code to the token endpoint, together with the PKCE verifier and the client authentication its registration requires.
If those checks succeed, the token endpoint returns an access token to the backend. The backend uses that token when requesting photos from the API, and the API still decides whether each operation is permitted.
The browser carried the code. The access token travelled only in the direct exchange between the backend and the token endpoint, and from there to the API, and the backend keeps it out of browser-facing responses.
The printer can now retrieve the photos Ava authorized. It has not received her photo account's password, and this OAuth exchange alone has not established a standardized sign-in result for a printer account. OpenID Connect addresses that separate requirement.
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
- Run one connection and see what each channel carriesSend Ava through Lab Photos as the printer, see only a code come back in an address, redeem it in a direct request, use the token at the photo API, then compare a browser client.