Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

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 token. Whether the API decodes it as a JWT or receives the same fields from introspection, 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 server as the confidential client 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 authorization 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.example as its audience, so it works nowhere else;
  • keeps user-2048 as 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: issuer, 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 endpoint, like the others you have met.

Instead of forwarding the printer's token, the photo API exchanges it for one addressed to storage. Storage can still check that the files belong to Ava.

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 server would: issuer, signature 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 secret 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 certificate 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.

To read the files for album 42, the photo API calls storage with a client credentials token that represents only the photo API. What does storage lose? 1 of 3

Choose one answer.

The photo API calls storage with its own client credentials token and adds X-User-Id: user-2048. What is wrong with this? 2 of 3

Choose one answer.

The API gateway already checks every incoming token's issuer, signature, audience, expiry and scope. A developer proposes that the photo API stop validating the internal tokens it receives. Why is that a mistake? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity