Skip to content

API Gateway with Cognito User Pool: Unauthorised

0

Hi, I have a Cognito user pool and an app client. I have created a user that generated access_token and id_token I created an API Gateway resource and added an Authoriser using my Cognito Pool. The idea is that I will not use an IAM role but use the Cognito user pool email + password to invoke an API. Now, when I test Test authorizer with id_token it works but if I test it with Postman it does not.

I looked for a lot of documentation but there is also no step by step guidance. In Postman, I add Authorization as Header and put my access_token. I tried several variations with Authentication type Bearer. None of them worked. I created custom scopes, none of them worked. Can you help me?

my invoke URL: https://xxxxx.execute-api.eu-west-1.amazonaws.com/test I want to do a POST request for https://xxxx.execute-api.eu-west-1.amazonaws.com/test/mock/StartJob I added authorization scope https://xxxx.execute-api.eu-west-1.amazonaws.com/test/auth I created this resource /auth I also created a custom scope in my Cognito domain, scope auth, with resource server identifier both https://xxxxx.execute-api.eu-west-1.amazonaws.com/test and https://xxxxx.execute-api.eu-west-1.amazonaws.com with scope auth. I also removed it and vice versa tested all combinations Sometimes it was unathorised Sometimes it was Message": "User: anonymous is not authorized to perform: execute-api:Invoke on resource: ONLY IF I USED ID_TOKEN I just cannot find any documentation that explains what is the issue.

  1. What custom scopes do I need, if thats the issue? I just cant seem to get it right
  2. Can I not use id_token to invoke API
  3. Is it a policy issue? I also cannot get this right.

Thank you in advance.

1 Answer
1

Based on the information provided, it seems you're encountering some common issues when integrating Amazon Cognito with API Gateway. Let's address your questions and provide some guidance:

  1. Custom scopes: Custom scopes are typically used with access tokens, not ID tokens. If you're using an access token, you need to ensure that the custom scopes you've defined in your Cognito user pool match the scopes you've configured on your API Gateway methods. However, it's not always necessary to use custom scopes if you're just looking for basic authentication.

  2. Using ID token vs Access token: You can use either an ID token or an access token to invoke an API, but they serve different purposes:

  • ID token: Used for authentication and contains claims about the user's identity.
  • Access token: Used for authorization and contains scopes that define what actions the user can perform.

For simple authentication scenarios, using the ID token is often sufficient. If you're using the ID token, you don't need to configure custom scopes.

  1. Policy issues: If you're seeing the "User: anonymous is not authorized to perform: execute-api:Invoke" error, it suggests that the token is not being properly recognized or validated by API Gateway. This could be due to incorrect configuration of the Cognito User Pools authorizer in API Gateway.

To troubleshoot and resolve your issues:

  1. In API Gateway, ensure your Cognito User Pools authorizer is correctly configured:

    • Verify that the correct Cognito User Pool is selected.
    • Check that the Token Source is set to "Authorization" (this is the header name Postman should use).
  2. In Postman:

    • Set the Authorization type to "Bearer Token".
    • In the Token field, paste your ID token (not the access token).
    • Ensure the token is fresh and not expired.
  3. If you decide to use access tokens and custom scopes:

  4. Double-check your API's deployment:

    • Make sure you've deployed your API after making any changes to the authorizer or method settings.
  5. Verify your Cognito app client settings:

    • Ensure that the app client used to obtain the token is configured to allow the grant types you're using (e.g., authorization code, client credentials).

If you're still encountering issues after trying these steps, you may need to review your API Gateway logs or CloudWatch logs for more detailed error messages that could point to the specific problem.
Sources
Configuring machine to machine Authentication with Amazon Cognito and Amazon API Gateway – Part 2 | AWS Cloud Operations Blog
Trouble get access to API GAteway with Cognito UserPool | AWS re:Post
Integrate a REST API with an Amazon Cognito user pool - Amazon API Gateway
Control access to REST APIs using Amazon Cognito user pools as an authorizer - Amazon API Gateway
Accessing resources with API Gateway after sign-in - Amazon Cognito

answered a year ago
EXPERT
reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.