首页 > 编程问答 >使用 aws cdk 设置用户池客户端属性以具有读/写访问权限 - Python

使用 aws cdk 设置用户池客户端属性以具有读/写访问权限 - Python

时间:2024-07-26 05:32:59浏览次数:10  
标签:python amazon-cognito aws-cdk

我试图根据属性给予一些自定义属性特定的读/写访问权限。我收到此错误。

资源处理程序返回消息:“无效 写入创建客户端时指定的属性(服务:CognitoIdentityProvider,状态代码:400,请求 ID:<request_id>)”(RequestToken:<request_token>,HandlerErrorCode:InvalidRequest)

任何人都可以为我指出正确的方向或告诉我为什么会发生这种情况?显然,我明白错误告诉我什么,但我不知道是什么(具体)导致了它或如何修复它,也许与我开始创建属性的方式有关。 ...

这是我的代码;

self.my_user_pool = cognito.UserPool(
    self,
    COGNITO_USER_POOL_ID,
    sign_in_aliases=cognito.SignInAliases(email=True),
    self_sign_up_enabled=True,
    auto_verify=cognito.AutoVerifiedAttrs(email=True),
    user_verification=cognito.UserVerificationConfig(
        email_style=cognito.VerificationEmailStyle.LINK
    ),
    custom_attributes={
        "custom_attribute_1": cognito.StringAttribute(mutable=True),
        "custom_attribute_2": cognito.StringAttribute(mutable=True),
    },
    password_policy=cognito.PasswordPolicy(
        min_length=8,
        require_lowercase=True,
        require_uppercase=True,
        require_digits=True,
        require_symbols=True,
    ),
    account_recovery=cognito.AccountRecovery.EMAIL_ONLY,
    removal_policy=RemovalPolicy.DESTROY,
)


client_read_attributes = (cognito.ClientAttributes()).with_custom_attributes(
    "custom:custom_attribute_1", "custom:custom_attribute_2"
)
client_write_attributes = (cognito.ClientAttributes()).with_custom_attributes(
    "custom:custom_attribute_1"
)

self.my_user_pool_client = self.user_pool.add_client(
    "<my_cognito_client_id>",
    access_token_validity=Duration.minutes(60),
    id_token_validity=Duration.minutes(60),
    refresh_token_validity=Duration.days(1),
    auth_flows=cognito.AuthFlow(admin_user_password=True, user_srp=True, custom=True),
    o_auth=cognito.OAuthSettings(flows=cognito.OAuthFlows(implicit_code_grant=True)),
    prevent_user_existence_errors=True,
    generate_secret=True,
    read_attributes=client_read_attributes,
    write_attributes=client_write_attributes,
    enable_token_revocation=True,
)

代码中的错误在于尝试使用 with_custom_attributes 方法向 client_read_attributes client_write_attributes 添加自定义属性的方式。该方法不接受属性名称列表。

为了在 AWS CDK 中为用户池客户端设置具有读/写访问权限的自定义属性,需要使用 cognito.ClientAttributes 类的新实例,并指定要允许读写的每个属性的范围。

以下是更新后的代码:

from aws_cdk import (
    aws_cognito as cognito,
    core,
    Duration
)

class YourStack(core.Stack):

    def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # ... your existing code ...

        # Define read attributes with access token scope
        client_read_attributes = cognito.ClientAttributes(
            standard_attributes=cognito.StandardAttributes(
                email_verified=cognito.StandardAttribute.read()
            ),
            custom_attributes={
                "custom:custom_attribute_1": cognito.ClientAttribute.readable(),
                "custom:custom_attribute_2": cognito.ClientAttribute.readable()
            }
        )
        # Define write attributes with access token scope
        client_write_attributes = cognito.ClientAttributes(
            custom_attributes={
                "custom:custom_attribute_1": cognito.ClientAttribute.writable()
            }
        )

        self.my_user_pool_client = self.my_user_pool.add_client(
            "<my_cognito_client_id>",
            # ... your existing code ...
            read_attributes=client_read_attributes,
            write_attributes=client_write_attributes,
            # ... your existing code ...
        )

在这个更新后的代码中:

  1. 我们创建了两个 cognito.ClientAttributes 实例: client_read_attributes client_write_attributes

  2. 在每个实例中,我们使用 custom_attributes 参数来指定自定义属性及其访问权限。

  3. 对于 client_read_attributes ,我们使用 cognito.ClientAttribute.readable() 来授予对 custom:custom_attribute_1 custom:custom_attribute_2 的读取访问权限。

  4. 对于 client_write_attributes ,我们使用 cognito.ClientAttribute.writable() 来授予对 custom:custom_attribute_1 的写入访问权限。

  5. 最后,我们将 client_read_attributes client_write_attributes 分别传递给 add_client 方法的 read_attributes write_attributes 参数。

通过进行这些更改,应该能够为用户池客户端成功设置具有读/写访问权限的自定义属性。

标签:python,amazon-cognito,aws-cdk
From: 78784687

相关文章