Server-rendered web applications
When the printer's website is its own 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. client, every 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. arrives at its backend and none needs to reach Ava's browser. The browser holds only the printer's session cookieA small value that a website asks the browser to store and send back with later matching requests. Cookies commonly carry a session identifier, so the site can recognize an ongoing sign-in without your password., the backend keeps her photo connection encrypted, and signing out of the printer and disconnecting her photos become separate actions.
What the browser holds
The printer's website is the version of the printing application we have followed since the first lesson. Ava visits printer.example, signs in to her printer account, and each page arrives from the printer's servers already assembled. When she selects Connect photo account, the redirect to the photo service, the callback, and the token request all pass through code that the printing company runs on its own backend.
That arrangement has a useful property. The OAuth client is the backend, the 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. registered as photo-printer, and every token the photo service issues arrives in a direct response to it. Her browser carries the 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. request out and the code back, but it never needs to hold a token.
After Ava signs in, the printer's response gives her browser one thing that matters here, the printer's own 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. cookie. Like every domain, token and key in these examples, its value is fictional:
Set-Cookie: __Host-printer-session=demo-session-31; Path=/; Secure; HttpOnly; SameSite=LaxThese are the protections the Staying signed in lesson described. Secure keeps the cookie off unencrypted connections, HttpOnly keeps it out of reach of the page's scripts, SameSite=Lax limits when other sites can cause the browser to send it, and the __Host- prefix keeps it to printer.example alone.
The cookie identifies a printer session. It is not 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., and the photo service would not recognize it. That matters when something goes wrong in the browser. A script injected into a printer page could misuse her printer session while the page is open, but it would find no access token or refresh tokenA longer-lived credential that a client sends to the authorization server, never to an API, to get new access tokens without asking the person again. to carry away and use against 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. from somewhere else, because none was ever sent to the browser.
The backend has to keep it that way. Tokens do not belong in the HTML it renders, in a script variable added for convenience, or in the response of an endpoint that the page calls. Once a token appears in a page, the website takes on the exposure of a browser application without any of the reasons for it.
printer.example
- Cookie
- __Host-printer-session
- Access token
- None
- Refresh token
- None
- Injected script can misuse her printer session while the page is open
- It finds no token to carry away
Connection: conn-5521
- Printer account
- 8812 (Ava)
- Access token
- encrypted
- Refresh token
- encrypted
- Key
- In a key management service
A copy of the database reveals nothing usable on its own
Tokens do not belong in the HTML it renders, in a script variable, or in an endpoint the page calls.
Keeping tokens on the server
When the token response arrives, the backend saves it in a connection record: the printer's record of the connection to Ava's photo account, tied to her printer account rather than to the browser session that created it. A simplified record might look like this:
Connection: conn-5521
Printer account: 8812 (Ava)
Authorization server: https://auth.photos.example
Client ID: photo-printer
Granted scope: photos.read
Access token: encrypted, expires 2026-10-01 09:10 UTC
Refresh token: encrypted
Status: active
Connected: 2026-10-01 09:00 UTCThe granted scope is the one the token response reported, which may be narrower than the printer requested. Tying the record to Ava's printer account lets the connection outlive the visit that created it, and it makes the ownership rule easy to state: work done for one printer account must never be able to load another account's connection.
The refresh token is the valuable part. It can keep producing access tokens long after Ava has left, so the printer protects it like any other long-lived secret. The Refresh tokens and rotation lesson described how an 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. can store refresh tokens as hashes, because it only needs to recognize one when it is presented. The printer cannot do that. It has to send the original value 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., so it needs a form it can turn back into the token.
Instead, it encrypts the tokens before storing them, with a key held in a key management service rather than in the database or the application's configuration. A copy of the database then reveals nothing usable on its own, and only the components that call the photo API are allowed to ask for decryption. The same care applies wherever a token might pass. Request logs, error reports, analytics events, and support tools can record that a connection exists and whether it works, but never the tokens themselves.
Sessions and connections
Ava's printer session and her photo connection are now separate things, and they end in different ways:
| What happens | Printer session | Photo connection |
|---|---|---|
| Ava signs out of the printer | Ends | Stays active |
| Ava selects Disconnect in the printer | Unaffected | Revoked, then deleted |
| Ava removes the printer at the photo service | Unaffected | Ended by the photo service |
Signing out ends the session. The printer invalidates its server-side session record and clears the cookie, as Staying signed in described, and leaves the connection alone. That is deliberate. Ava connected her photo account so the printer could prepare next December's calendar, and signing out of a shared computer at a library should not cancel that.
Disconnect is the action that ends the grant, by revoking the refresh token as the Token revocation lesson described. Removing the printer at the photo service ends the grant from the other side. Neither event touches her printer session, because the session was never what gave the printer access to her photos.
Work without a browser
In December, the calendar job runs on the printer's servers. Nobody is signed in, there is no browser, and there is no session cookie. All the job has is the connection record, and it works through it in order:
- Load the connection for the printer account whose calendar is due, and check that it is active.
- DecryptTransforming data so that only someone holding the right key can read it. It protects confidentiality and, unlike hashing, is designed to be reversed by the key holder. the refresh token and send it to the token endpoint with the printer's client authentication.
- Save the replacement refresh token, if one is returned, before relying on the new access token.
- Request twelve recent photos from the photo API with the access token.
- If the token endpoint answers
invalid_grant, mark the connection as needing attention and send Ava a notice instead of retrying.
Steps 2 and 3 are the refresh token grant as you have already seen it, including the rule that only one refresh for a connection runs at a time.
The first step needs more care than it seems to. A background job usually receives its work from a queue, as a message such as "prepare the calendar for printer account 8812." It acts with whatever authority the connection record gives it, and no person is watching to notice a mistake. If a bug or a forged message could make the job load the wrong connection, it would print someone else's photos with entirely valid tokens. The job should find the connection through the printer account it is working for, rather than trusting a connection identifier carried in the message, and record which connection it used.
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
- Play the printer's backend and keep every token off the browserRun the confidential web flow with your shell as the backend, keep a connection record on the server side, and show that signing out ends a session but not the connection.