VOOZH about

URL: https://thenewstack.io/jwts-on-a-journey-sending-jwt-access-tokens-across-apis/

⇱ JWTs on a Journey — Sending JWT Access Tokens across APIs - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-07-17 07:12:12
JWTs on a Journey — Sending JWT Access Tokens across APIs
sponsor-curity,sponsored-post-contributed,
API Management / Security

JWTs on a Journey — Sending JWT Access Tokens across APIs

Both the optimum lifetime of a token and the required data must be thoroughly considered when designing JSON Web Tokens.
Jul 17th, 2023 7:12am by Judith Kahrer
👁 Featued image for: JWTs on a Journey — Sending JWT Access Tokens across APIs
Image from stocksolutions on Shutterstock
Curity sponsored this post.

Access controls are essential for securing APIs. OAuth enables token-based authorization, where access controls demand access tokens that fulfill certain requirements. Such a token, commonly in the form of a JSON Web Token (JWT), serves as the entry ticket to APIs. When entering the system, there are access controls at various places, like in the API gateway and in each microservice. This paradigm is called zero trust. When applying zero trust and token-based authorization, JWTs inevitably travel through the system, from one endpoint to another.

👁 Image

JWTs are validated and verified constantly while they follow a flow. Validation means checking that the token is valid — that the signature is trusted and that the token has not yet expired. Verification implies that the token fulfills the requirements for passing access controls, like the expected issuer, audience, scope and claims values. Consequently, there are two challenges with JWTs on a journey that this article addresses:

  • Setting the lifetime of the token.
  • Providing adequate data in tokens.
Curity is a leading IAM and API security technology provider that enables user authentication and authorization for digital services. The Curity Identity Server is highly scalable, handles the complexities of the leading identity standards, making them easier to use, customize and deploy.
Learn More
The latest from Curity

Token Lifetime

Security best practices recommend keeping the lifetime of JWTs short, preferably just a few minutes. However, users would likely be frustrated if they had to log in all the time to renew an access token. Therefore, OAuth provides a flow where client applications can use refresh tokens for access without having to interact with the user. The refresh token is for the client, though, and not for the API. Consequently, APIs cannot refresh an access token but eventually return an error when they receive an expired token.

Whenever APIs return an authorization error (HTTP 401) because of an expired or otherwise invalid token, the client should retry the request with a new access token. However, requests may be asynchronous. A JWT may have already moved to upstream APIs before it expires, and the API may not be able to return an error to the client as part of the current request. In that case, there must be a callback or notification mechanism that allows an upstream API to inform any downstream API, and eventually the client, about an expired token so the client can restart the flow. To minimize such callbacks, clients can refresh access tokens before they expire so that JWTs have enough lifetime left before they are sent out.

Minimization for Privacy

Security is about minimization. Not only should the token lifetime be the minimum acceptable value, but the JWT should also only contain the absolute necessary data required by the access control. To stretch it further, even though access tokens are issued to clients, ideally clients should not have access to any data associated with the access token. Therefore, the JWTs journey should not pass clients, but opaque access tokens should.

If clients receive opaque access tokens, the JWT’s journey will start at the API gateway. Opaque access tokens are not only opaque to clients but also APIs. Consequently, APIs need to look up the data associated with such a token. This flow is called introspection. Since introspection affects performance, especially if required for every microservice, it is better to have the API gateway translate the opaque token to a JWT and cache the result. This approach is called the Phantom Token Pattern. In this way, APIs in the backend still handle JWTs.

👁 Image

Token Sharing

As mentioned earlier, access controls are spread out as they are enforced by APIs. Thus, APIs require requests to include an access token, the JWT, on which they base decisions. Token-based authorization allows for transporting user data in a verifiable and auditable manner between APIs by putting it into JWTs.

Token Forwarding

The easiest way to share JWTs between APIs and microservices is to simply forward them to upstream APIs. However, with hundreds of different APIs, and potentially hundreds of different access controls, it is infeasible to feed a JWT with all potentially required data. It is infeasible because it may simply not be known beforehand which path the JWT will travel and what data to add. Even if it is known, adding all data and access rights to cover all possible flows violates the principle of least privilege. For example, in this case an access token needs to include all valid scope values so that it is accepted by any API, despite a single API only requiring a subset of scopes. The goal should be to keep the data at a minimum to cover the most common cases.

👁 Image

Token Embedding

One approach to narrow down the data in JWTs when forwarding tokens is to embed one token in another — one of the claims in the JWT contains another JWT. Instead of having one and the same JWT travel through the system, an API (or most likely the API gateway) can forward an embedded, narrower JWT to upstream APIs. Consequently, the upstream API receives tailored tokens, which satisfies the principle of least privilege. The downside of the embedded token approach is that it not only increases the size of the outer token, but all data still needs to be known when JWTs are issued. This may not always be the case. For example, a JWT will not embed any tokens for new APIs if they were not known when the JWT was created. In this case, new tokens are first embedded when the JWT has expired and gets renewed.

👁 Image

Token Exchange

The most flexible approach for token sharing is to exchange tokens. The token exchange approach is similar to the refresh token flow where one token is exchanged for another. While the refresh token flow is designated for the client and limited to refreshing an (existing) access token with the help of the refresh token, token exchange can be used for a range of tokens and other use cases. For example, it allows for exchanging an access token for a new one with a different scope and claims than the original token. Token exchange is not only useful when sharing tokens for the same user, but also for implementing delegation and impersonation use cases where the subject of an access token represents a completely different user.

Consequently, token exchange enables a highly customizable token-sharing mechanism where an API or API gateway can, on demand, request a new JWT for upstream APIs that follows the principle of least privilege.

👁 Image

Conclusion

Once JWTs are sent off, they cannot be changed. Therefore, both token lifetime and token data must be thoroughly considered when designing JWTs. JWTs should contain adequate but minimal data for the challenges they meet on their way (aka access controls). Therefore:

  • Keep JWT lifetimes short and handle expiry.
  • Keep JWT access tokens private and do not expose them to clients.
  • Use token-sharing techniques to meet security requirements between APIs.

There are various token-sharing approaches that help to fulfill security requirements. A combination of token forwarding and token exchange is probably sufficient even for complex cloud native applications to enable a safe journey for JWTs.

Curity is a leading IAM and API security technology provider that enables user authentication and authorization for digital services. The Curity Identity Server is highly scalable, handles the complexities of the leading identity standards, making them easier to use, customize and deploy.
Learn More
The latest from Curity
TRENDING STORIES
Judith is a Product Marketing Engineer, with a keen interest in security and identity. She started her working life as a developer and moved onto being a security engineer and consultant before joining the Curity team.
Read more from Judith Kahrer
Curity sponsored this post.
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Pragma.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.