@mondart/nestjs-common-module 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/dist/lib/google-recaptcha/decorators/recaptcha-result.d.ts +1 -0
  2. package/dist/lib/google-recaptcha/decorators/recaptcha-result.js +16 -0
  3. package/dist/lib/google-recaptcha/decorators/recaptcha.d.ts +2 -0
  4. package/dist/lib/google-recaptcha/decorators/recaptcha.js +9 -0
  5. package/dist/lib/google-recaptcha/decorators/set-recaptcha-options.d.ts +2 -0
  6. package/dist/lib/google-recaptcha/decorators/set-recaptcha-options.js +8 -0
  7. package/dist/lib/google-recaptcha/enums/classification-reason.d.ts +8 -0
  8. package/dist/lib/google-recaptcha/enums/classification-reason.js +12 -0
  9. package/dist/lib/google-recaptcha/enums/error-code.d.ts +16 -0
  10. package/dist/lib/google-recaptcha/enums/error-code.js +20 -0
  11. package/dist/lib/google-recaptcha/enums/google-recaptcha-context.d.ts +4 -0
  12. package/dist/lib/google-recaptcha/enums/google-recaptcha-context.js +8 -0
  13. package/dist/lib/google-recaptcha/enums/google-recaptcha-enterprise-reason.d.ts +10 -0
  14. package/dist/lib/google-recaptcha/enums/google-recaptcha-enterprise-reason.js +14 -0
  15. package/dist/lib/google-recaptcha/enums/google-recaptcha-network.d.ts +4 -0
  16. package/dist/lib/google-recaptcha/enums/google-recaptcha-network.js +8 -0
  17. package/dist/lib/google-recaptcha/exceptions/google-recaptcha-network.exception.d.ts +5 -0
  18. package/dist/lib/google-recaptcha/exceptions/google-recaptcha-network.exception.js +14 -0
  19. package/dist/lib/google-recaptcha/exceptions/google-recaptcha.exception.d.ts +8 -0
  20. package/dist/lib/google-recaptcha/exceptions/google-recaptcha.exception.js +54 -0
  21. package/dist/lib/google-recaptcha/google-recaptcha.module.d.ts +12 -0
  22. package/dist/lib/google-recaptcha/google-recaptcha.module.js +159 -0
  23. package/dist/lib/google-recaptcha/guards/google-recaptcha.guard.d.ts +15 -0
  24. package/dist/lib/google-recaptcha/guards/google-recaptcha.guard.js +89 -0
  25. package/dist/lib/google-recaptcha/helpers/get-error-info.d.ts +4 -0
  26. package/dist/lib/google-recaptcha/helpers/get-error-info.js +13 -0
  27. package/dist/lib/google-recaptcha/helpers/load-module.d.ts +2 -0
  28. package/dist/lib/google-recaptcha/helpers/load-module.js +15 -0
  29. package/dist/lib/google-recaptcha/helpers/xor.d.ts +1 -0
  30. package/dist/lib/google-recaptcha/helpers/xor.js +6 -0
  31. package/dist/lib/google-recaptcha/index.d.ts +15 -0
  32. package/dist/lib/google-recaptcha/index.js +31 -0
  33. package/dist/lib/google-recaptcha/interfaces/google-recaptcha-enterprise-options.d.ts +5 -0
  34. package/dist/lib/google-recaptcha/interfaces/google-recaptcha-enterprise-options.js +2 -0
  35. package/dist/lib/google-recaptcha/interfaces/google-recaptcha-guard-options.d.ts +7 -0
  36. package/dist/lib/google-recaptcha/interfaces/google-recaptcha-guard-options.js +2 -0
  37. package/dist/lib/google-recaptcha/interfaces/google-recaptcha-module-options.d.ts +21 -0
  38. package/dist/lib/google-recaptcha/interfaces/google-recaptcha-module-options.js +2 -0
  39. package/dist/lib/google-recaptcha/interfaces/google-recaptcha-validator-options.d.ts +12 -0
  40. package/dist/lib/google-recaptcha/interfaces/google-recaptcha-validator-options.js +2 -0
  41. package/dist/lib/google-recaptcha/interfaces/literal-object.d.ts +3 -0
  42. package/dist/lib/google-recaptcha/interfaces/literal-object.js +2 -0
  43. package/dist/lib/google-recaptcha/interfaces/verify-response-decorator-options.d.ts +13 -0
  44. package/dist/lib/google-recaptcha/interfaces/verify-response-decorator-options.js +2 -0
  45. package/dist/lib/google-recaptcha/interfaces/verify-response-enterprise.d.ts +30 -0
  46. package/dist/lib/google-recaptcha/interfaces/verify-response-enterprise.js +2 -0
  47. package/dist/lib/google-recaptcha/interfaces/verify-response.d.ts +11 -0
  48. package/dist/lib/google-recaptcha/interfaces/verify-response.js +2 -0
  49. package/dist/lib/google-recaptcha/models/recaptcha-config-ref.d.ts +12 -0
  50. package/dist/lib/google-recaptcha/models/recaptcha-config-ref.js +30 -0
  51. package/dist/lib/google-recaptcha/models/recaptcha-verification-result.d.ts +26 -0
  52. package/dist/lib/google-recaptcha/models/recaptcha-verification-result.js +34 -0
  53. package/dist/lib/google-recaptcha/provider.declarations.d.ts +4 -0
  54. package/dist/lib/google-recaptcha/provider.declarations.js +7 -0
  55. package/dist/lib/google-recaptcha/services/enterprise-reason.transformer.d.ts +5 -0
  56. package/dist/lib/google-recaptcha/services/enterprise-reason.transformer.js +38 -0
  57. package/dist/lib/google-recaptcha/services/recaptcha-request.resolver.d.ts +4 -0
  58. package/dist/lib/google-recaptcha/services/recaptcha-request.resolver.js +32 -0
  59. package/dist/lib/google-recaptcha/services/recaptcha-validator.resolver.d.ts +11 -0
  60. package/dist/lib/google-recaptcha/services/recaptcha-validator.resolver.js +40 -0
  61. package/dist/lib/google-recaptcha/services/validators/abstract-google-recaptcha-validator.d.ts +11 -0
  62. package/dist/lib/google-recaptcha/services/validators/abstract-google-recaptcha-validator.js +27 -0
  63. package/dist/lib/google-recaptcha/services/validators/google-recaptcha-enterprise.validator.d.ts +17 -0
  64. package/dist/lib/google-recaptcha/services/validators/google-recaptcha-enterprise.validator.js +126 -0
  65. package/dist/lib/google-recaptcha/services/validators/google-recaptcha.validator.d.ts +17 -0
  66. package/dist/lib/google-recaptcha/services/validators/google-recaptcha.validator.js +128 -0
  67. package/dist/lib/google-recaptcha/types.d.ts +6 -0
  68. package/dist/lib/google-recaptcha/types.js +2 -0
  69. package/dist/lib/health-check/health-check.controller.d.ts +5 -5
  70. package/dist/lib/index.d.ts +1 -1
  71. package/dist/lib/index.js +1 -1
  72. package/dist/tsconfig.tsbuildinfo +1 -1
  73. package/package.json +3 -3
  74. package/.github/workflows/npm-publish.yml +0 -34
  75. package/.github/workflows/npm-unpublish.yml +0 -35
  76. package/dist/lib/captcha/captcha.decorator.d.ts +0 -4
  77. package/dist/lib/captcha/captcha.decorator.js +0 -7
  78. package/dist/lib/captcha/captcha.exception.d.ts +0 -4
  79. package/dist/lib/captcha/captcha.exception.js +0 -11
  80. package/dist/lib/captcha/captcha.guard.d.ts +0 -16
  81. package/dist/lib/captcha/captcha.guard.js +0 -86
  82. package/dist/lib/captcha/captcha.interface.d.ts +0 -26
  83. package/dist/lib/captcha/captcha.interface.js +0 -4
  84. package/dist/lib/captcha/captcha.module.d.ts +0 -8
  85. package/dist/lib/captcha/captcha.module.js +0 -97
  86. package/dist/lib/captcha/enums/captcha-decorators.enum.d.ts +0 -3
  87. package/dist/lib/captcha/enums/captcha-decorators.enum.js +0 -7
  88. package/dist/lib/captcha/enums/captcha-messages.enum.d.ts +0 -3
  89. package/dist/lib/captcha/enums/captcha-messages.enum.js +0 -7
  90. package/dist/lib/captcha/index.d.ts +0 -5
  91. package/dist/lib/captcha/index.js +0 -9
@@ -0,0 +1 @@
1
+ export declare const RecaptchaResult: (...dataOrPipes: any[]) => ParameterDecorator;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RecaptchaResult = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const load_module_1 = require("../helpers/load-module");
6
+ exports.RecaptchaResult = (0, common_1.createParamDecorator)((data, context) => {
7
+ switch (context.getType()) {
8
+ case 'http':
9
+ return context.switchToHttp().getRequest().recaptchaValidationResult;
10
+ case 'graphql':
11
+ return (0, load_module_1.loadModule)('@nestjs/graphql', true)
12
+ .GqlExecutionContext.create(context)
13
+ .getContext().req?.connection?._httpMessage?.req
14
+ ?.recaptchaValidationResult;
15
+ }
16
+ });
@@ -0,0 +1,2 @@
1
+ import { VerifyResponseDecoratorOptions } from '../interfaces/verify-response-decorator-options';
2
+ export declare function Recaptcha(options?: VerifyResponseDecoratorOptions): MethodDecorator & ClassDecorator;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Recaptcha = Recaptcha;
4
+ const common_1 = require("@nestjs/common");
5
+ const google_recaptcha_guard_1 = require("../guards/google-recaptcha.guard");
6
+ const set_recaptcha_options_1 = require("./set-recaptcha-options");
7
+ function Recaptcha(options) {
8
+ return (0, common_1.applyDecorators)((0, set_recaptcha_options_1.SetRecaptchaOptions)(options), (0, common_1.UseGuards)(google_recaptcha_guard_1.GoogleRecaptchaGuard));
9
+ }
@@ -0,0 +1,2 @@
1
+ import { VerifyResponseDecoratorOptions } from '../interfaces/verify-response-decorator-options';
2
+ export declare function SetRecaptchaOptions(options?: VerifyResponseDecoratorOptions): MethodDecorator & ClassDecorator;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SetRecaptchaOptions = SetRecaptchaOptions;
4
+ const common_1 = require("@nestjs/common");
5
+ const provider_declarations_1 = require("../provider.declarations");
6
+ function SetRecaptchaOptions(options) {
7
+ return (0, common_1.SetMetadata)(provider_declarations_1.RECAPTCHA_VALIDATION_OPTIONS, options);
8
+ }
@@ -0,0 +1,8 @@
1
+ export declare enum ClassificationReason {
2
+ CLASSIFICATION_REASON_UNSPECIFIED = "CLASSIFICATION_REASON_UNSPECIFIED",
3
+ AUTOMATION = "AUTOMATION",
4
+ UNEXPECTED_ENVIRONMENT = "UNEXPECTED_ENVIRONMENT",
5
+ TOO_MUCH_TRAFFIC = "TOO_MUCH_TRAFFIC",
6
+ UNEXPECTED_USAGE_PATTERNS = "UNEXPECTED_USAGE_PATTERNS",
7
+ LOW_CONFIDENCE_SCORE = "LOW_CONFIDENCE_SCORE"
8
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClassificationReason = void 0;
4
+ var ClassificationReason;
5
+ (function (ClassificationReason) {
6
+ ClassificationReason["CLASSIFICATION_REASON_UNSPECIFIED"] = "CLASSIFICATION_REASON_UNSPECIFIED";
7
+ ClassificationReason["AUTOMATION"] = "AUTOMATION";
8
+ ClassificationReason["UNEXPECTED_ENVIRONMENT"] = "UNEXPECTED_ENVIRONMENT";
9
+ ClassificationReason["TOO_MUCH_TRAFFIC"] = "TOO_MUCH_TRAFFIC";
10
+ ClassificationReason["UNEXPECTED_USAGE_PATTERNS"] = "UNEXPECTED_USAGE_PATTERNS";
11
+ ClassificationReason["LOW_CONFIDENCE_SCORE"] = "LOW_CONFIDENCE_SCORE";
12
+ })(ClassificationReason || (exports.ClassificationReason = ClassificationReason = {}));
@@ -0,0 +1,16 @@
1
+ export declare enum ErrorCode {
2
+ MissingInputSecret = "missing-input-secret",
3
+ InvalidInputSecret = "invalid-input-secret",
4
+ MissingInputResponse = "missing-input-response",
5
+ InvalidInputResponse = "invalid-input-response",
6
+ BadRequest = "bad-request",
7
+ TimeoutOrDuplicate = "timeout-or-duplicate",
8
+ UnknownError = "unknown-error",
9
+ ForbiddenAction = "forbidden-action",
10
+ LowScore = "low-score",
11
+ InvalidKeys = "invalid-keys",
12
+ IncorrectCaptchaSol = "incorrect-captcha-sol",
13
+ NetworkError = "network-error",
14
+ SiteMismatch = "site-mismatch",
15
+ BrowserError = "browser-error"
16
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorCode = void 0;
4
+ var ErrorCode;
5
+ (function (ErrorCode) {
6
+ ErrorCode["MissingInputSecret"] = "missing-input-secret";
7
+ ErrorCode["InvalidInputSecret"] = "invalid-input-secret";
8
+ ErrorCode["MissingInputResponse"] = "missing-input-response";
9
+ ErrorCode["InvalidInputResponse"] = "invalid-input-response";
10
+ ErrorCode["BadRequest"] = "bad-request";
11
+ ErrorCode["TimeoutOrDuplicate"] = "timeout-or-duplicate";
12
+ ErrorCode["UnknownError"] = "unknown-error";
13
+ ErrorCode["ForbiddenAction"] = "forbidden-action";
14
+ ErrorCode["LowScore"] = "low-score";
15
+ ErrorCode["InvalidKeys"] = "invalid-keys";
16
+ ErrorCode["IncorrectCaptchaSol"] = "incorrect-captcha-sol";
17
+ ErrorCode["NetworkError"] = "network-error";
18
+ ErrorCode["SiteMismatch"] = "site-mismatch";
19
+ ErrorCode["BrowserError"] = "browser-error";
20
+ })(ErrorCode || (exports.ErrorCode = ErrorCode = {}));
@@ -0,0 +1,4 @@
1
+ export declare enum GoogleRecaptchaContext {
2
+ GoogleRecaptcha = "GoogleRecaptcha",
3
+ GoogleRecaptchaEnterprise = "GoogleRecaptchaEnterprise"
4
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GoogleRecaptchaContext = void 0;
4
+ var GoogleRecaptchaContext;
5
+ (function (GoogleRecaptchaContext) {
6
+ GoogleRecaptchaContext["GoogleRecaptcha"] = "GoogleRecaptcha";
7
+ GoogleRecaptchaContext["GoogleRecaptchaEnterprise"] = "GoogleRecaptchaEnterprise";
8
+ })(GoogleRecaptchaContext || (exports.GoogleRecaptchaContext = GoogleRecaptchaContext = {}));
@@ -0,0 +1,10 @@
1
+ export declare enum GoogleRecaptchaEnterpriseReason {
2
+ InvalidReasonUnspecified = "INVALID_REASON_UNSPECIFIED",
3
+ UnknownInvalidReason = "UNKNOWN_INVALID_REASON",
4
+ Malformed = "MALFORMED",
5
+ Expired = "EXPIRED",
6
+ Dupe = "DUPE",
7
+ SiteMismatch = "SITE_MISMATCH",
8
+ Missing = "MISSING",
9
+ BrowserError = "BROWSER_ERROR"
10
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GoogleRecaptchaEnterpriseReason = void 0;
4
+ var GoogleRecaptchaEnterpriseReason;
5
+ (function (GoogleRecaptchaEnterpriseReason) {
6
+ GoogleRecaptchaEnterpriseReason["InvalidReasonUnspecified"] = "INVALID_REASON_UNSPECIFIED";
7
+ GoogleRecaptchaEnterpriseReason["UnknownInvalidReason"] = "UNKNOWN_INVALID_REASON";
8
+ GoogleRecaptchaEnterpriseReason["Malformed"] = "MALFORMED";
9
+ GoogleRecaptchaEnterpriseReason["Expired"] = "EXPIRED";
10
+ GoogleRecaptchaEnterpriseReason["Dupe"] = "DUPE";
11
+ GoogleRecaptchaEnterpriseReason["SiteMismatch"] = "SITE_MISMATCH";
12
+ GoogleRecaptchaEnterpriseReason["Missing"] = "MISSING";
13
+ GoogleRecaptchaEnterpriseReason["BrowserError"] = "BROWSER_ERROR";
14
+ })(GoogleRecaptchaEnterpriseReason || (exports.GoogleRecaptchaEnterpriseReason = GoogleRecaptchaEnterpriseReason = {}));
@@ -0,0 +1,4 @@
1
+ export declare enum GoogleRecaptchaNetwork {
2
+ Google = "https://www.google.com/recaptcha/api/siteverify",
3
+ Recaptcha = "https://recaptcha.net/recaptcha/api/siteverify"
4
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GoogleRecaptchaNetwork = void 0;
4
+ var GoogleRecaptchaNetwork;
5
+ (function (GoogleRecaptchaNetwork) {
6
+ GoogleRecaptchaNetwork["Google"] = "https://www.google.com/recaptcha/api/siteverify";
7
+ GoogleRecaptchaNetwork["Recaptcha"] = "https://recaptcha.net/recaptcha/api/siteverify";
8
+ })(GoogleRecaptchaNetwork || (exports.GoogleRecaptchaNetwork = GoogleRecaptchaNetwork = {}));
@@ -0,0 +1,5 @@
1
+ import { GoogleRecaptchaException } from './google-recaptcha.exception';
2
+ export declare class GoogleRecaptchaNetworkException extends GoogleRecaptchaException {
3
+ readonly networkErrorCode?: string;
4
+ constructor(networkErrorCode?: string);
5
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GoogleRecaptchaNetworkException = void 0;
4
+ const google_recaptcha_exception_1 = require("./google-recaptcha.exception");
5
+ const error_code_1 = require("../enums/error-code");
6
+ class GoogleRecaptchaNetworkException extends google_recaptcha_exception_1.GoogleRecaptchaException {
7
+ constructor(networkErrorCode) {
8
+ super([error_code_1.ErrorCode.NetworkError], networkErrorCode
9
+ ? `Network error '${networkErrorCode}'.`
10
+ : 'Unknown network error.');
11
+ this.networkErrorCode = networkErrorCode;
12
+ }
13
+ }
14
+ exports.GoogleRecaptchaNetworkException = GoogleRecaptchaNetworkException;
@@ -0,0 +1,8 @@
1
+ import { HttpException } from '@nestjs/common';
2
+ import { ErrorCode } from '../enums/error-code';
3
+ export declare class GoogleRecaptchaException extends HttpException {
4
+ readonly errorCodes: ErrorCode[];
5
+ constructor(errorCodes: ErrorCode[], errorMessage?: string);
6
+ private static getErrorMessage;
7
+ private static getErrorStatus;
8
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GoogleRecaptchaException = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const error_code_1 = require("../enums/error-code");
6
+ class GoogleRecaptchaException extends common_1.HttpException {
7
+ constructor(errorCodes, errorMessage) {
8
+ super(errorMessage || GoogleRecaptchaException.getErrorMessage(errorCodes[0]), GoogleRecaptchaException.getErrorStatus(errorCodes[0]));
9
+ this.errorCodes = errorCodes;
10
+ }
11
+ static getErrorMessage(errorCode) {
12
+ switch (errorCode) {
13
+ case error_code_1.ErrorCode.InvalidInputResponse:
14
+ return 'The response parameter is invalid or malformed.';
15
+ case error_code_1.ErrorCode.MissingInputResponse:
16
+ return 'The response parameter is missing.';
17
+ case error_code_1.ErrorCode.TimeoutOrDuplicate:
18
+ return 'The response is no longer valid: either is too old or has been used previously.';
19
+ case error_code_1.ErrorCode.InvalidInputSecret:
20
+ case error_code_1.ErrorCode.MissingInputSecret:
21
+ return 'Invalid module configuration. Please check public-secret keys.';
22
+ case error_code_1.ErrorCode.InvalidKeys:
23
+ return 'Recaptcha token was signed by invalid api key.';
24
+ case error_code_1.ErrorCode.LowScore:
25
+ return 'Low recaptcha score.';
26
+ case error_code_1.ErrorCode.ForbiddenAction:
27
+ return 'Forbidden recaptcha action.';
28
+ case error_code_1.ErrorCode.SiteMismatch:
29
+ return 'The user verification token did not match the provided site key.';
30
+ case error_code_1.ErrorCode.BrowserError:
31
+ return 'Retriable error (such as network failure) occurred on the browser.';
32
+ case error_code_1.ErrorCode.IncorrectCaptchaSol:
33
+ return 'incorrect-google-recaptcha-sol';
34
+ case error_code_1.ErrorCode.UnknownError:
35
+ case error_code_1.ErrorCode.BadRequest:
36
+ default:
37
+ return 'Unexpected error.';
38
+ }
39
+ }
40
+ static getErrorStatus(errorCode) {
41
+ return errorCode === error_code_1.ErrorCode.InvalidInputResponse ||
42
+ errorCode === error_code_1.ErrorCode.MissingInputResponse ||
43
+ errorCode === error_code_1.ErrorCode.TimeoutOrDuplicate ||
44
+ errorCode === error_code_1.ErrorCode.ForbiddenAction ||
45
+ errorCode === error_code_1.ErrorCode.SiteMismatch ||
46
+ errorCode === error_code_1.ErrorCode.BrowserError ||
47
+ errorCode === error_code_1.ErrorCode.IncorrectCaptchaSol ||
48
+ errorCode === error_code_1.ErrorCode.LowScore ||
49
+ errorCode === error_code_1.ErrorCode.InvalidKeys
50
+ ? common_1.HttpStatus.BAD_REQUEST
51
+ : common_1.HttpStatus.INTERNAL_SERVER_ERROR;
52
+ }
53
+ }
54
+ exports.GoogleRecaptchaException = GoogleRecaptchaException;
@@ -0,0 +1,12 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { GoogleRecaptchaModuleAsyncOptions, GoogleRecaptchaModuleOptions } from './interfaces/google-recaptcha-module-options';
3
+ export declare class GoogleRecaptchaModule {
4
+ private static axiosDefaultConfig;
5
+ static forRoot(options: GoogleRecaptchaModuleOptions): DynamicModule;
6
+ static forRootAsync(options: GoogleRecaptchaModuleAsyncOptions): DynamicModule;
7
+ private static transformAxiosConfig;
8
+ private static createAsyncProviders;
9
+ private static createAsyncOptionsProvider;
10
+ private static validateOptions;
11
+ private static isGoogleRecaptchaFactory;
12
+ }
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GoogleRecaptchaModule = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const google_recaptcha_guard_1 = require("./guards/google-recaptcha.guard");
6
+ const google_recaptcha_validator_1 = require("./services/validators/google-recaptcha.validator");
7
+ const google_recaptcha_enterprise_validator_1 = require("./services/validators/google-recaptcha-enterprise.validator");
8
+ const provider_declarations_1 = require("./provider.declarations");
9
+ const recaptcha_request_resolver_1 = require("./services/recaptcha-request.resolver");
10
+ const core_1 = require("@nestjs/core");
11
+ const axios_1 = require("axios");
12
+ const https_1 = require("https");
13
+ const recaptcha_validator_resolver_1 = require("./services/recaptcha-validator.resolver");
14
+ const enterprise_reason_transformer_1 = require("./services/enterprise-reason.transformer");
15
+ const xor_1 = require("./helpers/xor");
16
+ const recaptcha_config_ref_1 = require("./models/recaptcha-config-ref");
17
+ class GoogleRecaptchaModule {
18
+ static forRoot(options) {
19
+ const providers = [
20
+ core_1.Reflector,
21
+ google_recaptcha_guard_1.GoogleRecaptchaGuard,
22
+ google_recaptcha_validator_1.GoogleRecaptchaValidator,
23
+ google_recaptcha_enterprise_validator_1.GoogleRecaptchaEnterpriseValidator,
24
+ recaptcha_request_resolver_1.RecaptchaRequestResolver,
25
+ recaptcha_validator_resolver_1.RecaptchaValidatorResolver,
26
+ enterprise_reason_transformer_1.EnterpriseReasonTransformer,
27
+ {
28
+ provide: provider_declarations_1.RECAPTCHA_OPTIONS,
29
+ useValue: options,
30
+ },
31
+ {
32
+ provide: provider_declarations_1.RECAPTCHA_LOGGER,
33
+ useFactory: () => options.logger || new common_1.Logger(),
34
+ },
35
+ {
36
+ provide: recaptcha_config_ref_1.RecaptchaConfigRef,
37
+ useFactory: () => new recaptcha_config_ref_1.RecaptchaConfigRef(options),
38
+ },
39
+ ];
40
+ this.validateOptions(options);
41
+ const internalProviders = [
42
+ {
43
+ provide: provider_declarations_1.RECAPTCHA_AXIOS_INSTANCE,
44
+ useFactory: () => axios_1.default.create(this.transformAxiosConfig({
45
+ ...this.axiosDefaultConfig,
46
+ ...options.axiosConfig,
47
+ headers: null,
48
+ })),
49
+ },
50
+ ];
51
+ return {
52
+ global: options.global != null ? options.global : true,
53
+ module: GoogleRecaptchaModule,
54
+ providers: providers.concat(internalProviders),
55
+ exports: providers,
56
+ };
57
+ }
58
+ static forRootAsync(options) {
59
+ const providers = [
60
+ core_1.Reflector,
61
+ {
62
+ provide: provider_declarations_1.RECAPTCHA_LOGGER,
63
+ useFactory: (options) => options.logger || new common_1.Logger(),
64
+ inject: [provider_declarations_1.RECAPTCHA_OPTIONS],
65
+ },
66
+ {
67
+ provide: recaptcha_config_ref_1.RecaptchaConfigRef,
68
+ useFactory: (opts) => new recaptcha_config_ref_1.RecaptchaConfigRef(opts),
69
+ inject: [provider_declarations_1.RECAPTCHA_OPTIONS],
70
+ },
71
+ google_recaptcha_guard_1.GoogleRecaptchaGuard,
72
+ google_recaptcha_validator_1.GoogleRecaptchaValidator,
73
+ google_recaptcha_enterprise_validator_1.GoogleRecaptchaEnterpriseValidator,
74
+ recaptcha_request_resolver_1.RecaptchaRequestResolver,
75
+ recaptcha_validator_resolver_1.RecaptchaValidatorResolver,
76
+ enterprise_reason_transformer_1.EnterpriseReasonTransformer,
77
+ ...this.createAsyncProviders(options),
78
+ ];
79
+ const internalProviders = [
80
+ {
81
+ provide: provider_declarations_1.RECAPTCHA_AXIOS_INSTANCE,
82
+ useFactory: (options) => {
83
+ this.validateOptions(options);
84
+ const transformedAxiosConfig = this.transformAxiosConfig({
85
+ ...this.axiosDefaultConfig,
86
+ ...options.axiosConfig,
87
+ headers: null,
88
+ });
89
+ return axios_1.default.create(transformedAxiosConfig);
90
+ },
91
+ inject: [provider_declarations_1.RECAPTCHA_OPTIONS],
92
+ },
93
+ ];
94
+ return {
95
+ global: options.global != null ? options.global : true,
96
+ module: GoogleRecaptchaModule,
97
+ imports: options.imports,
98
+ providers: providers.concat(internalProviders),
99
+ exports: providers,
100
+ };
101
+ }
102
+ static transformAxiosConfig(axiosConfig) {
103
+ const config = { ...axiosConfig };
104
+ delete config.baseURL;
105
+ delete config.url;
106
+ delete config.responseType;
107
+ delete config.method;
108
+ delete config.transformRequest;
109
+ delete config.transformResponse;
110
+ delete config.paramsSerializer;
111
+ delete config.validateStatus;
112
+ delete config.data;
113
+ delete config.adapter;
114
+ return config;
115
+ }
116
+ static createAsyncProviders(options) {
117
+ const providers = [this.createAsyncOptionsProvider(options)];
118
+ if (options.useClass) {
119
+ providers.push({
120
+ provide: options.useClass,
121
+ useClass: options.useClass,
122
+ });
123
+ }
124
+ return providers;
125
+ }
126
+ static createAsyncOptionsProvider(options) {
127
+ if (options.useFactory) {
128
+ return {
129
+ provide: provider_declarations_1.RECAPTCHA_OPTIONS,
130
+ useFactory: options.useFactory,
131
+ inject: options.inject,
132
+ };
133
+ }
134
+ return {
135
+ provide: provider_declarations_1.RECAPTCHA_OPTIONS,
136
+ useFactory: async (optionsFactory) => {
137
+ if (!this.isGoogleRecaptchaFactory(optionsFactory)) {
138
+ throw new Error("Factory must be implement 'GoogleRecaptchaOptionsFactory' interface.");
139
+ }
140
+ return optionsFactory.createGoogleRecaptchaOptions();
141
+ },
142
+ inject: [options.useExisting || options.useClass],
143
+ };
144
+ }
145
+ static validateOptions(options) {
146
+ const hasEnterpriseOptions = !!Object.keys(options.enterprise || {}).length;
147
+ if (!(0, xor_1.xor)(options.disable, !(0, xor_1.xor)(options.secretKey, hasEnterpriseOptions))) {
148
+ throw new Error('Google recaptcha options must be contains "secretKey" xor "enterprise".');
149
+ }
150
+ }
151
+ static isGoogleRecaptchaFactory(object) {
152
+ return (!!object && typeof object.createGoogleRecaptchaOptions === 'function');
153
+ }
154
+ }
155
+ exports.GoogleRecaptchaModule = GoogleRecaptchaModule;
156
+ GoogleRecaptchaModule.axiosDefaultConfig = {
157
+ timeout: 60_000,
158
+ httpsAgent: new https_1.Agent({ keepAlive: true }),
159
+ };
@@ -0,0 +1,15 @@
1
+ import { CanActivate, ExecutionContext, Logger } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ import { RecaptchaRequestResolver } from '../services/recaptcha-request.resolver';
4
+ import { RecaptchaValidatorResolver } from '../services/recaptcha-validator.resolver';
5
+ import { RecaptchaConfigRef } from '../models/recaptcha-config-ref';
6
+ export declare class GoogleRecaptchaGuard implements CanActivate {
7
+ private readonly reflector;
8
+ private readonly requestResolver;
9
+ private readonly validatorResolver;
10
+ private readonly logger;
11
+ private readonly configRef;
12
+ constructor(reflector: Reflector, requestResolver: RecaptchaRequestResolver, validatorResolver: RecaptchaValidatorResolver, logger: Logger, configRef: RecaptchaConfigRef);
13
+ canActivate(context: ExecutionContext): Promise<true | never>;
14
+ private resolveLogContext;
15
+ }
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.GoogleRecaptchaGuard = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const provider_declarations_1 = require("../provider.declarations");
18
+ const google_recaptcha_exception_1 = require("../exceptions/google-recaptcha.exception");
19
+ const core_1 = require("@nestjs/core");
20
+ const recaptcha_request_resolver_1 = require("../services/recaptcha-request.resolver");
21
+ const recaptcha_validator_resolver_1 = require("../services/recaptcha-validator.resolver");
22
+ const google_recaptcha_context_1 = require("../enums/google-recaptcha-context");
23
+ const google_recaptcha_enterprise_validator_1 = require("../services/validators/google-recaptcha-enterprise.validator");
24
+ const recaptcha_config_ref_1 = require("../models/recaptcha-config-ref");
25
+ let GoogleRecaptchaGuard = class GoogleRecaptchaGuard {
26
+ constructor(reflector, requestResolver, validatorResolver, logger, configRef) {
27
+ this.reflector = reflector;
28
+ this.requestResolver = requestResolver;
29
+ this.validatorResolver = validatorResolver;
30
+ this.logger = logger;
31
+ this.configRef = configRef;
32
+ }
33
+ async canActivate(context) {
34
+ const isDisabled = this.configRef.valueOf.disable;
35
+ if (isDisabled) {
36
+ return true;
37
+ }
38
+ const request = this.requestResolver.resolve(context);
39
+ const skipIfValue = this.configRef.valueOf.skipIf;
40
+ const skip = typeof skipIfValue === 'function'
41
+ ? await skipIfValue(request)
42
+ : !!skipIfValue;
43
+ if (skip) {
44
+ return true;
45
+ }
46
+ const options = this.reflector.get(provider_declarations_1.RECAPTCHA_VALIDATION_OPTIONS, context.getHandler());
47
+ const [response, remoteIp] = await Promise.all([
48
+ options?.response
49
+ ? await options.response(request)
50
+ : await this.configRef.valueOf.response(request),
51
+ options?.remoteIp
52
+ ? await options.remoteIp(request)
53
+ : (await this.configRef.valueOf.remoteIp) &&
54
+ this.configRef.valueOf.remoteIp(request),
55
+ ]);
56
+ const score = options?.score || this.configRef.valueOf.score;
57
+ const action = options?.action;
58
+ const validator = this.validatorResolver.resolve();
59
+ request.recaptchaValidationResult = await validator.validate({
60
+ response,
61
+ remoteIp,
62
+ score,
63
+ action,
64
+ });
65
+ if (this.configRef.valueOf.debug) {
66
+ const loggerCtx = this.resolveLogContext(validator);
67
+ this.logger.debug(request.recaptchaValidationResult.toObject(), `${loggerCtx}.result`);
68
+ }
69
+ if (request.recaptchaValidationResult.success) {
70
+ return true;
71
+ }
72
+ throw new google_recaptcha_exception_1.GoogleRecaptchaException(request.recaptchaValidationResult.errors);
73
+ }
74
+ resolveLogContext(validator) {
75
+ return validator instanceof google_recaptcha_enterprise_validator_1.GoogleRecaptchaEnterpriseValidator
76
+ ? google_recaptcha_context_1.GoogleRecaptchaContext.GoogleRecaptchaEnterprise
77
+ : google_recaptcha_context_1.GoogleRecaptchaContext.GoogleRecaptcha;
78
+ }
79
+ };
80
+ exports.GoogleRecaptchaGuard = GoogleRecaptchaGuard;
81
+ exports.GoogleRecaptchaGuard = GoogleRecaptchaGuard = __decorate([
82
+ (0, common_1.Injectable)(),
83
+ __param(3, (0, common_1.Inject)(provider_declarations_1.RECAPTCHA_LOGGER)),
84
+ __metadata("design:paramtypes", [core_1.Reflector,
85
+ recaptcha_request_resolver_1.RecaptchaRequestResolver,
86
+ recaptcha_validator_resolver_1.RecaptchaValidatorResolver,
87
+ common_1.Logger,
88
+ recaptcha_config_ref_1.RecaptchaConfigRef])
89
+ ], GoogleRecaptchaGuard);
@@ -0,0 +1,4 @@
1
+ import * as axios from 'axios';
2
+ import { LiteralObject } from '../interfaces/literal-object';
3
+ export declare function isAxiosError<T = LiteralObject>(error: Error | axios.AxiosError<T>): error is axios.AxiosError<T>;
4
+ export declare function getErrorInfo(error: Error): string | LiteralObject;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isAxiosError = isAxiosError;
4
+ exports.getErrorInfo = getErrorInfo;
5
+ function isAxiosError(error) {
6
+ return error.isAxiosError;
7
+ }
8
+ function getErrorInfo(error) {
9
+ if (isAxiosError(error)) {
10
+ return error.response?.data || error.code || 'Unknown axios error';
11
+ }
12
+ return { error: error.name, message: error.message, stack: error.stack };
13
+ }
@@ -0,0 +1,2 @@
1
+ import { LiteralObject } from '../interfaces/literal-object';
2
+ export declare function loadModule(moduleName: string, logError?: boolean): LiteralObject;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadModule = loadModule;
4
+ const common_1 = require("@nestjs/common");
5
+ function loadModule(moduleName, logError = false) {
6
+ try {
7
+ return require(moduleName);
8
+ }
9
+ catch (e) {
10
+ if (logError) {
11
+ common_1.Logger.error(`Module '${moduleName}' not found. \nPotential solution npm i ${moduleName}`);
12
+ }
13
+ throw e;
14
+ }
15
+ }
@@ -0,0 +1 @@
1
+ export declare function xor(a: any, b: any): boolean;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.xor = xor;
4
+ function xor(a, b) {
5
+ return !!a !== !!b;
6
+ }
@@ -0,0 +1,15 @@
1
+ export { Recaptcha } from './decorators/recaptcha';
2
+ export { SetRecaptchaOptions } from './decorators/set-recaptcha-options';
3
+ export { RecaptchaResult } from './decorators/recaptcha-result';
4
+ export { GoogleRecaptchaGuard } from './guards/google-recaptcha.guard';
5
+ export { GoogleRecaptchaModule } from './google-recaptcha.module';
6
+ export { GoogleRecaptchaModuleOptions } from './interfaces/google-recaptcha-module-options';
7
+ export { ErrorCode } from './enums/error-code';
8
+ export { GoogleRecaptchaNetwork } from './enums/google-recaptcha-network';
9
+ export { GoogleRecaptchaException } from './exceptions/google-recaptcha.exception';
10
+ export { GoogleRecaptchaNetworkException } from './exceptions/google-recaptcha-network.exception';
11
+ export { GoogleRecaptchaValidator } from './services/validators/google-recaptcha.validator';
12
+ export { GoogleRecaptchaEnterpriseValidator } from './services/validators/google-recaptcha-enterprise.validator';
13
+ export { RecaptchaVerificationResult } from './models/recaptcha-verification-result';
14
+ export { ClassificationReason } from './enums/classification-reason';
15
+ export { RecaptchaConfigRef } from './models/recaptcha-config-ref';