Skip to content

Commit

Permalink
Merge pull request #6 from chvarkov/develop
Browse files Browse the repository at this point in the history
Added override ability default recaptcha property.
  • Loading branch information
chvarkov authored Oct 24, 2020
2 parents d0d98e0 + c5541a1 commit f68ed45
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ export class FeedbackController {

```

You can override default property that contain recaptcha for specific endpoint.

```typescript

@Controller('feedback')
export class FeedbackController {
@Recaptcha(req => req.body.recaptha)
@Post('send')
async send(): Promise<any> {
// TODO: Your implementation.
}
}

```

If you want use google recaptcha guard in combination with another guards then you can use `@UseGuards` decorator.

```typescript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestlab/google-recaptcha",
"version": "1.1.1",
"version": "1.1.2",
"description": "Google recaptcha module for NestJS.",
"keywords": [
"nest",
Expand Down
11 changes: 8 additions & 3 deletions src/decorators/recaptcha.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { UseGuards } from '@nestjs/common';
import { applyDecorators, SetMetadata, UseGuards } from '@nestjs/common';
import { GoogleRecaptchaGuard } from '../guards/google-recaptcha.guard';
import { RecaptchaResponseProvider } from '../types';
import { RECAPTCHA_RESPONSE_PROVIDER } from '../provider.declarations';

export function Recaptcha(): MethodDecorator {
return UseGuards(GoogleRecaptchaGuard);
export function Recaptcha(response?: RecaptchaResponseProvider): MethodDecorator {
return applyDecorators(
SetMetadata(RECAPTCHA_RESPONSE_PROVIDER, response),
UseGuards(GoogleRecaptchaGuard),
);
}
10 changes: 8 additions & 2 deletions src/guards/google-recaptcha.guard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { CanActivate, ExecutionContext, Inject, Injectable } from '@nestjs/common';
import { GoogleRecaptchaValidator } from '../services/google-recaptcha.validator';
import { GoogleRecaptchaGuardOptions } from '../interfaces/google-recaptcha-guard-options';
import { RECAPTCHA_OPTIONS } from '../provider.declarations';
import { RECAPTCHA_OPTIONS, RECAPTCHA_RESPONSE_PROVIDER } from '../provider.declarations';
import { GoogleRecaptchaException } from '../exceptions/google-recaptcha.exception';
import { Reflector } from '@nestjs/core';

@Injectable()
export class GoogleRecaptchaGuard implements CanActivate {
constructor(private readonly validator: GoogleRecaptchaValidator,
private readonly reflector: Reflector,
@Inject(RECAPTCHA_OPTIONS) private readonly options: GoogleRecaptchaGuardOptions) {
}

Expand All @@ -19,7 +21,11 @@ export class GoogleRecaptchaGuard implements CanActivate {
return true;
}

const response = await this.options.response(request);
const provider = this.reflector.get(RECAPTCHA_RESPONSE_PROVIDER, context.getHandler());

const response = provider
? await provider(request)
: await this.options.response(request);

const result = await this.validator.validate(response);

Expand Down
4 changes: 3 additions & 1 deletion src/interfaces/google-recaptcha-guard-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { RecaptchaResponseProvider } from '../types';

export interface GoogleRecaptchaGuardOptions {
response: (req) => string | Promise<string>;
response: RecaptchaResponseProvider;
skipIf?: () => boolean | Promise<boolean>;
}
2 changes: 2 additions & 0 deletions src/provider.declarations.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const RECAPTCHA_OPTIONS = Symbol('RECAPTCHA_OPTIONS');

export const RECAPTCHA_RESPONSE_PROVIDER = Symbol('RECAPTCHA_RESPONSE_PROVIDER');
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type RecaptchaResponseProvider = (req) => string | Promise<string>;

0 comments on commit f68ed45

Please sign in to comment.