AccessDenied:未授权执行sts:AssumeRoleWithWebIdentity

Dhi*_*rêa 7 javascript amazon-web-services amazon-cognito aws-sdk

我查看了类似的问题,但无法解决我的问题.我正在开发一个Web应用程序,用户将使用AWS Cognito的身份验证进行身份验证.注册部分没问题,但是当我尝试登录时,我收到了"未授权"的例外情况.我已经尝试将自定义策略附加到我的IAM角色(授权sts:AssumeRoleWithWebIdentity),但是没有用.以下是代码的编写方式:

        var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        var sts = new AWS.STS({apiVersion: '2011-06-15'});

        var params = {
            RoleArn: 'arn:aws:iam::981601120657:role/Cognito_AliceAuth_Role', /* required */
            RoleSessionName: 'AliceUserSession', 
            WebIdentityToken: result.getIdToken().getJwtToken(), 
            Policy: '{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "sts:AssumeRoleWithWebIdentity", "Resource": "*" } ] }'
        };

        sts.assumeRoleWithWebIdentity(params, function (err, data) {
            if (err)
                console.log(err, err.stack); // ** <-- ERROR HERE
            else
                console.log(data);           // successful response
        });

        //document.getElementById('authForm').submit();
    },
    onFailure: function (err) {
        alert(err);
    }

});
Run Code Online (Sandbox Code Playgroud)

如您所见,我也在代码中指定了策略,但我仍然得到"AccessDenied:未授权执行sts:AssumeRoleWithWebIdentity"错误.请帮我 :/

编辑:

在"Cognito_AliceAuth_Role"内部,我创建了角色策略:AssumeRoleWithWebIdentityPolicy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:AssumeRoleWithWebIdentity",
            "Resource": "*"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

和:GetFederationTokenPolicy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:GetFederationToken",
            "Resource": "*"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

信任关系:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "cognito-identity.amazonaws.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "cognito-identity.amazonaws.com:aud": "us-east-1:e4c1833d-a62b-402a-b995-1b2513b04c02"
        },
        "ForAnyValue:StringLike": {
          "cognito-identity.amazonaws.com:amr": "authenticated"
        }
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

Vin*_*aha 5

似乎您正在使用 Cognito 用户池提供的 Id 令牌来调用假设RoleWithWebIdentity。

您需要先将此令牌与 Cognito 身份联合,然后可以使用 Cognito 身份提供的 Open Id 连接令牌来调用 AssumeRoleWithWebIdentity。您也可以使用Enhanced flow直接调用 getCredentialsForIdentity 。

请参阅此内容以了解有关如何将用户池令牌与 Cognito 身份联合的更多信息。