aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Cognito: Identity providers and OAuth flows are enabled by default

Open Urokhtor opened this issue 6 years ago • 5 comments

:question: General Issue

Recently issue #8134 made OAuth flows be enabled by default event if your user pool had not defined identity providers before.

The Question

Is it reasonable to enable those flows by default since for example here it's suggested that identity providers that support OAuth flows need to be enabled manually?

In my use case we have no need for these features and don't want them to be enabled by default. Unfortunately disabling them is not as straightforward as you need to do something like this:

const userPoolClient = new UserPoolClient(this, 'UserPoolClient', {
    ...
    oAuth: {
        flows: {
            implicitCodeGrant: false,
            authorizationCodeGrant: false
        },
        scopes: []
    },
    supportedIdentityProviders: []
};

const cfnUserPoolClient = userPoolClient.node.defaultChild as CfnUserPoolClient;
cfnUserPoolClient.allowedOAuthFlowsUserPoolClient = undefined;

This is quite involved and the worst thing is that you actually need to interact with low-level cfn API to be able to disable everything.

Environment

  • CDK CLI Version: 1.43.0
  • Module Version: 1.43.0

Other information

I do recognize that this breaking change was detailed in recent release notes. I'm just wondering if these defaults are reasonable since they seem to enable quite powerful features not everyone wants to use. Or have I misunderstood something about the implications of this change?

Urokhtor avatar Jun 08 '20 10:06 Urokhtor

Hello @Urokhtor -

Thanks for opening this issue. I see how painful this can be.

Would just setting the allowedOAuthFlowsUserPoolClient to false (and leaving out the changes to oAuth struct) work for your case? It seems from its description that this flag should turn off all oAuth configuration on the user pool client.

nija-at avatar Jun 10 '20 08:06 nija-at

I suppose that would be OK if it indeed turns off all OAuth configuration. It would be good to have a way to set that property with CDK's high level API instead of interacting with low-level cfn API though.

Urokhtor avatar Jun 10 '20 09:06 Urokhtor

I agree with @Urokhtor that there needs to be a way to support this from the high-level CDK API

Example of my current code that results in a None for the OAuthSettings when oauth flows aren't specified in the configuration because I don't want oauth enabled for that user pool client.

           # Gather oauth settings
            oauth_settings=None
            if appclient.get('oauth_flows', {}):
                oauth_settings = aws_cognito.OAuthSettings(
                    flows=aws_cognito.OAuthFlows(**appclient['oauth_flows']),
                    scopes=oauth_scopes,
                    callback_urls=pool_def['app_clients_extra'][appclient['user_pool_client_name']]['callback_urls'] )

            # Add client to pool
            client = user_pool.add_client(appclient['user_pool_client_name'],
                auth_flows=aws_cognito.AuthFlow(**appclient['auth_flows']),
                generate_secret=appclient['generate_secret'],
                o_auth=oauth_settings,
                prevent_user_existence_errors=appclient['prevent_user_existence_errors'],
                user_pool_client_name=appclient['user_pool_client_name'],
            )

Now using the current version I would have to add this to explicitly indicate that there shouldn't be oauth settings:

            if not oauth_settings:
                client.node.default_child.allowed_o_auth_flows_user_pool_client = False

However, this results in the following Cloudformation code which still includes the "default" values you are setting for AllowedOAuthFlows and AllowedOAuthScopes

      AllowedOAuthFlows:
        - implicit
        - code
      AllowedOAuthFlowsUserPoolClient: false
      AllowedOAuthScopes:
        - profile
        - phone
        - email
        - openid
        - aws.cognito.signin.user.admin

Oddly this doesn't fail during deployment, but it shows a warning in the cognito console with the enabled flows/scopes above: Your User Pool app client settings have been modified via the Cognito User Pools API (using a Lambda or CLI), and OAuth was not enabled. See the documentation for more details.

chalupaa avatar Jun 10 '20 15:06 chalupaa

In order to not have the oauth settings enabled properly I had to specify all of the following:

            if not oauth_settings:
                client.node.default_child.allowed_o_auth_flows_user_pool_client = False
                client.node.default_child.allowed_o_auth_flows = None
                client.node.default_child.allowed_o_auth_scopes = None

chalupaa avatar Jun 10 '20 16:06 chalupaa

A fix for this is in PR https://github.com/aws/aws-cdk/pull/8496

nija-at avatar Jun 11 '20 13:06 nija-at