我已经查看过类似的问题,但无法解决我的问题。我正在开发一个 Web 应用程序,用户将在其中使用 AWS Cognito 的身份验证进行身份验证。注册部分没问题,但是当我尝试登录时,出现“未授权”异常。我已经尝试将自定义策略附加到我的 IAM Angular 色(授权 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);
}
});
如您所见,我也在代码中指定了策略,但我仍然收到“AccessDenied: Not authorized to perform sts:AssumeRoleWithWebIdentity”错误。请帮助我:/
编辑:
在“Cognito_AliceAuth_Role”中,我创建了 Angular 色策略: AssumeRoleWithWebIdentityPolicy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRoleWithWebIdentity",
"Resource": "*"
}
]
}
和:GetFederationTokenPolicy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:GetFederationToken",
"Resource": "*"
}
]
}
信任关系:
{
"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"
}
}
}
]
}
最佳答案
似乎您正在使用 Cognito 用户池提供的 Id token 来调用 assumeRoleWithWebIdentity。
您需要先将此 token 与 Cognito 身份联合,然后您可以使用 Cognito 身份提供的 Open Id 连接 token 来调用 assumeRoleWithWebIdentity。 您也可以使用 Enhanced flow 直接调用 getCredentialsForIdentity| .
参见 this了解有关如何将用户池 token 与 Cognito 身份联合的更多信息。
关于javascript - 拒绝访问 : Not authorized to perform sts:AssumeRoleWithWebIdentity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37814997/