AWS cognito userpools JavaScript SDK get user's policy documents

丶灬走出姿态 提交于 2019-12-08 18:26:03

问题


For a registered user in AWS Cognito Userpools, is it possible to retrieve the policy documents attached to the user through IAM roles through JavaScript SDK?

The user case is to write a custom authorizer which authorize cognito id token and return the policy document with the IAM permission, user is capable of assuming through Cognito User Groups.


回答1:


After carrying out further research, following approach is used to retrieve 'inline policies' attached to the user through IAM roles.

  • From AWS Cognito JWT, extract role names from ARNs and using IAM SDK for JavaScript get the policy ARNs by using

    const aws = require('aws-sdk');
    let iam = new aws.IAM();
    iam.listRolePolicies({ RoleName: roleName }, function (err, data) {
        let policyNames = data["PolicyNames"];
        // Use policy names and role names to retrieve policy documents
    });
    
  • Using policy names and role names in combination, retrieve the policy documents in JSON format

    iam.getRolePolicy({ PolicyName: policyName, RoleName: roleName }, 
    function (err, data) {
        let document = decodeURIComponent(data["PolicyDocument"]);
    });
    
  • Next iteratively extract the statements from each policy document and build a single one.

Example code could be found in this github repository.



来源:https://stackoverflow.com/questions/45070378/aws-cognito-userpools-javascript-sdk-get-users-policy-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!