Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS-Cognito bot prevention with google reCaptcha

My problem is the POST-Request if the user is a bot or human.

It's not possible to send the request form Client-side, otherwise u will get an error on the OPTIONS request:" (response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource)", that's because the request is only possible from Server-Side.

In AWS-Cognito there is a way to use a pre authentication function to check something like that, but I couldn't find a way to get my response element into the function.

So my question is: Is there a way to implement Google recaptcha on AWS Cognito?

like image 293
Fabian_schoen Avatar asked Aug 07 '17 10:08

Fabian_schoen


People also ask

How do I authenticate someone on Cognito?

Go to AWS Cognito service and click “Manage Identity Pools”. 2. Enter “Identity pool name”, expand the “Authentication providers” section and select “Cognito” tab. This is where the Cognito authentication provider will be registered with the Identity pool.

Can AWS Cognito be used for authorization?

Amazon Cognito allows you to use groups to create a collection of users, which is often done to set the permissions for those users. In this post, I show you how to build fine-grained authorization to protect your APIs using Amazon Cognito, API Gateway, and AWS Identity and Access Management (IAM).

Is Cognito secure?

Cognito Forms supports full encryption of all entry data and uploaded files at rest. Additionally, you can mark sensitive form fields as protected.

Is AWS Cognito any good?

AWS Cognito: A good solution for a B2C web/and mobile apps having simple Authentication needs. Not for B2B SaaS use-cases. We use it for Authentication and Authorization of the mobile applications, and middleware that we write for our enterprise customers.


1 Answers

You can send this as validationData in the signup request and perform the recaptcha verify logic in a trigger SNS Lambda

Here is a snippet using the AWS Amplify library, excuse the typescript:

Client

class AuthService {
  ...

  public signUp(
    emailAddress: string,
    phoneNumber: string,
    password: string,
    recaptchaToken: string
  ): Observable<ISignUpResult> {

    const recaptchaTokenAttributeData: ICognitoUserAttributeData = {
      Name: 'recaptchaToken',
      Value: recaptchaToken
    };

    const signupParams: SignUpParams = {
      username: emailAddress,
      password,
      attributes: {
        'email': emailAddress,
        'phone_number': phoneNumber
      },
      validationData: [
        new CognitoUserAttribute(recaptchaTokenAttributeData)
      ]
    };

    return fromPromise(Auth.signUp(signupParams));
  }

  ...
}

Cognito trigger on PreSignUp SNS Lambda code

export async function validateHuman(
  event: CognitoUserPoolTriggerEvent, 
  context: Context, 
  callback: Callback
): Promise<CognitoUserPoolTriggerHandler> {

  try {
    const recaptchaToken: string = event.request.validationData.recaptchaToken;

    console.log(recaptchaToken);

    const isHuman: boolean = await googleRecaptcha.verify({response: recaptchaToken}, (error: Error) => {
      if (error) {
        console.error(error);

        return false;
      }

      return true;
    });

    if (!isHuman) {
      throw new Error('Not human');
    }

    callback(null, event);

    return;
  } catch (error) {
    console.error(error);
    callback(null, new Response(INTERNAL_SERVER_ERROR, {message: 'Something went wrong'}));

    return;
  }
}
like image 59
Matt Rowles Avatar answered Sep 28 '22 00:09

Matt Rowles