Javascript 如何为属于多个用户池组的AWS Cognito用户切换IAM角色?
我有一个使用AWS Amplify和Cognito构建的web应用程序,用于身份验证/授权。Cognito用户池是身份提供者 根据用户应有的权限将用户分组到Cognito用户池组中 我希望一些用户是多个组(例如管理员用户)的一部分,这些组应该具有这些组的总和 组的权限。但是由于用户只能扮演一个角色,我需要能够在应用程序中切换角色 我试图通过使用Javascript 如何为属于多个用户池组的AWS Cognito用户切换IAM角色?,javascript,amazon-web-services,amazon-cognito,amazon-iam,aws-amplify,Javascript,Amazon Web Services,Amazon Cognito,Amazon Iam,Aws Amplify,我有一个使用AWS Amplify和Cognito构建的web应用程序,用于身份验证/授权。Cognito用户池是身份提供者 根据用户应有的权限将用户分组到Cognito用户池组中 我希望一些用户是多个组(例如管理员用户)的一部分,这些组应该具有这些组的总和 组的权限。但是由于用户只能扮演一个角色,我需要能够在应用程序中切换角色 我试图通过使用getCredentialsForIdentity来实现这一点: const cognito_identity = new AWS.CognitoId
getCredentialsForIdentity
来实现这一点:
const cognito_identity = new AWS.CognitoIdentity({ apiVersion: '2014-06-30', region: 'eu-central-1' });
var params = {
IdentityId: 'some_identity',
CustomRoleArn: 'arn:aws:iam::<account_id>:role/editors',
};
cognito_identity.getCredentialsForIdentity(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
const cognito_identity=new AWS.CognitoIdentity({apiVersion:'2014-06-30',region:'eu-central-1'});
变量参数={
IdentityId:“某个_identity”,
CustomRoleArn:“arn:aws:iam:::角色/编辑”,
};
getCredentialsForIdentity(参数,函数(err,数据){
if(err)console.log(err,err.stack);
else console.log(数据);
});
当调用上述代码时,它会失败
NotAuthorizedException:禁止访问标识''some_Identity'。
我需要做什么才能使它工作?在参数中包含
Logins
属性,以getCredentialsForIdentity
后,它工作了:
async function switchRoles(region, identityId, roleArn, cognitoArn) {
const user = await Auth.currentUserPoolUser();
const cognitoidentity = new AWS.CognitoIdentity({ apiVersion: '2014-06-30', region });
const params = {
IdentityId: identityId,
CustomRoleArn: roleArn,
Logins: {
[cognitoArn]: user
.getSignInUserSession()
.getIdToken()
.getJwtToken(),
},
};
return cognitoidentity
.getCredentialsForIdentity(params)
.promise()
.then(data => {
return {
accessKeyId: data.Credentials.AccessKeyId,
sessionToken: data.Credentials.SessionToken,
secretAccessKey: data.Credentials.SecretKey,
expireTime: data.Credentials.Expiration,
expired: false,
};
})
.catch(err => {
console.log(err, err.stack);
return null;
});
}