Api 使用 Chalice 和 Cognito 授权者创建 returns “未经授权”

Api created with Chalice and a Cognito authorizer returns “Unauthorized”

我正在尝试使用 built-in 登录对话框将 Cognito 与 AWS Chalice 集成。这是我试过的:

# This passes in correct arn for my pool, not xxxx
authorizer = CognitoUserPoolAuthorizer(
    'end_users_dev', provider_arns=['arn:aws:cognito-idp:us-west-2:xxxx])

@app.route('/test', cors=True, authorizer=authorizer)
def test():
    return {"result": "Success with authorizer"}

@app.route('/test2', cors=True)
def test2():
    return {"result": "Success without authorizer"}

第二种方法 (test2) 有效,但第一种方法 (test) returns(如预期):

{
    "message": "Unauthorized"
}

现在,我尝试通过传入 header:

来进行授权测试
Authorization: <the token I get passed in from the 
built in login page callback as "id_token">

我可以手动验证 JWT 令牌内容和签名,并且用户池在 API 网关中显示为测试资源的 "Authorization",但我仍然得到相同的 "Unauthorized" 消息。我错过了什么?

(注意:我也在 https://forums.aws.amazon.com/message.jspa?messageID=871715#871715 上发布了这个,但 2 天内没有收到任何回复)

我会检查以确保您的 IAM 策略圣杯 运行允许访问 Cognito。

您可以根据需要将这些从 AmazonCognitoPowerUser 策略添加到您的策略中。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cognito-identity:*",
                "cognito-idp:*",
                "cognito-sync:*",
                "iam:ListRoles",
                "iam:ListOpenIdConnectProviders",
                "sns:ListPlatformApplications"
            ],
            "Resource": "*"
        }
    ]
}

见下面的 link “

无论何时使用 chalice 部署您的应用程序,自动生成的策略都会写入磁盘 /.chalice/policy.json。当您 运行 chalice deploy 命令时,您还可以指定 --no-autogen-policy 选项。这样做将导致 chalice CLI 加载 /.chalice/policy.json 文件并将该文件用作 IAM 角色的策略。如果您想完全控制与 IAM 角色关联的 IAM 策略,您可以手动编辑此文件并指定 --no-autogen-policy。

"

如此处的政策部分所示:https://github.com/aws/chalice

$ chalice gen-policy
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "s3:ListAllMyBuckets"
      ],
      "Resource": [
        "*"
      ],
      "Effect": "Allow",
      "Sid": "9155de6ad1d74e4c8b1448255770e60c"
    }
  ]
}