From ea3b72e6b6812a75f0ba7bcdabef55a2a0885bcb Mon Sep 17 00:00:00 2001 From: chvarkov Date: Sat, 17 Apr 2021 14:27:07 +0300 Subject: [PATCH] Handled invalid api key error. --- package.json | 2 +- src/enums/error-code.ts | 1 + src/exceptions/google-recaptcha.exception.ts | 10 +++++++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index eb9400c..7452633 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nestlab/google-recaptcha", - "version": "2.0.1", + "version": "2.0.2", "description": "Google recaptcha module for NestJS.", "keywords": [ "nest", diff --git a/src/enums/error-code.ts b/src/enums/error-code.ts index 9fbcce3..a6ea4b5 100644 --- a/src/enums/error-code.ts +++ b/src/enums/error-code.ts @@ -8,4 +8,5 @@ export enum ErrorCode { UnknownError = 'unknown-error', ForbiddenAction = 'forbidden-action', LowScore = 'low-score', + InvalidKeys = 'invalid-keys', } diff --git a/src/exceptions/google-recaptcha.exception.ts b/src/exceptions/google-recaptcha.exception.ts index 8c21d86..198465d 100644 --- a/src/exceptions/google-recaptcha.exception.ts +++ b/src/exceptions/google-recaptcha.exception.ts @@ -1,5 +1,5 @@ -import { HttpException, HttpStatus } from '@nestjs/common'; -import { ErrorCode } from '../enums/error-code'; +import {HttpException, HttpStatus} from '@nestjs/common'; +import {ErrorCode} from '../enums/error-code'; export class GoogleRecaptchaException extends HttpException { @@ -21,6 +21,9 @@ export class GoogleRecaptchaException extends HttpException { case ErrorCode.MissingInputSecret: return 'Invalid module configuration. Please check public-secret keys.'; + case ErrorCode.InvalidKeys: + return 'Recaptcha token was signed by invalid api key.'; + case ErrorCode.LowScore: return 'Low recaptcha score.' @@ -39,7 +42,8 @@ export class GoogleRecaptchaException extends HttpException { errorCode === ErrorCode.MissingInputResponse || errorCode === ErrorCode.TimeoutOrDuplicate || errorCode === ErrorCode.ForbiddenAction || - errorCode === ErrorCode.LowScore + errorCode === ErrorCode.LowScore || + errorCode === ErrorCode.InvalidKeys ? HttpStatus.BAD_REQUEST : HttpStatus.INTERNAL_SERVER_ERROR; }