From 27b72cb7f93ddf39ff4c44b406d92368f13457c5 Mon Sep 17 00:00:00 2001 From: Menci Date: Sat, 31 Oct 2020 01:10:31 +0800 Subject: [PATCH 1/2] Pass request object to skipIf callback for per-reuqest skip checking --- README.md | 5 ++++- src/guards/google-recaptcha.guard.ts | 2 +- src/interfaces/google-recaptcha-guard-options.ts | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 783a50a..4e68056 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,10 @@ $ npm i @nestlab/google-recaptcha GoogleRecaptchaModule.forRoot({ secretKey: process.env.GOOGLE_RECAPTCHA_SECRET_KEY, response: req => req.headers.recaptcha, - skipIf: () => process.env.NODE_ENV !== 'production', + // To check if a request can skip the recaptcha verification + // Useful for involing other check methods (e.g. custom privileged API key) + // Or just skip recaptcha on development + skipIf: async req => process.env.NODE_ENV !== 'production', // If your server has trouble connecting to https://www.google.com // You can use https://recaptcha.net instead // Or use an agent (see proxy-agent NPM module) diff --git a/src/guards/google-recaptcha.guard.ts b/src/guards/google-recaptcha.guard.ts index 2d4098a..7331036 100644 --- a/src/guards/google-recaptcha.guard.ts +++ b/src/guards/google-recaptcha.guard.ts @@ -15,7 +15,7 @@ export class GoogleRecaptchaGuard implements CanActivate { async canActivate(context: ExecutionContext): Promise { const request = context.switchToHttp().getRequest(); - const skip = this.options.skipIf ? await this.options.skipIf() : false; + const skip = this.options.skipIf ? await this.options.skipIf(request) : false; if (skip) { return true; diff --git a/src/interfaces/google-recaptcha-guard-options.ts b/src/interfaces/google-recaptcha-guard-options.ts index 538e6fc..54ceff3 100644 --- a/src/interfaces/google-recaptcha-guard-options.ts +++ b/src/interfaces/google-recaptcha-guard-options.ts @@ -2,5 +2,5 @@ import { RecaptchaResponseProvider } from '../types'; export interface GoogleRecaptchaGuardOptions { response: RecaptchaResponseProvider; - skipIf?: () => boolean | Promise; + skipIf?: (request: any) => boolean | Promise; } From 4ab6f861334fdd87e90616c8f8f53fd5d40729eb Mon Sep 17 00:00:00 2001 From: chvarkov Date: Fri, 30 Oct 2020 20:32:29 +0300 Subject: [PATCH 2/2] Updated package version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4096f3b..bbb545a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nestlab/google-recaptcha", - "version": "1.1.4", + "version": "1.1.5", "description": "Google recaptcha module for NestJS.", "keywords": [ "nest",