UNPKG

@nestlab/google-recaptcha

Version:
74 lines (73 loc) 4.55 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GoogleRecaptchaGuard = void 0; const common_1 = require("@nestjs/common"); const provider_declarations_1 = require("../provider.declarations"); const google_recaptcha_exception_1 = require("../exceptions/google-recaptcha.exception"); const core_1 = require("@nestjs/core"); const recaptcha_request_resolver_1 = require("../services/recaptcha-request.resolver"); const recaptcha_validator_resolver_1 = require("../services/recaptcha-validator.resolver"); const google_recaptcha_context_1 = require("../enums/google-recaptcha-context"); const google_recaptcha_enterprise_validator_1 = require("../services/validators/google-recaptcha-enterprise.validator"); const recaptcha_config_ref_1 = require("../models/recaptcha-config-ref"); let GoogleRecaptchaGuard = class GoogleRecaptchaGuard { constructor(reflector, requestResolver, validatorResolver, logger, configRef) { this.reflector = reflector; this.requestResolver = requestResolver; this.validatorResolver = validatorResolver; this.logger = logger; this.configRef = configRef; } async canActivate(context) { const request = this.requestResolver.resolve(context); const skipIfValue = this.configRef.valueOf.skipIf; const skip = typeof skipIfValue === 'function' ? await skipIfValue(request) : !!skipIfValue; if (skip) { return true; } const options = this.reflector.get(provider_declarations_1.RECAPTCHA_VALIDATION_OPTIONS, context.getHandler()); const [response, remoteIp] = await Promise.all([ (options === null || options === void 0 ? void 0 : options.response) ? await options.response(request) : await this.configRef.valueOf.response(request), (options === null || options === void 0 ? void 0 : options.remoteIp) ? await options.remoteIp(request) : await this.configRef.valueOf.remoteIp && this.configRef.valueOf.remoteIp(request), ]); const score = (options === null || options === void 0 ? void 0 : options.score) || this.configRef.valueOf.score; const action = options === null || options === void 0 ? void 0 : options.action; const validator = this.validatorResolver.resolve(); request.recaptchaValidationResult = await validator.validate({ response, remoteIp, score, action }); if (this.configRef.valueOf.debug) { const loggerCtx = this.resolveLogContext(validator); this.logger.debug(request.recaptchaValidationResult.toObject(), `${loggerCtx}.result`); } if (request.recaptchaValidationResult.success) { return true; } throw new google_recaptcha_exception_1.GoogleRecaptchaException(request.recaptchaValidationResult.errors); } resolveLogContext(validator) { return validator instanceof google_recaptcha_enterprise_validator_1.GoogleRecaptchaEnterpriseValidator ? google_recaptcha_context_1.GoogleRecaptchaContext.GoogleRecaptchaEnterprise : google_recaptcha_context_1.GoogleRecaptchaContext.GoogleRecaptcha; } }; GoogleRecaptchaGuard = __decorate([ (0, common_1.Injectable)(), __param(3, (0, common_1.Inject)(provider_declarations_1.RECAPTCHA_LOGGER)), __metadata("design:paramtypes", [core_1.Reflector, recaptcha_request_resolver_1.RecaptchaRequestResolver, recaptcha_validator_resolver_1.RecaptchaValidatorResolver, common_1.Logger, recaptcha_config_ref_1.RecaptchaConfigRef]) ], GoogleRecaptchaGuard); exports.GoogleRecaptchaGuard = GoogleRecaptchaGuard;