Postman: Automate Generating Amazon Cognito Token

By Kundan Kumar

October 2nd, 2021

image

In this article, we’ll learn how to use Postman pre-request scripts to fetch Cognito tokens and attach bearer tokens to test REST APIs using. The pre-request script is the starting point for the Postman’s request execution. Any script that has been added to the pre-request script is performed first. Let’s see the Postman API request workflow:

  1. Create variables: Let’s begin by creating all of the variables that our pre-request script will require:

cognitoClientId: Set the value of App client id from the App client settings of your user pool.

cognitoClientSecret: Set the value of App client secret from the App client settings of your user pool. This is an optional parameter that you should use if you generated the secret hash for your cognito app client.

cognitoUserName: Set the value of the user’s username(email or phone number) from your user pool.

cognitoUserPassword: Set the value of the user’s password from your user pool.

cognitoAccessToken: After the InitiateAuth success response, use this variable to set the value of the access token.

cognitoIdToken: After the InitiateAuth success response, use this variable to set the value of the id token.

  1. Create the Pre-request Script:

Now go to the collection’s Pre-request Script tab and copy the following script:

var clientId = pm.environment.get("cognitoClientId");

var clientSecret = pm.environment.get("cognitoClientSecret");

var username = pm.environment.get("cognitoUserName");

var password = pm.environment.get("cognitoUserPassword");

pm.sendRequest({

           url: "https://cognito-idp.{your-region}.amazonaws.com/",

           method: 'POST',

           header: {

                    'X-Amz-Target':   'AWSCognitoIdentityProviderService.InitiateAuth',

                    'Content-Type': 'application/x-amz-json-1.1'

                   },

            body: {

                   mode: 'raw',

                   raw: JSON.stringify({

                   "AuthParameters": {

                   "USERNAME": username,

                   "PASSWORD": password

                   },

                  "AuthFlow": "USER_PASSWORD_AUTH",

                  "ClientId": clientId

  }),

options: {

raw: {

language: 'json'

}

}

}

}, function (error, response) {

console.log(response.json());

pm.environment.set("cognitoAccessToken", response.json().AuthenticationResult.AccessToken);

pm.environment.set("cognitoIdToken", response.json().AuthenticationResult.IdToken);

});

Note: Make sure your app client does not contain app-secret or create a new app without secret. Amazon Cognito JavaScript SDK does not support the app client secret.

  1. Update the Authorization Configuration Let’s now update the authorization settings. Switch to the Authorization tab and change the Access Token variable’s value with the {{cognitoAccessToken}} or {{cognitoIdToken}} variable.

References:

  1. https://www.softwaretestinghelp.com/Postman-request-scripts/

  2. https://vmsdurano.com/automating-access-token-generation-with-Postman/



Continue Learning