Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cognito username/email login is case-sensitive

Setup

I am using AWS Cognito to manage the user registration and user access for my web application. Specifically I am using the Cognito hosted UI. This means that Cognito presents a UI for my users to register, I do not have access to modify the user sign-up or login pages for my application (other than the controls provided by Cognito). I am using email addresses as usernames, so new users are simply asked to provide an email address and password.

enter image description here

Problem

Cognito treats email addresses as case sensitive. If a user signs up with the email address [email protected], they cannot then sign in using [email protected].

I want user email addresses for sign-up and login to be case insensitive.

What I have tried

Usually this would be trivial to deal with by setting the email address to the lowercase in the client before sending it to the server. However I do not have access to the client UI as it is hosted by Cognito.

My plan therefore was to try using a Lambda function invoked by a Cognito pre-signup trigger to lowercase the email supplied by the user.

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.

Here is the lamdba function I wrote:

'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);
};

This 'worked' in the sense that the email address in the event request was modified to be lowercase ([email protected]). However, it seems the account has already been created in the userpool by the time my Lambda function receives this event. Changing the email address in the request had no effect - the original email address ([email protected]) still appears in my user pool. I suspect the only fields in the event that have any effect are the response fields. Here is what my modified event looks like:

{
    "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": "[email protected]"
        },
        "validationData": null
    },
    "response": {
        "autoConfirmUser": false,
        "autoVerifyEmail": false,
        "autoVerifyPhone": false
    }
}

My question

I'm looking for ideas or examples to make my user registration and login case insensitive. This might include changes to my lambda trigger approach or something else entirely.

Please note I know I could implement my own UI, which I will only do as a last resort.

like image 225
F_SO_K Avatar asked Jan 19 '18 13:01

F_SO_K


People also ask

Is AWS Cognito case sensitive?

Amazon Cognito User Pools service now supports case insensitivity for user aliases. Amazon Cognito User Pools now enables customers to configure case sensitivity settings for user aliases, including native username, email alias and preferred username alias.

Is Cognito client ID sensitive?

No, they are not. They are supposed to be public. The only way they can be exploited is that someone can use them to make a large amount of SignUp calls to your userpool.

Is username unique in Cognito?

A username is always required to register a user, and it cannot be changed after a user is created. The username must be unique within a user pool. A username can be reused, but only after it has been deleted and is no longer in use.

How do I verify my email on AWS Cognito?

Amazon Cognito can automatically verify email addresses or phone numbers. To do this verification, Amazon Cognito sends a verification code or a verification link. For email addresses, Amazon Cognito can send a code or a link in an email message. For phone numbers, Amazon Cognito sends a code in an SMS text message.


2 Answers

Fixed on new user pools. You can turn off case sensitivity now.

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

like image 50
Mr. Young Avatar answered Oct 11 '22 16:10

Mr. Young


You could trigger a Lambda function after sign-up to change the email to lowercase. Without actually testing it, you should be able to trigger a Lambda post confirmation. That Lambda could use AdminUpdateUserAttributes API, called from your SDK of choice, to change the email to lowercase.

Note that user names are also case sensitive.

like image 34
bgdnlp Avatar answered Oct 11 '22 15:10

bgdnlp