📔
ServerlessDocs - Serverless File Service
  • Introduction
  • Why Serverless!!
  • Prerequisites
  • User Management
    • Authentication
    • Amazon Cognito
    • Create the User Pool
    • Create an App Client
    • Integrate the app with Cognito User Pool
    • Create the Identity Pool
    • Validate the setup
    • Troubleshooting
  • Manage Documents with AWS S3
    • Authorization
    • Update Cognito Identity Pool Auth Role
    • S3 Documents Bucket Folder Structure
    • Validate the Access Permissions
    • Public and Private files access
    • Upload Files to S3
    • Download Files
    • Delete Files
    • Share Files
  • More Security Configurations
    • Cognito user emails
    • Password policies
    • Allow only Organization and Whitelisted users to Signup
    • Allow admin users to upload Public files
    • Notify Admin users when a new user signup
    • Enable Multi-factor Authentication (MFA)
  • User Operations
    • Password Reset / Forgot Password
    • Resend Verification Code
  • Serverless APIs
    • APIs
    • Secure APIs using API Gateway Authorizer
    • Access Cognito values in Lambda function
    • Authorize APIs with OAuth 2 Scope
    • Fine-grained Access with AssumeRole
    • Notify Admin users on large file uploads
    • Generate a Month-To-Date Usage report
  • Source Code and Setup
    • Source Code
    • IAM Policies
    • S3 Bucket Policies
    • Suggestions / Feedback
    • More References
  • Deployment
    • AWS SAM
    • Serverless Framework
  • Contributors
    • Team
Powered by GitBook
On this page

Was this helpful?

  1. More Security Configurations

Allow only Organization and Whitelisted users to Signup

We are building an enterprise document management system. So we only want to allow only users who belong to our organization or whitelisted emails.

PreviousPassword policiesNextAllow admin users to upload Public files

Last updated 4 years ago

Was this helpful?

We will use Cognito Pre-Signup Hook to validate the email address and accordingly allow users to continue.

Here is the Lambda code which does the validation.

const whitelistedDomains = ['appgambit.com'];
const whitelistedEmails = [];

exports.handler = async (event, context, callback) => {    
    // Split the email address so we can compare domains
    const userEmail = event.request.userAttributes.email;
    const userDomain = userEmail.split("@")[1];
    
    console.log(`Validating domain ${userDomain} and email ${userEmail}`);
    if(whitelistedEmails.indexOf(userEmail) < 0){
        if (whitelistedDomains.indexOf(userDomain) < 0) {
            throw new Error('EMAIL_DOMAIN_ERR')
        }
    }

    // Return to Amazon Cognito
    return event;
};

You can configure your Cognito pool to use the above lambda function.

Now if you try to use an email that is not in the whitelist, you should see an error message. I am returning an error code EMAIL_DOMAIN_ERR and showing the proper message on the UI side.

At this point, the whole application will only accept only whitelisted signups. So this is now ready to use and share with your users.

Cognito Pre-signup Trigger
Cognito Pre sign-up Lambda Trigger