Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nestlab/google-recaptcha

Package Overview
Dependencies
Maintainers
1
Versions
72
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestlab/google-recaptcha - npm Package Compare versions

Comparing version 1.0.11 to 1.0.12

enums/error-code.d.ts

4

guards/google-recaptcha.guard.d.ts

@@ -1,4 +0,5 @@

import { CanActivate, ExecutionContext } from '@nestjs/common';
import { CanActivate, ExecutionContext, HttpException } from '@nestjs/common';
import { GoogleRecaptchaValidator } from '../services/google-recaptcha.validator';
import { GoogleRecaptchaGuardOptions } from '../interfaces/google-recaptcha-guard-options';
import { ErrorCode } from '../enums/error-code';
export declare class GoogleRecaptchaGuard implements CanActivate {

@@ -9,2 +10,3 @@ private readonly validator;

canActivate(context: ExecutionContext): Promise<true | never>;
errorHandler(errorCodes: ErrorCode[]): string | HttpException;
}

@@ -19,2 +19,3 @@ "use strict";

const provider_declarations_1 = require("../provider.declarations");
const error_code_1 = require("../enums/error-code");
let GoogleRecaptchaGuard = class GoogleRecaptchaGuard {

@@ -27,3 +28,3 @@ constructor(validator, options) {

const request = context.switchToHttp().getRequest();
const skip = this.options.skipIf ? await this.options.skipIf(request) : false;
const skip = this.options.skipIf ? await this.options.skipIf() : false;
if (skip) {

@@ -34,10 +35,33 @@ return true;

const result = await this.validator.validate(response);
if (result) {
if (result.success) {
return true;
}
if (this.options.onError) {
this.options.onError();
const error = this.options.onError
? this.options.onError(result.errors)
: this.errorHandler(result.errors);
if (error instanceof Error) {
throw error;
}
throw new common_1.ForbiddenException('Invalid recaptcha.');
throw new common_1.BadRequestException(error);
}
errorHandler(errorCodes) {
const first = errorCodes.shift();
switch (first) {
case error_code_1.ErrorCode.MissingInputSecret:
return 'The secret parameter is missing.';
case error_code_1.ErrorCode.InvalidInputSecret:
return 'The secret parameter is invalid or malformed.';
case error_code_1.ErrorCode.MissingInputResponse:
return 'The response parameter is missing.';
case error_code_1.ErrorCode.InvalidInputResponse:
return 'The response parameter is invalid or malformed.';
case error_code_1.ErrorCode.BadRequest:
return 'The request is invalid or malformed.';
case error_code_1.ErrorCode.TimeoutOrDuplicate:
return 'The response is no longer valid: either is too old or has been used previously.';
case error_code_1.ErrorCode.UnknownError:
default:
return new common_1.BadGatewayException('Unknown error when checking captcha.');
}
}
};

@@ -44,0 +68,0 @@ GoogleRecaptchaGuard = __decorate([

@@ -0,5 +1,6 @@

import { ErrorHandler } from '../types';
export interface GoogleRecaptchaGuardOptions {
response: (req: any) => string | Promise<string>;
skipIf?: (req: any) => boolean | Promise<boolean>;
onError?: () => never;
skipIf?: () => boolean | Promise<boolean>;
onError?: ErrorHandler;
}

@@ -0,4 +1,5 @@

import { ErrorHandler } from '../types';
export interface GoogleRecaptchaValidatorOptions {
secretKey: string;
onError?: () => never;
onError?: ErrorHandler;
}
{
"name": "@nestlab/google-recaptcha",
"version": "1.0.11",
"version": "1.0.12",
"description": "Google recaptcha module for NestJS.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -18,6 +18,3 @@ # Google recaptcha module

response: req => req.headers.recaptcha,
skipIf: req => process.env.NODE_ENV !== 'production',
onError: () => {
throw new BadRequestException('Invalid recaptcha.')
}
skipIf: () => process.env.NODE_ENV !== 'production',
})

@@ -45,2 +42,35 @@ ],

### Error handling
Example error customization. You can return or throw HttpException or return string.
If you will return string, then response will have status code 400 (Bad request).
```typescript
GoogleRecaptchaModule.forRoot({
secretKey: process.env.GOOGLE_RECAPTCHA_SECRET_KEY,
response: req => req.headers.recaptcha,
skipIf: () => process.env.NODE_ENV !== 'production',
onError: (errorCodes: ErrorCode[]) => {
switch (errorCodes.shift()) {
case ErrorCode.MissingInputSecret:
return 'The secret parameter is missing.';
case ErrorCode.InvalidInputSecret:
return 'The secret parameter is invalid or malformed.';
case ErrorCode.MissingInputResponse:
return 'The response parameter is missing.';
case ErrorCode.InvalidInputResponse:
return 'The response parameter is invalid or malformed.';
case ErrorCode.BadRequest:
return 'The request is invalid or malformed.';
case ErrorCode.TimeoutOrDuplicate:
return 'The response is no longer valid: either is too old or has been used previously.';
case ErrorCode.UnknownError:
default:
return new BadGatewayException('Unknown error when checking captcha.');
}
},
})
```
Enjoy!
import { HttpService } from '@nestjs/common';
import { GoogleRecaptchaValidatorOptions } from '../interfaces/google-recaptcha-validator-options';
import { GoogleRecaptchaValidationResult } from '../interfaces/google-recaptcha-validation-result';
export declare class GoogleRecaptchaValidator {

@@ -9,3 +10,3 @@ private readonly http;

constructor(http: HttpService, options: GoogleRecaptchaValidatorOptions);
validate(response: string): Promise<boolean>;
validate(response: string): Promise<GoogleRecaptchaValidationResult>;
}

@@ -19,2 +19,3 @@ "use strict";

const qs = require("querystring");
const error_code_1 = require("../enums/error-code");
let GoogleRecaptchaValidator = class GoogleRecaptchaValidator {

@@ -32,9 +33,10 @@ constructor(http, options) {

.then(res => res.data)
.then(result => result.success)
.catch(() => {
if (this.options.onError) {
return this.options.onError();
}
throw new common_1.InternalServerErrorException('Failed recaptcha verification.');
});
.then(result => ({
success: result.success,
errors: result['error-codes'] || [],
}))
.catch(() => ({
success: false,
errors: [error_code_1.ErrorCode.UnknownError],
}));
}

@@ -41,0 +43,0 @@ };

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc