首页 > 解决方案 > 从 Cognito 获取 ClientID 的最佳方法

问题描述

在我Cognito创建了多组用户池和客户端 ID 的设置中。每个用户池将包含一定数量的用户,其范围仅限于各自的 clientId。

现在我创建了 3 个不同的 lambda,并将这些 lambda 链接到 Api 网关。那些 lambda 和 API 端点路径看起来像signup, confirm-signup, forgot-password, confirm-password.

我的 python 准备好为客户端 ID 工作,并且存在一个用户池,这些 lambda 工作正常。我硬编码了我的clientid。例如

def lambda_handler(event, context):
    for field in ["username", "email", "password", "name"]:
        if not event.get(field):
            return {"error": False, "success": True, 'message': f"{field} is not present", "data": None}
    username = event['username']
    email = event["email"]
    password = event['password']
    name = event["name"]
    client = boto3.client('cognito-idp')

client.sign_up(
            ClientId=CLIENT_ID,
            SecretHash=get_secret_hash(username),
            Username=username,
            Password=password, 
            UserAttributes=[
            {
                'Name': "name",
                'Value': name
            },
            {
                'Name': "email",
                'Value': email
            }
            ],
            ValidationData=[
                {
                'Name': "email",
                'Value': email
            },
            {
                'Name': "custom:username",
                'Value': username
            }
])

这个client-id指的是这个client-id所属的用户池。作为 boto3 客户端 ID 的一部分,必须调用sign-up.

所以,现在我的问题是我有 2 个用户池和 2 个客户端 ID。

userpoolA-clientIdA
userpoolB-clinetIdB

所以,现在对于signupAPI 调用,我正在传递 ["username", "email", "password", "name", "userpoolA"] 现在我的问题是如何进入clientIdAlambdauserpoolA以便我可以调用sign-upmthod 并添加这个用户名到那个userpoolA

是的,这是必要的,我们需要以 poolA 用户不能也不会有资格访问 poolB 令牌或授权的方式来隔离用户。

我看到describe_user_pool但作为输入,它需要 UserPoolId 而不是 userpoolName。如果有人能提出最好的方法,将不胜感激。

标签: pythonboto3amazon-cognito

解决方案


推荐阅读