Need help?

Our experts have had an average response time of 13.14 minutes in February 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

Revoke JWT tokens in Amazon Cognito with ease

by | Nov 26, 2021

Revoke JWT tokens in Amazon Cognito via AWS CLI with Bobcares by your side.

At Bobcares, we offer solutions for every query, big and small, as a part of our Server Management Service.

Let’s take a look at how our Support Team recently helped a customer when they wanted to revoke JWT tokens in Amazon Cognito using the AWS CLI.

All about revoking JWT tokens in Amazon Cognito

Amazon Cognito refresh tokens expire thirty days after a user signs in to the user pool. However, we can set the app client refresh token expiration to last between 60 minutes to ten years.

Another way around this issue is to revoke refresh tokens in real-time, thereby preventing refresh tokens from generating additional access tokens.

How to revoke JWT tokens in Amazon Cognito

According to our Support Team, first, we have to revoke the JWT token based on the app client. Here are a couple of things to remember before we begin:

  • If you come across errors while running AWS CLI, verify whether you are using the most recent AWS CLI version.
  • Amazon Cognito user pool app clients can use the optional secret feature on the app.
  • We have to replace us-east-1 with the AWS Region. We also need to replace client-id, user-pool-id, username, tokens, email, secret, and password with the required variables.

App client without a secret

First, we will run admin-initiate-auth, an AWS CLI command to initiate the authentication flow as an administrator. This enables us to get the access token, ID, and refresh token.

$ aws  --region us-east-1 cognito-idp admin-initiate-auth --user-pool-id us-east-1_123456789 --client-id your-client-id --auth-parameters USERNAME=user-name,PASSWORD=your-password --auth-flow ADMIN_NO_SRP_AUTH

As a result, we will get an output similar to this:

{
    "ChallengeParameters": {},
    "AuthenticationResult": {
        "AccessToken": "eyJra....",
        "ExpiresIn": 3600,
        "TokenType": "Bearer",
        "RefreshToken": "ey.._9Dg",
        "IdToken": "ey..DU-Q"
    }
}

After that, we will run the revoke-token command to revoke the refresh token as seen below:

$ aws --region us-east-1 cognito-idp revoke-token --client-id your-client-id --token eyJra....

In case we do not get an output after running the command, we can test it with the same refresh token in order to get a fresh access token and ID:

$ aws --region us-east-1 cognito-idp admin-initiate-auth --user-pool-id us-east-1_123456789 --client-id your-client-id --auth-parameters REFRESH_TOKEN=eyJra....tw --auth-flow REFRESH_TOKEN_AUTH

Then, we will get an output stating that the refresh tokens are revoked as seen below:

Error: An error occurred (NotAuthorizedException) when calling the AdminInitiateAuth operation: Refresh Token has been revoked

App client with a secret

In this scenario, we will create a SecretHash value via a Python script.

Then, we will run the admin-initiate-auth command to initiate the authentication flow as an administrator. This enables us to get the access token, ID, and refresh token.

$ aws --region us-east-1 cognito-idp admin-initiate-auth --user-pool-id us-east-1_123456789 --client-id your-client-id --auth-parameters USERNAME=user-name,PASSWORD=your-password,SECRET_HASH=IkVyH...= --auth-flow ADMIN_NO_SRP_AUTH

This results in the following output:

{
    "ChallengeParameters": {},
    "AuthenticationResult": {
        "AccessToken": "eyJra....",
        "ExpiresIn": 3600,
        "TokenType": "Bearer",
        "RefreshToken": "eyJjd....",
        "IdToken": "ey..YQSA"
    }
}

Then, we have to run the revoke-token in order to revoke the refresh token:

$ aws --region us-east-1 cognito-idp revoke-token --client-id your-client-id --token eyJjd... --client-secret 1n00....

After that, we will test with the same refresh token:

$ aws --region us-east-1 cognito-idp admin-initiate-auth --user-pool-id us-east-1_123456789 --client-id your-client-id --auth-parameters REFRESH_TOKEN=eyJjdH.... --auth-flow REFRESH_TOKEN_AUTH

As a result, we will get an output stating that the refresh tokens are revoked:

Error: An error occurred (NotAuthorizedException) when calling the AdminInitiateAuth operation: Refresh Token has been revoked

Newly added claims

Additionally, two new claims, namely, origin_jti and jti are added in the ID and access token, thereby increasing the size of tokens in the app client.

The jti offers a unique identifier for the JWT. Moreover, an identifier value has to be assigned to ensure the same value is not assigned to another data object. In other words, this prevents collisions if the app client uses multiple issuers.

[Need an additional hand? We are available 24/7.]

Conclusion

To sum up, the skilled Support Engineers at Bobcares demonstrated how to revoke JWT tokens in Amazon Cognito using the AWS CLI.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF