Bobcares

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 *

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