AWS Cognito Saas 演示:如何实现 "Get Access Token (with IAM policies)?"

AWS Cognito Saas Demo: How does it "Get Access Token (with IAM policies)?"

设置 我正在处理 "Saas identity and isolation with Amazon Cognito" deployment guide and accompanying source code.

在部署、测试和阅读所有内容后,我能够弄清楚

主要问题 这一切是如何联系在一起的?具体来说,租户用户池中的用户如何与身份池中的策略相关联? "Get Access Token (with IAM policies)" 周围似乎有人在挥手。这是如何运作的?自定义授权人如何适应?

更多信息 我相信这与 token-manager.js:346

有关
var cognitoidentity = new AWS.CognitoIdentity({apiVersion: '2014-06-30',region: configuration.aws_region});
var params = {
    IdentityId: event.IdentityId, /* required */
    //CustomRoleArn: 'STRING_VALUE',
    Logins: {
        [event.provider]: event.token,
        /* '<IdentityProviderName>': ... */
    }
};
cognitoidentity.getCredentialsForIdentity(params, function (err, data) {

我看到 getCredentialsForIdentity 被记录为采用 CustomRoleArn,如下所示。上面的代码已将此注释掉,但代码仍然有效。

var params = {
  IdentityId: 'STRING_VALUE', /* required */
  CustomRoleArn: 'STRING_VALUE',
  Logins: {
    '<IdentityProviderName>': 'STRING_VALUE',
    /* '<IdentityProviderName>': ... */
  }
};

自定义授权方custom-authorizer/index.js 中,我能找到的与 permission/policy 有关的唯一代码是:

var policy = new AuthPolicy(principalId, awsAccountId, apiOptions);
policy.allowAllMethods();
const authResponse = policy.build();

<Gasp> 此代码使我们似乎绕过了所有策略。有没有可能这个应用程序在伪造它并且只处理 Angular 中的路由?可能不会。

补充问题 通过 AWS PowerShell. The one thing I don't know how to retrieve is the User Pool Identity Provider. The Get-CGIPIdentityProvider cmdlet 需要 "ProviderName",我已经能够提取几乎所有我需要知道的信息。这是什么? "Cognito"? "www.amazon.com"?一些标识符?

重复主要问题,以防您只是滚动到底部。 这一切是如何联系在一起的?具体来说,租户用户池中的用户如何与身份池中的策略相关联? "Get Access Token (with IAM policies)" 周围似乎有人在挥手。这是如何运作的?自定义授权人如何适应?

角色通过 Cognito 身份池在内部关联。下面的代码显示我们通过配置映射这个。

cognito-user.js:911 - addRoleToIdentity

var cognitoidentity = new AWS.CognitoIdentity(...);

# [ ... ]

RulesConfiguration: {
    Rules: [/* required */
        {
        Claim: 'custom:role', /* required */
        MatchType: 'Equals', /* required */
        RoleARN: identityPoolRoleParams.rolesystem, /* required */
        Value: identityPoolRoleParams.adminRoleName /* required */
        },
        {
        Claim: 'custom:role', /* required */
        MatchType: 'Equals', /* required */
        RoleARN: identityPoolRoleParams.rolesupportOnly, /* required */
        Value: identityPoolRoleParams.userRoleName /* required */
        },
    ]
cognitoidentity.setIdentityPoolRoles(params, function (err, data) {

# [ ... ]

此配置可以用以下伪代码表示,其中 user.'custom:role' 是一个字符串。

const user = userpool.getuser(...)
if (user.'custom:role' == identityPoolRoleParams.adminRoleName)
    role = identityPoolRoleParams.roleSystem
else if (user.'custom:role' == identityPoolRoleParams.userRoleName) 
    role = identityPoolRoleParams.rolessupportOnly