Service-to-service access
When the photo API needs another internal service to answer the printer, it makes a request of its own across another trust boundary. That request has to carry who the user is and what the call may do, in a form the next service can check for itself, and the services need credentials of their own that are not long-lived secrets.
A call behind the API
The photo API is rarely the end of the line. It validates the printer's token and confirms that album 42 is Ava's, but the image files themselves live in an internal storage service at https://storage.photos.example. To answer the printer, the photo API has to make a request of its own.
That second request crosses another trust boundary. The storage service has to decide whether to hand over the files for album 42, and the photo API has to present something that lets it decide.
Start with what the photo API already holds, the printer's 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.. Whether the API decodes it as a JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing. or receives the same fields from introspectionA way for an API to ask the authorization server what a token means: whether it is still active and what access it carries. APIs need it when a token is a random string that only the authorization server can interpret., it knows these claims. All domains, tokens and keys in these examples are fictional.
{
"iss": "https://auth.photos.example",
"sub": "user-2048",
"aud": "https://api.photos.example",
"client_id": "photo-printer",
"scope": "photos.read",
"iat": 1790845200,
"exp": 1790845800,
"jti": "demo-token-id-7"
}Most of what the storage service would want to know is here: whose photos these are (Ava's, user-2048), which application asked (photo-printer), and what kind of access she approved (photos.read). One claim stands in the way. aud names the photo API, and the Scopes and audiences lesson explained why a different API should refuse a token issued for someone else.
The photo API needs a way to call storage that still gives storage enough to make its own decision. Several approaches are common, and the obvious ones each lose something.
Shortcuts that lose something
Calling as itself. The photo API is registered with 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. as 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. photo-api, so it could use the client credentials grant and present a token that represents only the photo API. Storage then knows which service is calling but not for whom. It has to trust the photo API to have checked that album 42 is Ava's, and the photo API's token has to be able to read every user's files, because it might be asked for anyone's. A single mistake in the photo API's object checks, the broken object-level 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. described in Enforcing access at an API, now exposes the whole storage service with no second check behind it. Client credentials remain the right choice for calls that really are the photo API's own business, such as reporting how much space it uses. They are the wrong choice for calls about Ava's photos.
Forwarding the incoming token. The photo API could pass the printer's token along unchanged. A storage service that validates tokens properly rejects it, because its audience is the photo API. The tempting fixes, issuing tokens with both audiences or switching off storage's audience check, make every token issued for the photo API valid at storage. The printer, which was never meant to talk to storage, could then call it directly, and any service that receives such a token, including a compromised or careless one, could replay it there. The token's scope is also the wrong description: it says what the printer may do at the photo API, not what the photo API needs from storage.
Naming the user in a header. The photo API could call storage with its own token and add a header such as X-User-Id: user-2048. Storage now has a user to check, but only the caller's word for it. Any service able to call storage could name any user, and a bug that lets an outside request set that header, or pass it through unchanged, would let a stranger name Ava. A downstream service should not accept a user identity that its caller merely asserts.
A token for the next hop
The approach that keeps both the user and the audience straight is to ask the authorization server for a new token. The photo API authenticates as photo-api, presents the printer's token, and asks for a token meant for the storage service. The authorization server checks that the presented token is valid and was issued by itself, that photo-api is allowed to make this kind of request, and that storage is a service it may ask for. It then issues a token that:
- names
https://storage.photos.exampleas its audience, so it works nowhere else; - keeps
user-2048as its subject, so storage can check which files belong to Ava; - carries only the access this call needs, and never more than the original token allowed;
- records that the photo API is acting on Ava's behalf, so storage and its logs can see the chain;
- expires within minutes.
Storage validates it like any access token: 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., audience, and expiry, then whether the files requested belong to user-2048. If a bug in the photo API asked for album 43, which belongs to someone else, storage's own check would refuse, because the token speaks only for Ava. This mechanism is called token exchange, and it is a grant at 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., like the others you have met.
Each exchange is another request to the authorization server, so a service usually keeps an exchanged token for its short lifetime and reuses it for the same user and audience, rather than exchanging before every call.
Tokens at the gateway
As the photo service grows, the photo API becomes one of a dozen internal services, and requests from outside reach none of them directly. They arrive first at an API gateway at api.photos.example, which routes each request to the right internal service and applies rules every service would otherwise repeat, such as rate limits. It is also the natural place to deal with the printer's token once.
The gateway checks the token as any resource serverIn OAuth, the API that holds protected resources and decides, for each request, whether the presented access token allows it. would: issuer, 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. or introspection, expiry, audience, and the scope the route needs. A bad token is refused before any internal service sees the request. Some services go a step further. Clients receive opaque access tokens that reveal nothing and can be revoked at once, and the gateway introspects each one and hands the services behind it a signed JWT carrying the same facts. Because that JWT never leaves the inside, the arrangement is often called the phantom token pattern. A variant, sometimes called the split token, gives the client only the signature of a JWT as its opaque token and keeps the rest at the gateway, which puts the two back together. Either way, the external token is examined once, at the edge, and the gateway can cache the answer within the limits Token introspection described.
What the gateway must not do is pass the printer's token itself to the services behind it. That token was issued for the public API, anyone who copies it from an internal log can replay it from outside, and it describes what the printer may do rather than what an internal call needs, the same problems the forwarding shortcut ran into. The gateway sends an internal token instead, either the JWT it built from introspection or one it obtained by token exchange: addressed to the internal service, short-lived, and carrying the subject, the client, and only the scopes the call needs. When the photo API then calls storage, that is a new hop, and it gets a token of its own in the way the previous section described.
A gateway does not let the services behind it stop checking. Each one still validates the internal token it receives, at least its signature, issuer, audience, and expiry, so a request that reaches it some other way, or arrives from a compromised neighbor, is refused. And each one still decides access to the object in front of it. The gateway can confirm that the token carries photos.read, but only the photo API knows whether album 42 is Ava's.
Credentials for the services themselves
In every approach above, the photo API has to prove who it is, to the authorization server or to storage. Inside a company, the easy way is a long-lived client secretA shared secret that a confidential client uses to prove itself to the authorization server, much like a password for an application. It belongs on a backend, never in code sent to browsers or phones. copied into each service's configuration. It works, and it has every weakness that Secrets and signed assertions described, multiplied by the number of services and copies.
Many hosting platforms offer something better. The platform knows which service it is running, because it started it. It can give the photo API an identity as a service, and each running instance a short-lived signed credential that states that identity, often a JWT that expires within the hour. An identity managed this way for a piece of software is usually called a workload identity. The photo API presents that credential when it authenticates to the authorization server, much like the signed client assertion the printer used, except that the platform signed it rather than the service holding a key of its own. The authorization server accepts it because it has been configured in advance to trust the platform as an issuer for photo-api, the same kind of arrangement Lantern Studio made for its designers. There is no secret to copy, leak, or rotate by hand, because the platform keeps issuing fresh credentials.
Services can also prove their identity to each other directly with mutual TLS, in which both ends of a connection present certificates, so storage knows the caller is the photo API before it reads a single header. Many internal platforms set this up automatically between services. A token can even be bound to the 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. of the service it was issued to, so a copied token is useless without the matching private key.
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
- Call a storage service from the photo API and see what each shortcut losesGive the photo API its own storage token, show that forwarding Ava's token or naming her in a header fails or proves nothing, then map a scope and exchange her token for one storage accepts.