Quick revision
The short answer
I would run one identity provider, or a federation behind one, issuing tokens for users, partners and services. Users log in with authorization code and PKCE, with web front ends behind a backend-for-frontend so tokens stay out of the browser. Partners use client credentials with a client, scopes and rate limits each. Services use client credentials or workload identity such as mTLS or platform-issued tokens, with token exchange when user context must flow. Every token is validated at the gateway and again in each service, with an audience per service, never trusting the network alone.
What to remember
In this answer, we drew trust boundaries where the internal network is not trusted, used one identity provider for everyone, gave users the code flow with PKCE and a BFF for web, gave each partner its own client credentials with scopes and rate limits, gave services workload identity with token exchange when user context flows, and validated tokens at the gateway and again in every service with a per-service audience. For identity three calls deep, token exchange at each hop gives each service a token only for the next one.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would run one identity provider, or a federation behind one, issuing tokens for users, partners and services. Users log in with authorization code and PKCE, with web front ends behind a backend-for-frontend so tokens stay out of the browser. Partners use client credentials with a client, scopes and rate limits each. Services use client credentials or workload identity such as mTLS or platform-issued tokens, with token exchange when user context must flow. Every token is validated at the gateway and again in each service, with an audience per service, never trusting the network alone.
Background to this question
This is a classic architecture question for senior engineers because every choice ripples across the company: which flows teams must implement, what a token means, and who may call whom. A weak design either leaks tokens, lets one compromised service impersonate users everywhere, or makes every integration a special case. In this answer, we will set the trust boundaries, design the flows for users, partners and services, look at token types and lifetimes, place validation at the gateway and in services, and answer the follow-up about passing user identity three calls deep.
1Trust Boundaries First
Before choosing flows, we decide where trust changes. There are four boundaries here: the public internet, where browsers and mobile apps live; partners, who are known organisations but outside our control; the edge, where an API gateway receives traffic; and the internal network, where 60 microservices call each other. The key decision is that the internal network is not a trust boundary in itself. Being inside the cluster proves nothing; every call carries a token or a workload identity that the receiver checks. This is the zero trust idea, and it limits what an attacker can do after compromising one service.
- Clientsweb via BFF, mobile apps, partner systems
- Identity providerOIDC for users, client credentials, token exchange
- API gatewayvalidates tokens, rate limits per partner
- Microservicesvalidate again, audience per service, mTLS between them
- Data storesservice credentials only, never user tokens
5 layers, from the top down. Step through them one at a time.
Each layer checks its caller; none relies on its network position.
2Users: Code Flow, PKCE and a BFF
All user logins go through one identity provider using the authorization code flow with PKCE, so password policy, MFA, passkeys and social or enterprise federation are implemented once. The web app gets a backend-for-frontend: a server-side component that runs the code flow as a confidential client, keeps the tokens and gives the browser an HttpOnly, SameSite session cookie. Mobile apps are public clients and use the code flow with PKCE through the system browser, storing refresh tokens in the platform's secure storage.
Access tokens are short-lived, around 5 to 15 minutes, and refresh tokens rotate on each use, so a leaked token expires quickly and a reused refresh token reveals theft.
3Partners: One Client Each
Partners integrate system to system, so they use the client credentials grant. Each partner gets its own client registration, never a shared one, with only the scopes its contract allows, such as catalog.read or orders.create, and authenticates with a private key JWT or mutual TLS rather than a shared secret where possible. The gateway applies rate limits and quotas per client id, and every request is logged with that id.
Per-partner clients make the daily operations simple: we can rotate one partner's keys, cut off one partner during an incident, and answer exactly what one partner accessed, without affecting the others.
4Services: Workload Identity and Token Exchange
Service-to-service calls need two kinds of identity. The calling service's own identity is established with workload identity: mTLS certificates issued by a service mesh or SPIFFE-style infrastructure, or platform-issued tokens such as Kubernetes service account tokens federated with the identity provider, or client credentials for services outside the platform. The receiving service checks which service called it and whether that service may use this endpoint.
When the operation is done for a user, such as reading their orders, the receiving service also needs the user's identity. We pass it with a token, and the safest way is token exchange (RFC 8693): the calling service presents the user's token to the identity provider and receives a new token for the next service's audience, with narrower scopes.
Notice the act claim: the new token says the user is user-123 and the actor is orders-service, so payments can authorise and audit both. Spring Security has client support for token exchange in recent versions, and Spring Authorization Server can act as the exchange endpoint; check your version.
5Validate at the Gateway and in Every Service
The gateway validates every incoming token: signature, issuer, expiry and that the audience belongs to our APIs. This stops garbage early and centralises rate limiting. But each service validates again, as an OAuth2 resource server with its own audience, for example orders-api. If the gateway is bypassed, misconfigured or one service is compromised and calls another directly, the receiving service still refuses tokens that were not meant for it.
Audience per service is what makes a stolen token less valuable: a token for catalog-api cannot be replayed against payments-api. Validation of JWTs is local, using the identity provider's cached public keys, so doing it twice costs microseconds.
6Follow-Up: User Identity Three Calls Deep
Suppose the web BFF calls orders, orders calls payments, and payments calls the ledger service, all for one user. There are three common options. Passing the original token through every hop is simplest, but it needs a token whose audience includes all services, so any one of them can replay it anywhere, which is the problem audiences exist to prevent. Having the gateway mint an internal signed token with user context is a reasonable middle ground in a closed platform. Token exchange at each hop is the strongest: each service exchanges the token it received for one aimed at the next service, scopes narrow along the way, and the act claim records the chain.
7 messages in order. Play them, or step through them one at a time.
Each hop holds a token valid only for the next service.
The cost is an extra call to the identity provider per hop, which we reduce by caching exchanged tokens for their short lifetime. For deep, high-volume internal chains, some teams exchange once at the edge and pass a narrowly scoped internal token, accepting slightly less isolation for lower latency. Whatever we choose, we write it down as the platform standard so 60 services do not invent 60 variations.
7Follow-up questions
Should the gateway strip external tokens and add its own?
It can, and in some designs the gateway exchanges the external token for an internal one. That hides external token formats from services and lets internal tokens carry internal claims. The services must still validate the internal token, so this is token exchange at the edge, not trust in the network.
Why not use API keys for partners?
API keys are long-lived shared secrets without scopes, expiry or a standard way to rotate. Client credentials give short-lived tokens with scopes, and with private key JWT authentication the partner never sends a secret at all.
Where do we keep the authorization rules themselves?
Tokens carry identity and coarse scopes. Detailed rules, such as which orders a user may see, stay in each service or a policy engine, because they depend on data the identity provider does not have.
8Conclusion
In this answer, we drew trust boundaries where the internal network is not trusted, used one identity provider for everyone, gave users the code flow with PKCE and a BFF for web, gave each partner its own client credentials with scopes and rate limits, gave services workload identity with token exchange when user context flows, and validated tokens at the gateway and again in every service with a per-service audience. For identity three calls deep, token exchange at each hop gives each service a token only for the next one.
Open with
I would start with one identity provider, or a federation presented as one, so MFA, federation and token formats are defined once. Users log in with the authorization code flow and PKCE; the web app uses a backend-for-frontend that keeps tokens on the server and gives the browser an HttpOnly cookie, and mobile apps use PKCE through the system browser, with short-lived access tokens and rotating refresh tokens.
Then, if they want more
Partners use client credentials, one client per partner with only its contracted scopes, ideally private key JWT or mTLS instead of secrets, and per-client rate limits at the gateway.
Services prove their own identity with workload identity, like mesh mTLS or platform tokens federated with the identity provider, and when a call is made for a user, the user context travels in a token obtained through token exchange.
Tokens are validated at the gateway and again in each service as a resource server with its own audience, because the internal network is not a trust boundary.
For a service three calls deep, I would use token exchange at each hop: each service exchanges the token it received for one whose audience is the next service, with narrower scopes and an act claim showing who acts for the user, and cache those tokens for their short life.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- RFC 6749, The OAuth 2.0 Authorization Framework, Spring Security reference, OAuth2
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design the identity architecture for a company with a web app, mobile apps, partner APIs...": https://scrivio-interview-library.netlify.app/spring-boot/oauth2-and-oidc/44ef6e48fd/