Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

349
Views
How to switch IAM roles for AWS Cognito User belonging to multiple User Pool groups?

I have a web application built with AWS Amplify and Cognito used for authentication/authorization. Cognito User Pools is the identity provider.

Users are grouped into Cognito User Pools groups based on what permissions they should have.

I want some users to be part of multiple groups (e.g. Admin users) which should have the sum of these group's permissions. But since the user can only assume one role I need to be able to switch roles in the app.

I tried accomplishing this by using the 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);
  });

When invoking the above code it fails with NotAuthorizedException: Access to Identity '`some_identity' is forbidden.

What do I need to do to make it work?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

After including the Logins property in the parameters to getCredentialsForIdentity it worked:

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;
    });
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!