With AWS-Cognito-Identity-Js I get the session identifier token session.getIdToken().getJwtToken() for an authenticated Cognito user.
I pass this token my AWSInitialize function and update my AWS credentials:
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 }) }); };
This works correctly, because now, for example, I can execute the Lambda function on behalf of an authenticated Cognito user.
var lambda = new AWS.Lambda({}); lambda.invoke({FunctionName: 'createToken'}, function(err, data) ...
This is possible because in Cognito_myAppAuth_Role I have bound a policy that allows me to execute this Lambda function:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "Stmt1471300653000", "Effect": "Allow", "Action": [ "lambda:InvokeFunction" ], "Resource": [ "arn:aws:lambda:eu-west-1:593845191076:function:createToken" ] } ] }
Now what I'm trying to do is get tokens with STS for the same users
For this, I applied a different policy to Cognito_myAppAuth_Role . This should allow Cognito users to call assumeRoleWithWebIdentity :
{ "Version": "2012-10-17", "Statement": [ { "Sid": "Stmt1472560044000", "Effect": "Allow", "Action": [ "sts:*" ], "Resource": [ "*" ] } ] }
But when I run this code:
var sts = new AWS.STS({}); var params = { RoleArn: 'arn:aws:iam::593845191076:role/Cognito_myAppAuth_Role', RoleSessionName: "UserName", WebIdentityToken: token, }; sts.assumeRoleWithWebIdentity(params, function(err, data) { if (err) console.log(err, err.stack);
I get the following error:
AccessDenied: Not authorized to perform sts:AssumeRoleWithWebIdentity
I do not understand why the user is not allowed to execute sts: AssumeRoleWithWebIdentity. I applied the STS policy to the authenticated role, and Lambda-Policy also worked for the user
Where could the problem be? How can I solve this problem?
Thanks a lot!