【问题标题】:AWS Cognito Generated Credentials are not accepted by API GatewayAPI Gateway 不接受 AWS Cognito 生成的凭证
【发布时间】:2016-09-25 18:14:55
【问题描述】:

我已设置 API Gateway Endpoints,授权启用为 IAM。

首先,我尝试使用自己的用户凭据(密钥、机密)访问资源,并且成功了。

然后,我使用身份池设置了 Cognito。该池允许经过身份验证和未经身份验证的访问。 我已经使用 PHP SDK 生成了凭据:

$id = $cognitoClient->getId([
        'AccountId' => 'xxx',
        'IdentityPoolId' => 'xxx', 
    ]);
$credentials = $cognitoClient->getCredentialsForIdentity([
    'IdentityId' => $id->get('IdentityId')
])->get('Credentials');

这能够生成凭据 - 正在返回 AccessKeyIdSecretKeySessionToken

我附加的未经身份验证访问的角色定义如下:

信任关系:

{
   "Version": "2012-10-17",
   "Statement": [
     {
       "Effect": "Allow",
       "Principal": {
         "Federated": "cognito-identity.amazonaws.com"
       },
       "Action": "sts:AssumeRoleWithWebIdentity",
       "Condition": {
         "StringEquals": {
           "cognito-identity.amazonaws.com:aud": "xxx"
         }
       }
     }
  ]
}

内联策略:

{
  "Version": "2012-10-17",
  "Statement": [
    {
        "Effect": "Allow",
        "Action": "cognito-sync:*",
        "Resource": [
            "arn:aws:cognito-sync:us-east-1:123456789012:identitypool/${cognito-identity.amazonaws.com:aud}/identity/${cognito-identity.amazonaws.com:sub}/*"
        ]
    }
  ]
}

{
  "Version": "2012-10-17",
  "Statement": [
    {
        "Effect": "Allow",
        "Action": "cognito-sync:*",
        "Resource": [
            "arn:aws:cognito-sync:us-east-1:xxxxx:identitypool/*"
        ]
    }
  ]
}

{
  "Version": "2012-10-17",
  "Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "apigateway:*"
        ],
        "Resource": [
            "*"
        ]
    }
  ]
}

我没有为该角色附加任何托管策略。

现在,在使用 PHP SDK 生成凭据后,我使用邮递员访问资源(我在使用我的帐户凭据时也使用了邮递员)。此调用给出以下错误:

{"message":"The security token included in the request is invalid."}

我无法确定这里出了什么问题。

【问题讨论】:

    标签: php amazon-web-services amazon-cognito aws-api-gateway


    【解决方案1】:

    目前,我已切换到使用可为每个部署生成的 Javascript SDK。

    使用 SDK 可以解决问题。我观察到的一件事是我必须在 SDK 中传递我之前没有传递的会话令牌(我不知道把它放在哪里)。

    【讨论】:

    • 那将是我的猜测。我不知道 Postman 是否支持临时凭证,但是在使用 Cognito 或任何会话凭证时,您需要在请求中发送 SessionToken。很高兴 SDK 为您工作!
    【解决方案2】:

    TL;DR :要让它与 Postman 一起使用,您必须在名为 X-Amz-Security-Token 的标头中传递您的令牌。

    首先谢谢,我一直在为同样的问题苦苦挣扎,直到你自己的回答,这导致我找到了解决方案。

    您可以在 javascript SDK 的 README 中找到:

    var apigClient = apigClientFactory.newClient({
        accessKey: 'ACCESS_KEY',
        secretKey: 'SECRET_KEY',
        sessionToken: 'SESSION_TOKEN', //OPTIONAL: If you are using temporary credentials you must include the session token
        region: 'eu-west-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1
    });
    

    好的,所以您需要临时凭证的令牌,很高兴在 cognito 文档而不是此处看到它,但没关系。我辞职写一个javascript版本来测试,显然需要CORS才能工作。在激活 CORS 的页面上,您有字段 Access-Control-Allow-Headers 和默认值 Content-Type, X-Amz-Date, Authorization, X-Api-Key, X-Amz-Security-Token,就是这样。

    【讨论】:

    • 谢谢!我明天试试。从外观上看,我认为它会起作用。我会在这里更新。
    猜你喜欢
    • 1970-01-01
    • 2016-09-06
    • 2017-01-14
    • 2016-08-20
    • 2017-12-30
    • 2018-12-16
    • 2020-01-05
    • 1970-01-01
    • 2019-09-15
    相关资源
    最近更新 更多