41

I was using Amazon Cognito user pool for login. When I access my web application, I get a redirect to

https://<domain>.auth.<region>.amazoncognito.com/login?response_type=code&client_id=<client id>&redirect_uri=<callback> . 

Once logged in with the username/password of a user from the pool, I will be redirected to the callback URL with the code as a query parameter. I can use this to get tokens. How do I integrate this in postman so that I can use the token for my upcoming request?

peterh
  • 11,875
  • 18
  • 85
  • 108
Shamseer PC
  • 787
  • 2
  • 9
  • 20

8 Answers8

48

I have an example of doing this...enter image description here

  1. The callback URL as defined in the Cognito User Pool console under App Integration / App client settings.
  2. The URL for the login endpoint of your domain. This will be under Cognito User Pool / App Integration / Domain Name
  3. Client ID is found under Cognito User Pool / General Settings / App clients
  4. List the scopes you want to include in the Access Token. These must be enabled under Cognito User Pool / App Integration / App client settings. These can be either standard or custom scopes. Custom scopes are defined under App Integration / Resource servers and must include the resource server ID (e.g. https://myresource.com/myscope)
  5. Click Request Token enter image description here

You may now log in to your Cognito User Pool and receive an Access Token! The problem is that once you have the Access Token it isn't usable within Postman because Cognito expects it to be bare and Postman automatically prepends 'Bearer' to the token:enter image description here The token can be used in cURL though: curl -i -H "Authorization: dyJraWQiOiI1YVcwTUlqN1hBaHg4Yzh4Q3JNT2RsQjhZWjlCR3NQOE9BbkFlVFJtUklRPSIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiI3YmEwZmMzOC01ZDcwkYS05MTI5ZTBmYTUzNTEiLCJ0b2tlbl91c2UiOiJhY2Nlc3MiLCJzY29wZSI6Imh0dHBzOlwvXC9hcGkubXk5MC5jb21cL3BvbGljZURlcGFydG1lbnRzLnJlYWQiLCJhdXRoX3RpbWUiOjE1NDA1OTIzMTYsImlzcyI6Imh0dHBzOlwvXC9jb2duaXRvLWlkcC51cy1lYXN0LTEuYW1hem9uYXdzLmNvbVwvdXMtZWFzdC0xX2xIbGo4NXpRYSIsImV4cCI6MTU0MDU5NTkxNiwiaWF0IjoxNTQwNTkyMzE2LCJ2ZXJzaW9uIjoyLCJqdGkiOiJhN2JiOWU2MC1kNmY1LTQ3ODYtODMwYi0xODdkZDZmYTZlODAiLCJjbGllbnRfaWQiOiI2MzhlYmZ1dTdiZDRkMXVkYnRzY2pxcnJncyIsInVzZXJuYW1lIjoicm9qbyJ9.O_GAxfFX3IQfLUu5Hxr05Wrk_2QDwNSL8tvDdEU0Dzs9d1XhQPafT6ney6yiGnKPOwsO8HhWdbT1QdDmByjuwQAURf1Da4Au7c-yhfgJcqWuHWZ4mledTSP8ukXqihMb4PoaDdU4JXyOdMLa50dBXVMgJNyXTpIulWOxFhiTW6DeQbnxNDk94cGNz_CTKCEqKStiloFZfLR7ndSrWqdOQ_SU__YV0RyKXZyK5yguv3nkUcI6cuKpbPVIZ5DNdpufbrtOLuZcC6HePBKrbTKjSZCt5-swy3YrwnY4ApTX7QUFzof6FylWaLA_KVP3Zv6ksSJ_IjBMFH1NRVHh4lbsOA" \ https://xxxxx.execute-api.us-east-1.amazonaws.com/v1/myresource/1234

Robert Jordan
  • 1,053
  • 1
  • 10
  • 17
  • 1
    Your comment about not prepending "Bearer" to the token helped, but I wonder; Where is that documented? – Jon Lorusso Dec 05 '18 at 22:08
  • 1
    From https://forums.aws.amazon.com/thread.jspa?threadID=241945 and also my own testing; it looks like they fixed the issue with "Bearer" and it can be in the header now. So, using the OAuth2.0 in Postman should "just work". – justderb Feb 21 '19 at 00:14
  • 10
    Is there a way to automate the login process? I can login manually but that's not a viable solution since I want my tests in Postman fully automated. – escape_artist Feb 27 '19 at 11:56
  • 6
    If you can't find the `GET NEW ACCESS TOKEN` dialog, it is under tab `Authorization` and you need to change `TYPE` to `OAuth 2.0`. – Big Pumpkin Aug 10 '19 at 19:07
  • I filled out the values as described but seeing `An error was encountered with the requested page` instead of the signin prompt. The only differences I have is callback URL has a path added (to match my client and Cognito settings) and scope is blank (because I'm not sure which scope I need and Cognito docs claim to default to all if unset). – Alexander Tsepkov Mar 25 '20 at 16:45
  • 3
    Thanks. The problemi I found is that the token is made up of two field: Access Token and ID Token. Postman uses the Access Token, but the correct one to use is instead the ID Token – Marcello Romani Apr 22 '20 at 16:18
  • 1
    Postman now allows setting the "Header Prefix" (which defaults to "Bearer"). Clearing this field allows you to use the auth inheritance of Postman. – morgler Apr 04 '21 at 15:41
  • 1
    Thanks for the great answer. I am left with the same problem as @MarcelloRomani that Postman is using the Access Token when I need to use the id token. Has anyone found a resolution for this? – Josh Nov 12 '21 at 04:20
  • same here, cognito return both "access token" and "id token", but we need to use "id token" for authorization. is there a way to configure postman to use "id token" instead of "access token" when we press the "use token" button? it would eliminate the need of the copy/paste... – Rod Jan 06 '23 at 09:24
30

by yl.

Thanks to Robert Jordan for his above postman OAuth2.0 configuration post.

I'll try to cover here the entire Cognito user pool definition part to make it easier.

Ok,

Open the Cognito console and follow the bellow stages:

1) create new user pool

name: Test1
left panel menu->Attributes

Select the following radio buttons:

o Email address or phone number - Users can use an email address or phone number as
  their "username" to sign up and sign in.
   o Allow email addresses   

And checkboxes:

[v] email
[v] name

Screenshot:

enter image description here

Press the [Create Pool] button. (if not available yet to the wizard - press [Review Details] option on the left panel menu)

2) left panel menu->App Clients

press: [add app client]

App client name: me1

clear all checkboxes but the:

[v] Enable username password based authentication (ALLOW_USER_PASSWORD_AUTH)

Leave Radio buttons as is:

o Enabled (Recommended)

Screenshot:

enter image description here

press [create app client]

3) copy and keep the 'App client id'

this is a string format similar to 5psjts111111117jclis0mu28q

Screenshot:

enter image description here

4) left panel menu->App Client settings

Enabled Identity Providers: [v]Select all
[v] Cognito User Pool

Callback URL(s): put the api gw url or https://www.google.com/

OAuth 2.0
 Allowed OAuth Flows
   [v] Implicit grant
 Allowed OAuth Scopes
   [v] openid

Screenshot:

enter image description here

5) left panel menu->Domain name

put a string in the prefix field, for instance: music123456789

check if available using the 'check' button.

your domain now is: https://music123456789.auth.us-east-1.amazoncognito.com

Screenshot:

enter image description here

6) left panel menu->Users and Groups

press [Create user]

Username (Required): Your.Mail@company.com

clear all [v] check boxes

Temporary password: Xx123456!

eMail: Your.Mail@company.com

7) in POSTMAN

Press new Request

enter the 'Authorization' tab

Select TYPE: OAuth 2.0

press the [Get new Access Token] button and fill in:

Token Name: myToken123

Grant Type: select 'implicit' from the listbox

  1. callback URL: https://www.google.com/
    (as in clause 4 or in cognito console->App Integration->App client settings)

  2. Auth URL: https://music123456789.auth.us-east-1.amazoncognito.com/login
    (as in clause 5 + '/login' suffix, what you have defined in cognito console->App Integration->Domain Name)

  3. Client ID: 5psjts343gm7gm7jclis0mu28q (the app client id - as in 3,

what you have defined in cognito console->General Settings->App clients)

  1. Scope: openid (as in 4, what you have defined in cognito console->App client settings->Allowed OAuth Scopes)

COGNITO to OKTA idp configuration

When connecting Cognito to Okta IDP, Configuration should be as follows:

Okta Setup enter image description here

Cognito Setup enter image description here

Postman setup enter image description here

ylev
  • 2,313
  • 1
  • 23
  • 16
  • 1
    the username & password on postman, where is it set ? – Tiago Medici Nov 17 '21 at 21:45
  • I had to also do the following: - Set 'Add Authorization data to' be 'Request Headers' - Make sure that the Grant Type was set in my App settings - Once this configuration was set, then you can press the 'Get New Access Token', which will take you to the login page. Once you enter the credentials, you will be routed back to Postman to accept/save the token. - Then you can 'Send' the API request I found the Postman Console (under the View menu) to very useful in figuring out where I was going wrong. – HiDefLoLife Jan 02 '22 at 06:58
8

For those wanting to move away from the deprecated "implicit" grant to the recommended "authorization" aka "authorization code" grant, you'll want to have the following in Postman:

Grant Type: Authorization Code (Authorization Code with PKCE would prevent the code from being used by anyone else if it were intercepted in transit but either or... you probably want to start with getting "Authorization Code" working.)

Callback URL: https://oauth.pstmn.io/v1/callback (or whatever Postman sets it to when you check "Authorize using browser"

Auth URL: https://{app name you chose when creating the custom auth domain}.auth.{aws region}.amazoncognito.com/login e.g. https://myapp.auth.us-east-1.amazoncognito.com/login. You can find this in AWS Console -> Cognito -> the user pool -> App Integration tab -> Domain section -> Cognito domain (use the Actions dropdown to create a custom domain if you don't already have one).

Access Token URL: https://{app name}.auth.{aws region}.amazoncognito.com/oauth2/token e.g. https://myapp.auth.us-east-1.amazoncognito.com/oauth2/token.

Client ID: The Client ID corresponding to the "App Client" (e.g. the web app users will be authenticating through Cognito to use), found in AWS Console -> Cognito -> the user pool -> App Integration tab -> App Client List section -> the App Client.

Client Secret: An optional added security measure. This should never be sent to the web app as the client secret could then be extracted by a nefarious user via Chrome Dev Tools or the like. You can (and should) however use Client Secret with backend applications e.g. the API service backing your frontend web app. This is a decision that has to be made in AWS when the App Client is created within the Cognito User Pool, but don't fret- App Clients are easy to create/delete/recreate if you change your mind or pick the wrong setting.

Scope: OAuth uses "scopes" as a means of defining what the application which holds and uses the access token (e.g. some web app) can do/access on behalf of the user whose account it's using. It's similar to authorization in a web app (e.g. only users in the "admin" group can access the settings page) but it's meant to be authorization with respect to a user's metadata, so typically that manifests as user metadata the app has access to, for example the user's calendar or contacts or phone number. OAuth scope is not meant to replace an app's authorization system (e.g. RBAC) so if you're just making some web app and just need Cognito to handle user signup, storing and resetting passwords for users, etc. you can more or less ignore OAuth "scope", though you should probably be setting the "Scope" value in Postman to something like "openid email" (Scopes are separated by a single space and you can't request and obtain the "email" scope without also requesting "openid") so you at least get the user's email address in the access token to compare with your "Users" table in your app's database. Scope makes more sense and becomes more relevant in a scenario where your web app is authenticating with an actual third-party (not your own Cognito user pool), like Facebook or Google. Maybe you're making an app that syncs users' friends' contact data from Facebook to.. I don't know, a CSV file downloaded to your computer for backup purposes. In this case you want to request from Facebook's OAuth server the "friends-list" scope or whatever Facebook decided to call that scope. On the other hand, if you just need Facebook as a means for allowing your users to easily sign in to your app without having to create an account, you don't need the "friends-list" or any other scope from Facbeook (maybe just the scope that gives you the user's email address?).

Other fields:

  • Client Authentication: Send client credentials in body
  • Type: OAuth 2.0
  • Add authorization data to: Request Headers

Sources:

https://www.czetsuyatech.com/2021/01/aws-generate-cognito-access-token.html
https://aws.amazon.com/blogs/mobile/understanding-amazon-cognito-user-pool-oauth-2-0-grants/
https://api.slack.com/legacy/oauth-scopes

jspinella
  • 2,013
  • 3
  • 25
  • 39
6

As an addition to very through explanations of Robert Jordan and ylev, I made it work by using the id_token instead of the Access Token.

enter image description here

In the token details page, copy the id_token and add it to the header manually without Bearer prefix: enter image description here

Source: https://github.com/postmanlabs/postman-app-support/issues/6987

Kerem
  • 1,494
  • 2
  • 16
  • 27
5

If your client supports USER_PASSWORD_AUTH you can request valid bearer tokens using the aws client.

read -s -p "Password: " && \
    aws cognito-idp initiate-auth \
        --client-id <client id> \
        --auth-flow USER_PASSWORD_AUTH \
        --auth-parameters "USERNAME=<username>,PASSWORD=$REPLY"

This can be added to Postman under Authorization / Type: Bearer Token.

Danielle Madeley
  • 2,616
  • 1
  • 19
  • 26
3

I thought i would post some more information about using cognito with an elastic load balancer. AWS load balancers do not current support auth via headers :( you can get it working on postman by copying cookies from a successfull web request into the postman request

izzyp
  • 163
  • 1
  • 7
2

For Postman 8.5.1 and AWS Chalice + Cognito user pool on the backend I have working example:

Collection folder settings, Authorization tab

Cognito > User pools > > App integration > App client settings

App settings configuration

About vars:

  1. {{cognito_callback_url}} - Your Callback URL(s) from App client settings
  2. {{cognito_auth_url}} - Cognito > User pools > > App integration > Domain name + /login (https://.....auth.ap-south-1.amazoncognito.com/login)
  3. {{cognito_client_id}} - Your App client web id from App client settings
  4. {{cognito_scope}} - Use 'openid'

Now click the Get the new access token in the bottom and authorize yourself using existing user data from pool

1

The use-case you want to implement can be achieved by using the OAuth 2.0 authorization. If you can get the Auth URL/ Access Token URL, Client ID, and the Client Secret- you should be able to do it.

Here's a link to the documentation of the various authorization types we support including the above mentioned one- https://www.getpostman.com/docs/v6/postman/sending_api_requests/authorization.

Cheers.

Harsha Jayamanna
  • 2,148
  • 6
  • 25
  • 43
Harsha
  • 27
  • 4