Keeping context across services
Your photo book order passes through three of the printer's services, and each call now says which workload is calling. This lesson adds the other half: how each service learns whose request it is handling, why forwarding your token is the wrong answer, and how exchanging it for a narrower token at each hop keeps both you and the acting service visible.
Whose request is it?
When you order a photo book, you sign in at printer.example and the printer's web app sends your order to the order service with your 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.. The order service asks the render service to build the book. The render service needs the copies of your photos that the printer imported earlier, so it asks the storage service for them. Service-to-service access met this question behind 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.; inside the printer it comes up at every hop.
Each of those calls carries the caller's workload identityAn identity that belongs to software that runs and acts without a person directing each request, such as a service, a scheduled job or a pipeline step. It names the logical workload, not one running copy, and each copy holds its own credential., so the storage service knows the render service is calling. It does not know whose photos the render service should be reading. Every hop involves two identities: the workload making the call, and the person the request is for. If storage relies only on the render service's identity, the rule can say "the render service may read customers' files" and no more. That rule may be limited to certain folders, but it cannot say which customer asked for this particular order, so a bug in the render service, or an attacker who controls one copy of it, can read everyone's photos.
If the storage service knows both identities, the rule can be "the render service may read a customer's files while it is working on that customer's order." The question is how to tell the storage service whose request this is in a way it can rely on. The quickest answer is a header:
GET /files/cust-1182/lisbon-trip/page-01.jpg HTTP/1.1
Host: storage.printer.example
X-User-Id: cust-1182All domains, credentials, and tokens in these examples are fictional. Here cust-1182 is your customer ID at the printer. Nothing about that header comes from you: the storage service would be trusting the render service's word, and any caller, or any bug that builds the wrong header, could name any customer.
Forwarding the original token
The next idea is to pass your own access token along the chain, so the storage service can see it was issued to you. That is safer than an unsigned header, but it causes three problems.
The first is the audience. Your token was issued for the order service, with aud set to https://orders.printer.example. If the storage service accepts it anyway, it accepts tokens meant for other services, and a token leaked from any service in the chain works at the storage service too.
The second is breadth. Your token carries everything you allowed the printer's web app to do, such as placing orders and reading your account details, while the render service needs to read one album.
The third is spread. Every service that handles your token can present it anywhere it is accepted until it expires, so the more hops it travels, the more places it can leak from.
There is a related danger in the other direction. Suppose the render service fetched files using its own broad identity, based on an album name in the order. A customer could put someone else's album in their order, and the render service would fetch it with authority the customer never had. A program that holds authority of its own and is tricked into using it on someone else's behalf is called a confused deputy. Tying each request to the authority of the person it is for, rather than to the service's own, is what prevents it.
Exchanging for a narrower token
The printer's 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 issue a new token for each hop, with the token exchangeAn OAuth grant, RFC 8693, in which a client trades a token it holds for a new one, usually for another audience, with narrower access, or acting for someone. It lets a service call the next service without passing the original token along. from Following a token exchange. The order service authenticates with its workload certificateA signed statement that a public key belongs to a name, such as a website's hostname, for stated purposes and a limited period. A certificate authority signs it, so software that trusts the authority can connect the key to the name. over mutual TLSTransport Layer Security, the protection behind HTTPS. It encrypts traffic between two connected systems, detects changes on the way, and uses certificates so the client can confirm which server it reached. and asks 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. server to exchange your token for one meant for the render service:
POST /token HTTP/1.1
Host: auth.printer.example
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:token-exchange
&subject_token=demo-your-access-token
&subject_token_type=urn:ietf:params:oauth:token-type:access_token
&audience=https://render.printer.example
&scope=books.renderThe authorization server checks that your token is valid and that the order service may make this exchange. Its policy then says what your token may become: a token carrying orders.write may be translated into books.render for the render service, the narrower permission that rendering your order needs, and into nothing more. Then it issues the new token. Here are the two tokens side by side, decoded:
{
"iss": "https://auth.printer.example",
"sub": "cust-1182",
"aud": "https://orders.printer.example",
"client_id": "printer-web",
"scope": "orders.write account.read",
"exp": 1790001500
}{
"iss": "https://auth.printer.example",
"sub": "cust-1182",
"aud": "https://render.printer.example",
"scope": "books.render",
"act": { "sub": "spiffe://printer.example/prod/orders" },
"exp": 1790000900
}The subject is still you. The audience is now the render service, so the token is useless anywhere else. The scope covers only rendering, and the token expires sooner. A new claim, act, records that the order service is acting for you.
Two of those changes are separate decisions. An exchange can reduce the scopes and keep the same audience, and a token with fewer scopes is still a token for the order service, not one the render service should accept. Choosing the render service as the audience is a decision of its own. So is the translation from orders.write to books.render: the server allows it because its policy says rendering an order is part of what placing it authorized, not because the names look related.
The render service does the same when it needs your files. It exchanges the token it received for one whose audience is the storage service and whose scope is files.read. The storage service then checks three things: the token is meant for it, the files belong to the token's subject, and the actor is the render service. A forwarded token fails the first of those checks:
The same context helps when a reply goes missing. When the book is rendered, the render service exchanges its token again, this time for a storage token with scope files.write, and saves the finished file to the storage service as job rj-2207. Suppose storage saves it, but its answer is lost on the way back. The render service cannot tell whether the file exists, and uploading again under a fresh token could leave two copies of your book, or replace one that the next step has already read. So it treats the upload as unresolved and asks storage about the request it already made, by the same job identifier, for the same customer:
GET /uploads/rj-2207 HTTP/1.1
Host: storage.printer.example
Authorization: Bearer demo-storage-token
HTTP/1.1 200 OK
Content-Type: application/json
{"job": "rj-2207", "owner": "cust-1182", "status": "stored", "sha256": "demo-digest-of-the-book"}The file is there, with the digest the render service computed before it sent it, so the render service carries on without a second upload. Storage checks the lookup exactly as it checks an upload: the token must be meant for storage, and its subject must own the job. Asking about a lost reply never becomes a way to read another customer's files.
Delegation and impersonation
The act claim is what makes this delegation: the token says the request is yours and that the order service is the one acting for you. Both identities stay visible, so the render service can require that only the order service starts renders for customers, and its records can show who did what.
The same exchange could instead produce a token that leaves out the actor and looks as though it had been issued to you directly. That is impersonation, and the receiving service cannot tell that another party was involved. Impersonation has uses, such as old services that cannot read an actor, but it gives up accountability, so where it matters who acted, prefer delegation. Delegation, impersonation, and actor tokens looked at when impersonation is justified and how to keep it narrow.
When the render service exchanges its token in turn, the storage service's token records both actors. The outer act names the render service, the current actor, and a nested act inside it names the order service, which acted before it:
"act": {
"sub": "spiffe://printer.example/prod/render",
"act": { "sub": "spiffe://printer.example/prod/orders" }
}The storage service bases its decision on the token's subject and the current actor. The nested actors are a record of the path the request took, which is what an investigation reads when it needs to know how a request reached storage, not only who sent it last.
Context inside one trust domain
A round trip to the authorization server at every hop is fine for a chain of three services. Some systems handle a single request with dozens of internal calls, and exchanging tokens at each one adds cost and delay.
A common alternative is to create a signed record of the request's context once, where the request enters the system. The record says who the user is, what the request is for, and which service received it. It travels with every internal call, and every service verifies its signatureA value made with a private key that lets anyone holding the matching public key check that a message came from the key's holder and has not been changed. It proves origin and integrity; it does not hide the message. before relying on it. No service along the way can change the record; it can only ask 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. for a new one, and no internal service accepts a user's identity from an unsigned header.
Transaction Tokens (Txn-Tokens), defined in an 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. working group draft that is not yet a standard, carry short-lived transaction context in a tctx claim inside one trust domain. A Transaction Token does not replace the caller's workload authenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do., and it is not an OAuth access token. Calls that leave the trust domain, such as the batch job's requests to the print lab, still need access tokens issued for that outside audience.
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
- Keep the person's subject and the actor chain across servicesFollow an errand through order, render and storage, read each hop's exchanged token in Audit, see a chain longer than the tenant allows refused, and recover a reply that never arrived.