AWS 用户迁移 Lambda 不会在 Cognito 中创建用户

问题描述 投票:0回答:2

由于某种原因,我无法再使用

user-migration
在 Cognito 中创建用户。我已将其与最基本的配对:

    def migration_handler(event, context):

        # Create the user in Cognito
        event["response"]["userAttributes"] = {
            "username": event["userName"],
            "email": event["userName"],
            # "email_verified": True,
            # "custom:nutrinoId": 'nutrino_id',
            # 'custom:finished-signup': 'true'
        }
        event["response"]["finalUserStatus"] = "CONFIRMED"
        event["response"]["messageAction"] = "SUPPRESS"

        return event

我正在使用无服务器(v2.72.2)进行发布,当迁移处理程序触发时,一切似乎都很好。以下是一次

InitiateAuth
尝试的 Cloudwatch 日志:

时间戳 留言
1643820943789 START 请求 ID:7381165c-88b7-4a57-98b2-28abc8a53abe 版本:$LATEST
1643820943789 [INFO] 2022-02-02T16:55:43.789Z 在环境变量中找到凭据。
1643820943884 [信息] 2022-02-02T16:55:43.884Z 7381165c-88b7-4a57-98b2-28abc8a53abe 迁移 Lambda。事件:{'version':'1','triggerSource':'UserMigration_Authentication','region':'us-west-2','userPoolId':'us-west-2_xxx','userName':'[电子邮件受保护]', 'callerContext': {'awsSdkVersion': 'aws-sdk-unknown-unknown', 'clientId': 'xxx'}, 'request': {'password': 'TestPassword', 'validationData' :无,'userAttributes':无},'响应':{'userAttributes':无,'forceAliasCreation':无,'finalUserStatus':无,'messageAction':无,'desiredDeliveryMediums':无}}
1643820943884 [调试] 2022-02-02T16:55:43.884Z 7381165c-88b7-4a57-98b2-28abc8a53abe 最终事件:{'version':'1','triggerSource':'UserMigration_Authentication','region':'us- west-2', 'userPoolId': 'us-west-2_xxx', '用户名': '[电子邮件受保护]', 'callerContext': {'awsSdkVersion': 'aws-sdk-unknown-unknown', ' clientId': 'xxx'}, 'request': {'password': 'TestPassword', 'validationData': None, 'userAttributes': None}, 'response': {'userAttributes': {'username': ' [电子邮件受保护]'}、'forceAliasCreation':无、'finalUserStatus':'已确认'、'messageAction':'SUPPRESS'、'desiredDeliveryMediums':无}}
1643820943885 END 请求 ID:7381165c-88b7-4a57-98b2-28abc8a53abe
1643820943885 报告请求 ID:7381165c-88b7-4a57-98b2-28abc8a53abe 持续时间:1.74 毫秒 计费持续时间:2 毫秒 内存大小:1024 MB 最大已用内存:65 MB 初始化持续时间:797.46 毫秒

据我所知,lambda 中返回的最终事件对象应该创建用户,但我从 boto 收到

NotAuthorizedException: Incorrect username or password.
异常,并且该用户未在我的用户池中列出。

如您所见,lambda 日志中没有错误消息。

关于进一步调查有什么想法或建议吗?

python aws-lambda boto3 amazon-cognito serverless
2个回答

0
投票

确保目标池允许用户名属性的电子邮件格式,在我的情况下,当我手动创建用户时,它会抛出此错误:“用户名不能是电子邮件格式,因为用户池配置为电子邮件别名。”所以我尝试使用分割功能来剪切电子邮件中的用户名,它对我有用:

const handler = async (event) => {
  if (event.triggerSource == "UserMigration_Authentication") {
    const user = await authenticateUser(event.userName, event.request.password);

    if (user) {
      const userAttributes = user.UserAttributes.reduce((acc, { Name, Value }) => {
                acc[Name] = Value;
                return acc;
            }, {});
      
      userAttributes['username'] = event.userName.split('@')[0]
      delete userAttributes.sub
      
      event.response.userAttributes = userAttributes
      event.response.finalUserStatus = "CONFIRMED";
      event.response.messageAction = "SUPPRESS";
    }
  } else if (event.triggerSource == "UserMigration_ForgotPassword") {
    // Look up the user in your existing user directory service
    const user = await lookupUser(event.userName);
    if (user) {
      
       const userAttributes = user.UserAttributes.reduce((acc, { Name, Value }) => {
                acc[Name] = Value;
                return acc;
            }, {});
            
       userAttributes['username'] = event.userName.split('@')[0]
       delete userAttributes.sub
       event.response.messageAction = "SUPPRESS";
    }
  }
  
  return event;
};

© www.soinside.com 2019 - 2024. All rights reserved.