Redirects and authorization codes
Once the photo service has finished with Ava, it redirects her browser back to the printer with the result in the address. That address carries a credential, so the photo service sends it only to a return address registered exactly, and the printer handles the code it receives as something to protect and redeem, never to read.
Choosing the return address
Imagine changing the delivery address on a parcel after someone has packed it. It would not help to pack the right item if it then went to the wrong recipient. Similarly, a correctly issued authorization codeA short-lived, single-use value that the authorization server sends back to a client through the browser. The client swaps it for tokens in a direct request to the authorization server; APIs never accept it. can be exposed if the server sends it to an untrusted address.
The printer's request named its return address in redirect_uri, but anyone can write any value into a 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.. So the photo service compares the requested redirect URI with the addresses registered for the printer, character for character. For a web client like the printer this is exact matching: a different path, a changed letter or an added or missing trailing slash makes a different address, and the server rejects it. Accepting anything that begins with the printer's hostname is not a substitute for this check, because other pages on the same site, or on a lookalike site, could then collect codes.
Our example registers one HTTPSHTTP protected by TLS, so traffic is encrypted and checked for changes while it travels. It protects the connection, but does not prove that the site at the other end is honest. address:
https://printer.example/oauth/callbackRegistered for the printer
https://printer.example/oauth/callback
Requested as registered
- redirect_uri
- /oauth/callback
- Matches
- Character for character
- Server answers
- Redirect with the code
Code goes to the printer
One character added
- redirect_uri
- /oauth/callback/
- Matches
- No
- Server answers
- Its own error page
Nothing is sent back
Unregistered host
- redirect_uri
- example.com/callback
- Matches
- No
- Server answers
- Its own error page
Nothing is sent back
Accepting anything that begins with the printer's hostname is not a substitute for this check.
When the address does not match, the server cannot know who controls it, so it does not redirect there at all, not even with an error. It shows its own error page in the browser instead. Installed applications receive the response in other ways, such as loopback addresses whose port is allowed to vary. Redirect URIs and client metadata, in Clients and registration, covers those cases rather than stretching this web example to fit them.
Returning the response
Once Ava has signed in and the photo service has decided to allow access, it answers her browser with a redirect. A successful response in our example looks like this:
HTTP/1.1 302 Found
Location: https://printer.example/oauth/callback?code=demo-code-7&state=demo-attempt-7&iss=https%3A%2F%2Fauth.photos.example
Cache-Control: no-storeAs How applications communicate showed, the browser follows the Location address with a new request, so the code is carried in the query string of the request to the printer. Cache-Control: no-store tells the browser and any cache along the way not to keep a copy of this response, which holds a credential.
Do not treat every redirect status as equivalent. After a 302 or 303, the browser sends a fresh GET to the new address. A 307 or 308 instead repeats the original method and body. If a sign-in endpoint answered a password POST with such a redirect, the browser could forward the submitted password to the redirect destination. A server must choose a status that changes a credential submission into a safe GET, rather than depending on the destination to discard the body.
iss names 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 sent the response, 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. from Roles and endpoints. It comes from an extension to 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. called authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. response issuer identification, so not every authorization server includes it. Our photo service does, and Correlating requests and responses explains how the printer checks it.
state carries back the value from the authorization request. The printer must validate it against its pending transaction. The presence of familiar-looking parameter names does not make a callback trustworthy. A refusal returns the same way, with an error parameter in place of the code, and the printer checks it just as carefully.
Handling the code
The code is short-lived and single-use. The photo service records which client it was issued to, which redirect URI the request used and, in this flow, the 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. challenge, and checks all of them when the code is redeemed. The printer should treat its contents as opaque: often it is just a random string, so the printer presents it to the 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. rather than trying to decode an account identifier or permissions from it.
Codes still need protection. A callback URL can end up in analytics, request logs, browser history or the Referer header, the leaks Why use the authorization code flow? listed. Keep the callback focused on processing the response, avoid third-party resources there, send Referrer-Policy: no-referrer, and move the browser to a clean application URL after handling it. Do not record the full callback query in application logs; record the outcome instead.
Using a code creates an opportunity for the 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. endpoint to perform additional checks before issuing an 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.. It does not make redirect validation or credential handling unnecessary.
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
- Test exact redirect matching and read a real redirect responseFind which return addresses Lab Photos accepts for the printer, read a real redirect response with its state and iss, and treat the code that comes back for Ava as an opaque value.