我已经查看了类似的问题,但无法解决我的问题。我正在开发一个Web应用程序,用户将使用AWS Cognito的身份验证进行身份验证。注册部分没问题,但是当我尝试登录时,我得到的是#34;未授权"例外。我已经尝试将自定义策略附加到我的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);
}
});
正如您所看到的,我也在代码中指定了策略,但我仍然得到&#34; AccessDenied:未授权执行sts:AssumeRoleWithWebIdentity&#34;错误。请帮帮我:/
编辑:
在&#34; Cognito_AliceAuth_Role&#34;我创建了角色政策: 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"
}
}
}
]
}
答案 0 :(得分:4)
好像您正在使用Cognito用户池提供的Id令牌来调用assumeRoleWithWebIdentity。
您需要先将此令牌与Cognito身份联合起来,然后您可以使用Cognito identity出售的Open Id连接令牌来调用assumeRoleWithWebIdentity。 您也可以使用Enhanced flow直接调用getCredentialsForIdentity。
请参阅this以了解有关如何将用户池令牌与Cognito身份联合的更多信息。
答案 1 :(得分:1)
遇到同样的问题,
AWS.config.region = "<YOUR_REGION>";
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId : '<YOUR_IDENTITY_POOL_ID>',
Logins : {
// Change the key below according to the specific region your user pool is in.
`cognito-idp.${AWS.config.region}.amazonaws.com/${data.UserPoolId}` : session.getIdToken().getJwtToken()
}
});
参考文章 - https://aws.amazon.com/blogs/developer/authentication-with-amazon-cognito-in-the-browser/