【发布时间】:2016-10-15 08:41:33
【问题描述】:
我查看了类似的问题,但无法解决我的问题。我正在开发一个 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);
}
});
如您所见,我也在代码中指定了策略,但我仍然收到“AccessDenied: Not authorized to perform sts:AssumeRoleWithWebIdentity”错误。请帮帮我:/
编辑:
在“Cognito_AliceAuth_Role”中,我创建了角色策略: 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"
}
}
}
]
}
【问题讨论】:
-
如何附加策略以允许您的 IAM 用户使用 sts:AssumeRoleWithWebIdentity 不起作用?
-
我真的不知道... :(我已经更新了问题,添加了更多信息
-
当您尝试使用临时凭据进行身份验证时,您会提供哪些信息?
标签: javascript amazon-web-services aws-sdk amazon-cognito