AWS Cognito用户池JavaScript SDK获取用户策略文件

7

对于 AWS Cognito Userpools 中的注册用户,能否通过 JavaScript SDK 通过 IAM 角色检索附加到用户的策略文档?

用户案例是编写自定义授权者,该授权者授权 Cognito ID 令牌并返回带有 IAM 权限的策略文档,用户能够通过 Cognito 用户组进行假定。


你打算如何使用政策文件,以便找到解决方案? - Rachit Dhall
我计划将其用于API网关资源授权。理想情况下,它更像是API网关的CognitoAuthorizer和IAMAuthorizer的组合。 - Ashan
1个回答

2

经过进一步的研究,以下方法用于通过IAM角色检索附加到用户的“内联策略”。

  • 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.

可以在这个 GitHub 代码库中找到示例代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接