How to create Cognito IdentityPool with Cognito UserPool as one of the Authentication provider using aws cdk?

前端 未结 3 998
猫巷女王i
猫巷女王i 2021-02-15 17:39

I am trying to create a Cognito FederatedIdentityPool with CognitoUserPool as one Authentication Provider. Creating UserPool was easy enough:



        
相关标签:
3条回答
  • 2021-02-15 17:59

    This is the way I managed to mimic the default configuration created through the aws console when you create an identity pool with a user pool as identity provider. It includes some other features apart from what you have asked (allows unauthenticated access and specify the password policy), but is easy to modify to your needs.

        const userPool = new cognito.UserPool(this, 'MyUserPool', {
            signInType: SignInType.EMAIL,
            autoVerifiedAttributes: [
                UserPoolAttribute.EMAIL
            ]
        });
        const cfnUserPool = userPool.node.defaultChild as cognito.CfnUserPool;
        cfnUserPool.policies = {
            passwordPolicy: {
                minimumLength: 8,
                requireLowercase: false,
                requireNumbers: false,
                requireUppercase: false,
                requireSymbols: false
            }
        };
        const userPoolClient = new cognito.UserPoolClient(this, 'MyUserPoolClient', {
            generateSecret: false,
            userPool: userPool,
            userPoolClientName: 'MyUserPoolClientName'
        });
        const identityPool = new cognito.CfnIdentityPool(this, 'MyCognitoIdentityPool', {
            allowUnauthenticatedIdentities: false,
            cognitoIdentityProviders: [{
                clientId: userPoolClient.userPoolClientId,
                providerName: userPool.userPoolProviderName,
            }]
        });
        const unauthenticatedRole = new iam.Role(this, 'CognitoDefaultUnauthenticatedRole', {
            assumedBy: new iam.FederatedPrincipal('cognito-identity.amazonaws.com', {
                "StringEquals": { "cognito-identity.amazonaws.com:aud": identityPool.ref },
                "ForAnyValue:StringLike": { "cognito-identity.amazonaws.com:amr": "unauthenticated" },
            }, "sts:AssumeRoleWithWebIdentity"),
        });
        unauthenticatedRole.addToPolicy(new PolicyStatement({
            effect: Effect.ALLOW,
            actions: [
                "mobileanalytics:PutEvents",
                "cognito-sync:*"
            ],
            resources: ["*"],
        }));
        const authenticatedRole = new iam.Role(this, 'CognitoDefaultAuthenticatedRole', {
            assumedBy: new iam.FederatedPrincipal('cognito-identity.amazonaws.com', {
                "StringEquals": { "cognito-identity.amazonaws.com:aud": identityPool.ref },
                "ForAnyValue:StringLike": { "cognito-identity.amazonaws.com:amr": "authenticated" },
            }, "sts:AssumeRoleWithWebIdentity"),
        });
        authenticatedRole.addToPolicy(new PolicyStatement({
            effect: Effect.ALLOW,
            actions: [
                "mobileanalytics:PutEvents",
                "cognito-sync:*",
                "cognito-identity:*"
            ],
            resources: ["*"],
        }));
        const defaultPolicy = new cognito.CfnIdentityPoolRoleAttachment(this, 'DefaultValid', {
            identityPoolId: identityPool.ref,
            roles: {
                'unauthenticated': unauthenticatedRole.roleArn,
                'authenticated': authenticatedRole.roleArn
            }
        });
    
    

    Why is there a UserPool and CfnUserPool? What is difference between them and which one is supposed to be used?

    UserPool is a high-level representation of the resource and is the prefered way to work but not all the properties are implemented yet. CfnUserPool (an any Cfn prefixed class) is a low-level representation that maps to a Cloudformation resource. You can use both when the high-level class don't fulfill your necessities, as in the example.

    0 讨论(0)
  • 2021-02-15 18:01

    The CDK must have changed this was created. I got it working with the example from @CCarlos:

    const pool = new cognito.CfnUserPool(this, "cdkUserpool", {
      userPoolName: "cdkUserPoolName",
      usernameAttributes: ["email"],
    });
    const client = new cognito.CfnUserPoolClient(this, "cdkClient", {
      userPoolId: pool.ref, // <--- This part has changed.
      explicitAuthFlows: ["ADMIN_NO_SRP_AUTH"],
      generateSecret: false,
      readAttributes: [
        "preferred_username",
        "website",
        "email",
        "name",
        "zoneinfo",
        "phone_number",
        "phone_number_verified",
        "email_verified",
      ],
      writeAttributes: ["name", "zoneinfo", "phone_number"],
    });
    
    0 讨论(0)
  • 2021-02-15 18:15

    I was able to figure our how to attach UserPool to Identity Pool

        const userPool = new cognito.CfnUserPool(this, 'MyCognitoUserPool')
        const userPoolClient = new cognito.CfnUserPoolClient(this, 'MyCognitoUserPoolClient', {
          generateSecret: false,
          userPoolId: userPool.userPoolId
        });
    
        const identityPool = new cognito.CfnIdentityPool(this, 'MyCognitoIdentityPool', {
          allowUnauthenticatedIdentities: false,
          cognitoIdentityProviders: [{
            clientId: userPoolClient.userPoolClientId,
            providerName: userPool.userPoolProviderName
          }]
        });
    

    Still struggling with attaching Role to IdentityPool and don't know the difference between CfnUserPool and UserPool. However, this question can marked as partially resolved.

    0 讨论(0)
提交回复
热议问题