DevOps.com

Where the world meets DevOps

  • Latest
    • Articles
    • Features
    • Most Read
    • News
    • News Releases
  • Topics
    • AI
    • Continuous Delivery
    • Continuous Testing
    • Cloud
    • Culture
    • DevSecOps
    • Enterprise DevOps
    • Leadership Suite
    • DevOps Practice
    • ROELBOB
    • DevOps Toolbox
    • IT as Code
  • DevOps Chats
  • Webinars
    • Upcoming
    • On-Demand Webinars
  • Library
  • Events
    • Upcoming Events
    • On-Demand Events
  • Sponsored Communities
    • AWS Community Hub
    • CloudBees
    • IT as Code
    • Perforce DevSecOps
    • Rocket on DevOps.com
    • Traceable on DevOps.com
  • Related Sites
    • MediaOps
    • Container Journal
    • Security Boulevard
    • SweetCode.io
    • Digital Anarchist
    • DevOps TV on YouTube
    • DevOps Chat on SoundCloud
    • DevOps Connect
    • DevOps Dozen
  • Media Kit
  • Follow Us
    • Twitter
    • Google News
    • LinkedIn
    • Facebook
    • DevOps TV on YouTube
    • Email
  • AI
  • Cloud
  • Continuous Delivery
  • Continuous Testing
  • DevSecOps
  • Leadership Suite
  • Practices
  • ROELBOB
  • Low-Code/No-Code
  • IT as Code
  • More Topics
    • Application Performance Management/Monitoring
    • Culture
    • Enterprise DevOps

Home » Blogs » How to Revoke JSON Web Tokens (JWTs)

JWTs PASETO

How to Revoke JSON Web Tokens (JWTs)

By: Brian Pontarelli on July 23, 2021 Leave a Comment

One of the most common questions about JSON Web Tokens (JWTs): Once they’re issued, how can they be revoked?

Related Posts
    Related Categories
  • Blogs
  • DevOps Practice
  • DevOps Toolbox
  • DevSecOps
    Related Topics
  • APIs
  • identity and access management
  • JSON
  • web tokens
Show more
Show less

What Are JWTs?

JSON Web Tokens are portable, industry-standard identity tokens. They are issued after a login request by a central identity server and used to identify and credential a user and grant access to resources. They can be presented by clients such as browsers and external programs. Applications and APIs can examine them to ensure the caller is properly authenticated and authorized.

The Challenges Involved

One of the most common questions is how to revoke them. The problem is that once they are issued, no further communication is needed with the identity server. That’s one major benefit of JWTs decentralization.

Let’s say you use RSA public/private key signing for secure data transmission. After the IdP signs a JWT using the private key, any service that has the public key can verify the integrity of the token.

Let’s use the Todo-Backend API as an example. The architecture might look something like this:

Todo API

Now, Todo-Backend can use JWT and the public key to verify the user’s ID and other attributes, and then use that ID to perform operations on that user’s data. Once the JWT has been issued, Todo-Backend doesn’t communicate with the identity provider at all. As a result, if an administrator has locked or deleted that user’s account in the identity provider, Todo-Backend won’t know about it.

Managing Revocations Using a Distributed Event System

The most common way to revoke access to resources protected by a JWT involves setting its duration to a short period of time and revoking the refresh token so that the user can’t generate a new token. This does not revoke the JWT per se; it does solve the root issue, which is to limit access.

In other words, you can set the JWT’s expiration duration to a short period (e.g., anywhere from a few seconds to, say, ten minutes) and set the refresh token’s expiration duration to a longer period (e.g., a two-week or two-month window).

During the normal flow, the Todo API would accept the JWT until it expires, at which time access would be denied. At that time the client could present the refresh token to the identity provider, which would send a new JWT to the client. That new JWT would be valid for another ten minutes.

An administrator can, at the identity provider, revoke the refresh token at any time. Any subsequent request for a new JWT by a client holding that refresh token would fail and access to the Todo API would be lost.

However, while ten minutes isn’t long, this timeframe still comes with risks.

One strategic way to handle this challenge is to set up a distributed event system that notifies services when refresh tokens have been revoked. Upon receiving this event broadcast, backends/services would update a local cache that lists users with revoked refresh tokens. After a JWT is verified, this cache would then be checked to determine whether or not access should be revoked.

This distributed event method truly revokes the JWT, rather than simply waiting for it to expire, as mentioned above.

An Example of Revoking JWTs

Here’s a sample solution that provides support for events and webhooks that can be used to implement this revocation strategy. In this example, when a user logs out or has their refresh token otherwise revoked, a jwt.refresh-token.revoke event is emitted. This includes the user ID and the application for which the revocation occurred. It also includes the JWT lifespan for this application (in the below case, 600 seconds). Any JWT for this user that was issued before 600 seconds from the point in time when this event was received is thus revoked.

For example:

  • User logs in, and JWT ‘abc’ is issued at 1:11pm. JWT is good until 1:21
  • User logs out at 1:15. Entry is added stating that any JWT for this application and user expiring before 1:25 is revoked.
  • Application tries to use JWT ‘abc’ at 1:16. Todo-Backend does not deliver data because this JWT is on the denylist.
  • User logs in, and a new JWT ‘def’ is issued at 1:18. JWT is good until 1:28.
  • Application tries to use JWT ‘def’ at 1:19. Todo-Backend delivers data because this JWT is good until 1:28 and is therefore not on the denylist.

JWT Revoke

What you need to do is write a simple webhook that will receive this event and inform JWTManager that the refresh token for applicationId for this user has been revoked.

JWT webhook

JWTManager maintains a list of user IDs whose JWTs are revoked. You could also use JWT IDs (the `jti` claim), which, in some cases, might be simpler. JWTManager also launches a thread to clean up the cache to remove expired users and avoid running out of memory.

Is valid

For each API call, in addition to all the other checks that should be performed against a JWT (verifying the signature, expiration, issuer and so on), the backend needs to check validity with JWTManager.

configure webhook

As a final step, configure the webhook:

JWT webhook

The Revocation Process, Simplified

Using the process described above, you can revoke a user’s refresh token and broadcast the event using a webhook. The webhook receivers then update JWTManager, which will render JWTs for that user unusable.

The good news is that you can use a webhook and event system to build this feature into your application quickly. You can also write JWTManager implementations into Java, Node and other client libraries for future use. Other languages, such as Ruby, have denylist implementations already written.

Ultimately, all that’s required are the use of refresh tokens, an API that enables revocation of refresh tokens and a system for distributing the revocation event. This solution should work well with most systems, even large systems with numerous backends.

Filed Under: Blogs, DevOps Practice, DevOps Toolbox, DevSecOps Tagged With: APIs, identity and access management, JSON, web tokens

Sponsored Content
Featured eBook
The State of the CI/CD/ARA Market: Convergence

The State of the CI/CD/ARA Market: Convergence

The entire CI/CD/ARA market has been in flux almost since its inception. No sooner did we find a solution to a given problem than a better idea came along. The level of change has been intensified by increasing use, which has driven changes to underlying tools. Changes in infrastructure, such ... Read More
« Higher Mobile App Stability Begets Higher User Ratings

TechStrong TV – Live

Watch latest episodes and shows

Get The Top Stories of the Week

  • View DevOps.com Privacy Policy
  • This field is for validation purposes and should be left unchanged.

Upcoming Webinars

Protecting SUSE Rancher Container-Based Applications With Cloud-Native Backup and Recovery
Tuesday, July 27, 2021 - 11:00 am EDT
4 Open Source Tools That Will Change How You Manage Kubernetes
Tuesday, July 27, 2021 - 3:00 pm EDT
Finance-Level Security on Deployments With Earnin, AWS and Armory
Wednesday, July 28, 2021 - 3:00 pm EDT

Latest from DevOps.com

How to Revoke JSON Web Tokens (JWTs)
July 23, 2021 | Brian Pontarelli
Higher Mobile App Stability Begets Higher User Ratings
July 23, 2021 | Bill Doerrfeld
Programmability is Key to Agile Transformation
July 22, 2021 | Asanka Abeysinghe
Could Full-Stack Observability Help Lessen IT’s Burden?
July 22, 2021 | Bill Doerrfeld
How ESM Enables Digital Transformation
July 21, 2021 | Simon Haighton-Williams

Most Read on DevOps.com

Why IT Should Focus on Quality Over Velocity
July 19, 2021 | Nuria Manuel
3 Reasons Your Digital Transformation Stalled
July 20, 2021 | Amir Rapson
Puppet Survey Suggests DevOps Adoption is Stagnant
July 20, 2021 | Mike Vizard
Cycloid Brings Open DevOps Framework to the US and UK
July 20, 2021 | Mike Vizard
How ESM Enables Digital Transformation
July 21, 2021 | Simon Haighton-Williams

On-Demand Webinars

DevOps.com Webinar ReplaysDevOps.com Webinar Replays
  • Home
  • About DevOps.com
  • Meet our Authors
  • Write for DevOps.com
  • Media Kit
  • Sponsor Info
  • Copyright
  • TOS
  • Privacy Policy

© 2021 ·MediaOps Inc.All rights reserved.

Our website uses cookies. By continuing to browse the website you are agreeing to our use of cookies. For more information on how we use cookies and how you can disable them, please read our Privacy Policy.