AWS Cognito username/email 登录区分大小写

AWS Cognito username/email login is case-sensitive

设置

我正在使用 AWS Cognito 来管理我的 Web 应用程序的用户注册和用户访问。具体来说,我正在使用托管的 Cognito UI。这意味着 Cognito 为我的用户提供 UI 注册,我无权修改我的应用程序的用户注册或登录页面(Cognito 提供的控件除外)。我使用电子邮件地址作为用户名,因此新用户只需提供电子邮件地址和密码即可。

问题

Cognito 将电子邮件地址视为区分大小写。如果用户使用电子邮件地址 JOHN_smith@randommail.com 注册,则他们无法使用 john_smith@randommail.com.

登录

我希望用于注册和登录的用户电子邮件地址不区分大小写。

我试过的

通常情况下,在将电子邮件地址发送到服务器之前,将客户端中的电子邮件地址设置为小写即可轻松处理。但是我无法访问客户端 UI,因为它由 Cognito 托管。

因此,我的计划是尝试使用由 Cognito pre-signup trigger 调用的 Lambda 函数来小写用户提供的电子邮件。

Pre sign-up

Amazon Cognito invokes this trigger when a user attempts to register (sign up), allowing you to perform custom validation to accept or deny the registration request.

这是我写的lamdba函数:

'use strict';

console.log('Loading function');

exports.handler = (event, context, callback) => {
    console.log('Received event:', JSON.stringify(event, null, 2));

    var triggerSource = event.triggerSource;
    console.log('Received triggerSource:', triggerSource);

    var email = event.request.userAttributes.email;
    console.log('Received email:', email);

    var modifiedEvent = event;

    if (email !== null) {
        var lowerEmail = email.toLowerCase();
        modifiedEvent.request.userAttributes.email = lowerEmail;
        console.log('Set email in request to', lowerEmail);
        console.log('Modified event:', JSON.stringify(modifiedEvent, null, 2));
    } else {
        console.log('Email evaluated as NULL, exiting with no action');
    }

    // Return result to Cognito
    callback(null, modifiedEvent);
};

这个'worked'是指事件请求中的电子邮件地址被修改为小写(john_smith@randommail.com)。但是,当我的 Lambda 函数收到此事件时,该帐户似乎已经在用户池中创建。更改请求中的电子邮件地址没有任何效果 - 原始电子邮件地址 (JOHN_smith@randommail.com) 仍然出现在我的用户池中。我怀疑事件中唯一有影响的字段是响应字段。这是我修改后的事件的样子:

{
    "version": "1",
    "region": "us-east-1",
    "userPoolId": "us-east-1_xxxxxxx",
    "userName": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxx",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-java-console",
        "clientId": "xxxxxxxxxxxxxxxxxxxxxx"
    },
    "triggerSource": "PreSignUp_SignUp",
    "request": {
        "userAttributes": {
            "email": "john_smith@randommail.com"
        },
        "validationData": null
    },
    "response": {
        "autoConfirmUser": false,
        "autoVerifyEmail": false,
        "autoVerifyPhone": false
    }
}

我的问题

我正在寻找使我的用户注册和登录不区分大小写的想法或示例。这可能包括对我的 lambda 触发器方法的更改或完全其他内容。

请注意,我知道我可以实现我自己的 UI,我只会在万不得已时才这样做。

另一种可能性是,如果您可以创建用户注册 api,这将在 Cognito 中创建用户。在修剪电子邮件以及对电子邮件执行小写后。您可以在此之前执行自定义步骤。

你是对的,预注册触发器基本上是在执行注册后调用的。

https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-sign-up.html#aws-lambda-triggers-pre-registration-tutorials

您可以在 sign-up 之后触发 Lambda 函数以将电子邮件更改为小写。无需实际测试,您应该能够 trigger a Lambda post confirmation. That Lambda could use AdminUpdateUserAttributes API 从您选择的 SDK 调用,将电子邮件更改为小写。

请注意,用户名也区分大小写。

由于用户名区分大小写:为什么不直接使用用户名作为电子邮件地址,并让预挂钩使用用户名填充电子邮件字段?

已修复新用户池。您现在可以关闭区分大小写。

https://aws.amazon.com/about-aws/whats-new/2020/02/amazon-cognito-user-pools-service-now-supports-case-insensitivity-for-user-aliases/

创建帐户时将用户名设为小写。登录时将用户名设为小写。至少这样您只需重新创建具有大写字符的用户。或者您可以重建整个用户池并迁移用户,但谁知道这会对密码和 MFA 产生什么影响。现有用户池似乎不值得麻烦。希望你正在做一个全新的项目,你可以 select 创建用户池时不区分大小写的选项。