GH-3546 Recaptcha on login form (#4626)

## Description

This PR adds recaptcha on login form. One can add any one of three
recaptcha vendor -
1. Google Recaptcha -
https://developers.google.com/recaptcha/docs/v3#programmatically_invoke_the_challenge
2. HCaptcha -
https://docs.hcaptcha.com/invisible#programmatically-invoke-the-challenge
3. Turnstile -
https://developers.cloudflare.com/turnstile/get-started/client-side-rendering/#execution-modes

### Issue
- #3546 

### Environment variables - 
1. `CAPTCHA_DRIVER` - `google-recaptcha` | `hcaptcha` | `turnstile`
2. `CAPTCHA_SITE_KEY` - site key
3. `CAPTCHA_SECRET_KEY` - secret key

### Engineering choices
1. If some of the above env variable provided, then, backend generates
an error -
<img width="990" alt="image"
src="https://github.com/twentyhq/twenty/assets/60139930/9fb00fab-9261-4ff3-b23e-2c2e06f1bf89">
    Please note that login/signup form will keep working as expected.
2. I'm using a Captcha guard that intercepts the request. If
"captchaToken" is present in the body and all env is set, then, the
captcha token is verified by backend through the service.
3. One can use this guard on any resolver to protect it by the captcha.
4. On frontend, two hooks `useGenerateCaptchaToken` and
`useInsertCaptchaScript` is created. `useInsertCaptchaScript` adds the
respective captcha JS script on frontend. `useGenerateCaptchaToken`
returns a function that one can use to trigger captcha token generation
programatically. This allows one to generate token keeping recaptcha
invisible.

### Note
This PR contains some changes in unrelated files like indentation,
spacing, inverted comma etc. I ran "yarn nx fmt:fix twenty-front" and
"yarn nx lint twenty-front -- --fix".

### Screenshots

<img width="869" alt="image"
src="https://github.com/twentyhq/twenty/assets/60139930/a75f5677-9b66-47f7-9730-4ec916073f8c">

---------

Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Deepak Kumar
2024-04-26 03:22:28 +05:30
committed by GitHub
parent 44855f0317
commit dc576d0818
46 changed files with 737 additions and 71 deletions

View File

@ -1,10 +1,12 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { CanActivate } from '@nestjs/common';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { UserService } from 'src/engine/core-modules/user/services/user.service';
import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service';
import { User } from 'src/engine/core-modules/user/user.entity';
import { CaptchaGuard } from 'src/engine/integrations/captcha/captcha.guard';
import { AuthResolver } from './auth.resolver';
@ -13,6 +15,7 @@ import { AuthService } from './services/auth.service';
describe('AuthResolver', () => {
let resolver: AuthResolver;
const mock_CaptchaGuard: CanActivate = { canActivate: jest.fn(() => true) };
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
@ -43,7 +46,10 @@ describe('AuthResolver', () => {
useValue: {},
},
],
}).compile();
})
.overrideGuard(CaptchaGuard)
.useValue(mock_CaptchaGuard)
.compile();
resolver = module.get<AuthResolver>(AuthResolver);
});

View File

@ -32,6 +32,7 @@ import { AuthorizeApp } from 'src/engine/core-modules/auth/dto/authorize-app.ent
import { AuthorizeAppInput } from 'src/engine/core-modules/auth/dto/authorize-app.input';
import { ExchangeAuthCodeInput } from 'src/engine/core-modules/auth/dto/exchange-auth-code.input';
import { ExchangeAuthCode } from 'src/engine/core-modules/auth/dto/exchange-auth-code.entity';
import { CaptchaGuard } from 'src/engine/integrations/captcha/captcha.guard';
import { ApiKeyToken, AuthTokens } from './dto/token.entity';
import { TokenService } from './services/token.service';
@ -58,6 +59,7 @@ export class AuthResolver {
private userWorkspaceService: UserWorkspaceService,
) {}
@UseGuards(CaptchaGuard)
@Query(() => UserExists)
async checkUserExists(
@Args() checkUserExistsInput: CheckUserExistsInput,
@ -87,6 +89,7 @@ export class AuthResolver {
});
}
@UseGuards(CaptchaGuard)
@Mutation(() => LoginToken)
async challenge(@Args() challengeInput: ChallengeInput): Promise<LoginToken> {
const user = await this.authService.challenge(challengeInput);
@ -95,6 +98,7 @@ export class AuthResolver {
return { loginToken };
}
@UseGuards(CaptchaGuard)
@Mutation(() => LoginToken)
async signUp(@Args() signUpInput: SignUpInput): Promise<LoginToken> {
const user = await this.authService.signInUp({

View File

@ -1,6 +1,6 @@
import { ArgsType, Field } from '@nestjs/graphql';
import { IsEmail, IsNotEmpty, IsString } from 'class-validator';
import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
@ArgsType()
export class ChallengeInput {
@ -13,4 +13,9 @@ export class ChallengeInput {
@IsNotEmpty()
@IsString()
password: string;
@Field(() => String, { nullable: true })
@IsString()
@IsOptional()
captchaToken?: string;
}

View File

@ -18,4 +18,9 @@ export class SignUpInput {
@IsString()
@IsOptional()
workspaceInviteHash?: string;
@Field(() => String, { nullable: true })
@IsString()
@IsOptional()
captchaToken?: string;
}

View File

@ -1,6 +1,6 @@
import { ArgsType, Field } from '@nestjs/graphql';
import { IsNotEmpty, IsString } from 'class-validator';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
@ArgsType()
export class CheckUserExistsInput {
@ -8,4 +8,9 @@ export class CheckUserExistsInput {
@IsString()
@IsNotEmpty()
email: string;
@Field(() => String, { nullable: true })
@IsString()
@IsOptional()
captchaToken?: string;
}

View File

@ -1,5 +1,7 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { CaptchaDriverType } from 'src/engine/integrations/captcha/interfaces';
@ObjectType()
class AuthProviders {
@Field(() => Boolean)
@ -57,6 +59,15 @@ class Sentry {
dsn?: string;
}
@ObjectType()
class Captcha {
@Field(() => CaptchaDriverType, { nullable: true })
provider: CaptchaDriverType | undefined;
@Field(() => String, { nullable: true })
siteKey: string | undefined;
}
@ObjectType()
export class ClientConfig {
@Field(() => AuthProviders, { nullable: false })
@ -82,4 +93,7 @@ export class ClientConfig {
@Field(() => Sentry)
sentry: Sentry;
@Field(() => Captcha)
captcha: Captcha;
}

View File

@ -44,6 +44,10 @@ export class ClientConfigResolver {
release: this.environmentService.get('SENTRY_RELEASE'),
dsn: this.environmentService.get('SENTRY_FRONT_DSN'),
},
captcha: {
provider: this.environmentService.get('CAPTCHA_DRIVER'),
siteKey: this.environmentService.get('CAPTCHA_SITE_KEY'),
},
};
return Promise.resolve(clientConfig);