【发布时间】:2016-08-30 12:46:47
【问题描述】:
使用AWS-Cognito-Identity-Js,我为经过身份验证的 Cognito 用户获取会话 ID 令牌 session.getIdToken().getJwtToken()。
我将此 token 传递给我的 AWSInitialize 函数并更新 AWS 凭证:
var AWSInitialize = function(token){
Logins = {};
Logins['cognito-idp.' + AWSCognito.config.region + '.amazonaws.com/' + poolData.UserPoolId] = token;
AWS.config.update({
region: AWSCognito.config.region,
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId : identityPoolId,
region: AWSCognito.config.region,
Logins : Logins
})
});
};
这可以正常工作,因为现在我可以代表经过身份验证的 Cognito 用户执行 Lambda 函数。
var lambda = new AWS.Lambda({});
lambda.invoke({FunctionName: 'createToken'}, function(err, data) ...
这是可能的,因为在Cognito_myAppAuth_Role 中我附加了一个允许我执行这个 Lambda 函数的策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1471300653000",
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": [
"arn:aws:lambda:eu-west-1:593845191076:function:createToken"
]
}
]
}
现在我要做的是为相同的用户获取带有 STS 的令牌
为此,我将另一项政策附加到 Cognito_myAppAuth_Role。它应该允许 Cognito 用户调用 assumeRoleWithWebIdentity:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1472560044000",
"Effect": "Allow",
"Action": [
"sts:*"
],
"Resource": [
"*"
]
}
]
}
但是当我运行这段代码时:
var sts = new AWS.STS({});
var params = {
RoleArn: 'arn:aws:iam::593845191076:role/Cognito_myAppAuth_Role', /* required */
RoleSessionName: "UserName", /* required */
WebIdentityToken: token, /* required */
};
sts.assumeRoleWithWebIdentity(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
我收到以下错误:
AccessDenied: Not authorized to perform sts:AssumeRoleWithWebIdentity
我不明白为什么用户无权执行 sts:AssumeRoleWithWebIdentity。我为经过身份验证的角色附加了一个 STS 策略,并且 Lambda-Policy 也适用于用户
问题可能出在哪里? 我该如何解决这个问题?
非常感谢!
【问题讨论】:
-
只是澄清一下,这些政策都附加到同一个角色?您是否使用相同的用户/身份运行它?为什么要手动使用 STS?
标签: amazon-web-services lambda policy amazon-cognito