perimeterx-js-core 0.1.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 (288) hide show
  1. package/README.md +158 -0
  2. package/lib/activities/ActivityType.d.ts +6 -0
  3. package/lib/activities/ActivityType.js +10 -0
  4. package/lib/activities/HttpActivityClient.d.ts +12 -0
  5. package/lib/activities/HttpActivityClient.js +107 -0
  6. package/lib/activities/IActivityClient.d.ts +8 -0
  7. package/lib/activities/IActivityClient.js +2 -0
  8. package/lib/activities/constants.d.ts +1 -0
  9. package/lib/activities/constants.js +4 -0
  10. package/lib/activities/index.d.ts +5 -0
  11. package/lib/activities/index.js +7 -0
  12. package/lib/activities/model/Activity.d.ts +13 -0
  13. package/lib/activities/model/Activity.js +2 -0
  14. package/lib/activities/model/ActivityDetails.d.ts +41 -0
  15. package/lib/activities/model/ActivityDetails.js +2 -0
  16. package/lib/activities/utils.d.ts +10 -0
  17. package/lib/activities/utils.js +88 -0
  18. package/lib/additional_activity_handler/AdditionalActivityHandler.d.ts +3 -0
  19. package/lib/additional_activity_handler/AdditionalActivityHandler.js +2 -0
  20. package/lib/additional_activity_handler/AdditionalActivityHandlerUtils.d.ts +5 -0
  21. package/lib/additional_activity_handler/AdditionalActivityHandlerUtils.js +64 -0
  22. package/lib/additional_activity_handler/index.d.ts +2 -0
  23. package/lib/additional_activity_handler/index.js +5 -0
  24. package/lib/block_handler/BlockAction.d.ts +6 -0
  25. package/lib/block_handler/BlockAction.js +21 -0
  26. package/lib/block_handler/BlockReason.d.ts +4 -0
  27. package/lib/block_handler/BlockReason.js +8 -0
  28. package/lib/block_handler/DefaultBlockResponseGenerator.d.ts +14 -0
  29. package/lib/block_handler/DefaultBlockResponseGenerator.js +44 -0
  30. package/lib/block_handler/IBlockResponseGenerator.d.ts +14 -0
  31. package/lib/block_handler/IBlockResponseGenerator.js +2 -0
  32. package/lib/block_handler/captcha/CaptchaBlockResponseGenerator.d.ts +14 -0
  33. package/lib/block_handler/captcha/CaptchaBlockResponseGenerator.js +30 -0
  34. package/lib/block_handler/captcha/HtmlCaptchaResponseGenerator.d.ts +10 -0
  35. package/lib/block_handler/captcha/HtmlCaptchaResponseGenerator.js +25 -0
  36. package/lib/block_handler/captcha/JsonCaptchaResponseGenerator.d.ts +10 -0
  37. package/lib/block_handler/captcha/JsonCaptchaResponseGenerator.js +44 -0
  38. package/lib/block_handler/captcha/MobileCaptchaResponseGenerator.d.ts +12 -0
  39. package/lib/block_handler/captcha/MobileCaptchaResponseGenerator.js +36 -0
  40. package/lib/block_handler/index.d.ts +13 -0
  41. package/lib/block_handler/index.js +21 -0
  42. package/lib/block_handler/model/BlockData.d.ts +16 -0
  43. package/lib/block_handler/model/BlockData.js +2 -0
  44. package/lib/block_handler/model/JsonBlockPayload.d.ts +11 -0
  45. package/lib/block_handler/model/JsonBlockPayload.js +2 -0
  46. package/lib/block_handler/model/MobileBlockPayload.d.ts +8 -0
  47. package/lib/block_handler/model/MobileBlockPayload.js +2 -0
  48. package/lib/block_handler/templates/captcha_template.d.ts +1 -0
  49. package/lib/block_handler/templates/captcha_template.js +4 -0
  50. package/lib/block_handler/templates/rate_limit_template.d.ts +1 -0
  51. package/lib/block_handler/templates/rate_limit_template.js +4 -0
  52. package/lib/block_handler/utils.d.ts +5 -0
  53. package/lib/block_handler/utils.js +48 -0
  54. package/lib/config/ConfigurationParams.d.ts +65 -0
  55. package/lib/config/ConfigurationParams.js +2 -0
  56. package/lib/config/DefaultConfigurations.d.ts +2 -0
  57. package/lib/config/DefaultConfigurations.js +107 -0
  58. package/lib/config/IConfiguration.d.ts +172 -0
  59. package/lib/config/IConfiguration.js +2 -0
  60. package/lib/config/StaticConfigurationBase.d.ts +57 -0
  61. package/lib/config/StaticConfigurationBase.js +355 -0
  62. package/lib/config/index.d.ts +4 -0
  63. package/lib/config/index.js +7 -0
  64. package/lib/context/ContextBase.d.ts +55 -0
  65. package/lib/context/ContextBase.js +206 -0
  66. package/lib/context/DefaultTokenV2Context.d.ts +13 -0
  67. package/lib/context/DefaultTokenV2Context.js +35 -0
  68. package/lib/context/DefaultTokenV3Context.d.ts +13 -0
  69. package/lib/context/DefaultTokenV3Context.js +35 -0
  70. package/lib/context/IContext.d.ts +256 -0
  71. package/lib/context/IContext.js +2 -0
  72. package/lib/context/index.d.ts +4 -0
  73. package/lib/context/index.js +9 -0
  74. package/lib/custom_parameters/CustomParameters.d.ts +12 -0
  75. package/lib/custom_parameters/CustomParameters.js +2 -0
  76. package/lib/custom_parameters/CustomParametersFunction.d.ts +4 -0
  77. package/lib/custom_parameters/CustomParametersFunction.js +2 -0
  78. package/lib/custom_parameters/CustomParametersUtils.d.ts +4 -0
  79. package/lib/custom_parameters/CustomParametersUtils.js +19 -0
  80. package/lib/custom_parameters/index.d.ts +3 -0
  81. package/lib/custom_parameters/index.js +5 -0
  82. package/lib/enforcer/EnforcerBase.d.ts +139 -0
  83. package/lib/enforcer/EnforcerBase.js +376 -0
  84. package/lib/enforcer/IEnforcer.d.ts +4 -0
  85. package/lib/enforcer/IEnforcer.js +2 -0
  86. package/lib/enforcer/index.d.ts +2 -0
  87. package/lib/enforcer/index.js +5 -0
  88. package/lib/filter/DefaultFilter.d.ts +16 -0
  89. package/lib/filter/DefaultFilter.js +59 -0
  90. package/lib/filter/FilterReason.d.ts +8 -0
  91. package/lib/filter/FilterReason.js +12 -0
  92. package/lib/filter/IFilter.d.ts +8 -0
  93. package/lib/filter/IFilter.js +2 -0
  94. package/lib/filter/index.d.ts +3 -0
  95. package/lib/filter/index.js +7 -0
  96. package/lib/first_party/DefaultFirstParty.d.ts +22 -0
  97. package/lib/first_party/DefaultFirstParty.js +215 -0
  98. package/lib/first_party/IFirstParty.d.ts +9 -0
  99. package/lib/first_party/IFirstParty.js +2 -0
  100. package/lib/first_party/constants.d.ts +32 -0
  101. package/lib/first_party/constants.js +39 -0
  102. package/lib/first_party/index.d.ts +4 -0
  103. package/lib/first_party/index.js +21 -0
  104. package/lib/first_party/utils.d.ts +8 -0
  105. package/lib/first_party/utils.js +27 -0
  106. package/lib/http/IHttpClient.d.ts +10 -0
  107. package/lib/http/IHttpClient.js +2 -0
  108. package/lib/http/body_reader/IBodyReader.d.ts +5 -0
  109. package/lib/http/body_reader/IBodyReader.js +2 -0
  110. package/lib/http/body_reader/StringBodyReader.d.ts +8 -0
  111. package/lib/http/body_reader/StringBodyReader.js +70 -0
  112. package/lib/http/index.d.ts +10 -0
  113. package/lib/http/index.js +28 -0
  114. package/lib/http/request/DefaultHttpRequest.d.ts +5 -0
  115. package/lib/http/request/DefaultHttpRequest.js +42 -0
  116. package/lib/http/request/HttpRequestBase.d.ts +24 -0
  117. package/lib/http/request/HttpRequestBase.js +77 -0
  118. package/lib/http/request/IHttpRequest.d.ts +31 -0
  119. package/lib/http/request/IHttpRequest.js +2 -0
  120. package/lib/http/response/DefaultHttpResponse.d.ts +5 -0
  121. package/lib/http/response/DefaultHttpResponse.js +32 -0
  122. package/lib/http/response/HttpResponseBase.d.ts +17 -0
  123. package/lib/http/response/HttpResponseBase.js +75 -0
  124. package/lib/http/response/IHttpResponse.d.ts +26 -0
  125. package/lib/http/response/IHttpResponse.js +2 -0
  126. package/lib/http/utils/ContentType.d.ts +9 -0
  127. package/lib/http/utils/ContentType.js +13 -0
  128. package/lib/http/utils/HttpBody.d.ts +4 -0
  129. package/lib/http/utils/HttpBody.js +2 -0
  130. package/lib/http/utils/HttpHeaders.d.ts +51 -0
  131. package/lib/http/utils/HttpHeaders.js +106 -0
  132. package/lib/http/utils/HttpMethod.d.ts +11 -0
  133. package/lib/http/utils/HttpMethod.js +15 -0
  134. package/lib/http/utils/constants.d.ts +9 -0
  135. package/lib/http/utils/constants.js +12 -0
  136. package/lib/http/utils/index.d.ts +5 -0
  137. package/lib/http/utils/index.js +24 -0
  138. package/lib/index.d.ts +17 -0
  139. package/lib/index.js +33 -0
  140. package/lib/logger/DefaultLogger.d.ts +7 -0
  141. package/lib/logger/DefaultLogger.js +31 -0
  142. package/lib/logger/ILogger.d.ts +24 -0
  143. package/lib/logger/ILogger.js +2 -0
  144. package/lib/logger/LoggerBase.d.ts +11 -0
  145. package/lib/logger/LoggerBase.js +28 -0
  146. package/lib/logger/LoggerSeverity.d.ts +5 -0
  147. package/lib/logger/LoggerSeverity.js +9 -0
  148. package/lib/logger/index.d.ts +4 -0
  149. package/lib/logger/index.js +9 -0
  150. package/lib/pxde/DefaultDataEnrichment.d.ts +14 -0
  151. package/lib/pxde/DefaultDataEnrichment.js +133 -0
  152. package/lib/pxde/IDataEnrichment.d.ts +9 -0
  153. package/lib/pxde/IDataEnrichment.js +2 -0
  154. package/lib/pxde/constants.d.ts +4 -0
  155. package/lib/pxde/constants.js +7 -0
  156. package/lib/pxde/index.d.ts +4 -0
  157. package/lib/pxde/index.js +20 -0
  158. package/lib/pxde/model/PXDE.d.ts +9 -0
  159. package/lib/pxde/model/PXDE.js +2 -0
  160. package/lib/pxhd/PXHDUtils.d.ts +8 -0
  161. package/lib/pxhd/PXHDUtils.js +20 -0
  162. package/lib/pxhd/index.d.ts +1 -0
  163. package/lib/pxhd/index.js +5 -0
  164. package/lib/risk_api/PostRiskApiClient.d.ts +27 -0
  165. package/lib/risk_api/PostRiskApiClient.js +232 -0
  166. package/lib/risk_api/S2SCallReason.d.ts +12 -0
  167. package/lib/risk_api/S2SCallReason.js +16 -0
  168. package/lib/risk_api/S2SErrorHandler.d.ts +9 -0
  169. package/lib/risk_api/S2SErrorHandler.js +121 -0
  170. package/lib/risk_api/S2SErrorReason.d.ts +8 -0
  171. package/lib/risk_api/S2SErrorReason.js +12 -0
  172. package/lib/risk_api/constants.d.ts +1 -0
  173. package/lib/risk_api/constants.js +4 -0
  174. package/lib/risk_api/index.d.ts +13 -0
  175. package/lib/risk_api/index.js +34 -0
  176. package/lib/risk_api/model/RiskActivity.d.ts +50 -0
  177. package/lib/risk_api/model/RiskActivity.js +2 -0
  178. package/lib/risk_api/model/RiskResponseV2.d.ts +10 -0
  179. package/lib/risk_api/model/RiskResponseV2.js +2 -0
  180. package/lib/risk_api/model/RiskResponseV3.d.ts +12 -0
  181. package/lib/risk_api/model/RiskResponseV3.js +2 -0
  182. package/lib/risk_api/model/RiskStatus.d.ts +4 -0
  183. package/lib/risk_api/model/RiskStatus.js +8 -0
  184. package/lib/risk_api/risk_response_handler/IRiskResponseHandler.d.ts +5 -0
  185. package/lib/risk_api/risk_response_handler/IRiskResponseHandler.js +2 -0
  186. package/lib/risk_api/risk_response_handler/RiskResponseHandlerBase.d.ts +16 -0
  187. package/lib/risk_api/risk_response_handler/RiskResponseHandlerBase.js +73 -0
  188. package/lib/risk_api/risk_response_handler/RiskResponseV2Handler.d.ts +10 -0
  189. package/lib/risk_api/risk_response_handler/RiskResponseV2Handler.js +46 -0
  190. package/lib/risk_api/risk_response_handler/RiskResponseV3Handler.d.ts +10 -0
  191. package/lib/risk_api/risk_response_handler/RiskResponseV3Handler.js +47 -0
  192. package/lib/risk_token/MobileError.d.ts +6 -0
  193. package/lib/risk_token/MobileError.js +10 -0
  194. package/lib/risk_token/MobileErrorHandler.d.ts +9 -0
  195. package/lib/risk_token/MobileErrorHandler.js +100 -0
  196. package/lib/risk_token/RiskTokenScoreRetriever.d.ts +14 -0
  197. package/lib/risk_token/RiskTokenScoreRetriever.js +148 -0
  198. package/lib/risk_token/TokenOrigin.d.ts +4 -0
  199. package/lib/risk_token/TokenOrigin.js +8 -0
  200. package/lib/risk_token/TokenVerificationResult.d.ts +6 -0
  201. package/lib/risk_token/TokenVerificationResult.js +10 -0
  202. package/lib/risk_token/TokenVersion.d.ts +4 -0
  203. package/lib/risk_token/TokenVersion.js +8 -0
  204. package/lib/risk_token/constants.d.ts +9 -0
  205. package/lib/risk_token/constants.js +12 -0
  206. package/lib/risk_token/index.d.ts +9 -0
  207. package/lib/risk_token/index.js +32 -0
  208. package/lib/risk_token/token/BotDefenderTokenBase.d.ts +27 -0
  209. package/lib/risk_token/token/BotDefenderTokenBase.js +94 -0
  210. package/lib/risk_token/token/IBotDefenderToken.d.ts +54 -0
  211. package/lib/risk_token/token/IBotDefenderToken.js +2 -0
  212. package/lib/risk_token/token/index.d.ts +7 -0
  213. package/lib/risk_token/token/index.js +11 -0
  214. package/lib/risk_token/token/v2/BotDefenderTokenV2Payload.d.ts +6 -0
  215. package/lib/risk_token/token/v2/BotDefenderTokenV2Payload.js +2 -0
  216. package/lib/risk_token/token/v2/DefaultBotDefenderTokenV2.d.ts +28 -0
  217. package/lib/risk_token/token/v2/DefaultBotDefenderTokenV2.js +199 -0
  218. package/lib/risk_token/token/v3/BotDefenderTokenV3Payload.d.ts +9 -0
  219. package/lib/risk_token/token/v3/BotDefenderTokenV3Payload.js +2 -0
  220. package/lib/risk_token/token/v3/DefaultBotDefenderTokenV3.d.ts +31 -0
  221. package/lib/risk_token/token/v3/DefaultBotDefenderTokenV3.js +231 -0
  222. package/lib/risk_token/token/v3/TokenSignField.d.ts +4 -0
  223. package/lib/risk_token/token/v3/TokenSignField.js +8 -0
  224. package/lib/risk_token/utils.d.ts +2 -0
  225. package/lib/risk_token/utils.js +16 -0
  226. package/lib/telemetry/DefaultTelemetry.d.ts +19 -0
  227. package/lib/telemetry/DefaultTelemetry.js +176 -0
  228. package/lib/telemetry/ITelemetry.d.ts +13 -0
  229. package/lib/telemetry/ITelemetry.js +2 -0
  230. package/lib/telemetry/constants.d.ts +7 -0
  231. package/lib/telemetry/constants.js +10 -0
  232. package/lib/telemetry/index.d.ts +4 -0
  233. package/lib/telemetry/index.js +20 -0
  234. package/lib/telemetry/model/TelemetryActivity.d.ts +15 -0
  235. package/lib/telemetry/model/TelemetryActivity.js +2 -0
  236. package/lib/utils/Algorithm.d.ts +4 -0
  237. package/lib/utils/Algorithm.js +8 -0
  238. package/lib/utils/EnforcerException.d.ts +3 -0
  239. package/lib/utils/EnforcerException.js +26 -0
  240. package/lib/utils/IScoreRetriever.d.ts +14 -0
  241. package/lib/utils/IScoreRetriever.js +2 -0
  242. package/lib/utils/ModuleMode.d.ts +4 -0
  243. package/lib/utils/ModuleMode.js +8 -0
  244. package/lib/utils/PassReason.d.ts +6 -0
  245. package/lib/utils/PassReason.js +10 -0
  246. package/lib/utils/VidSource.d.ts +4 -0
  247. package/lib/utils/VidSource.js +8 -0
  248. package/lib/utils/base64/DefaultBase64Utils.d.ts +5 -0
  249. package/lib/utils/base64/DefaultBase64Utils.js +16 -0
  250. package/lib/utils/base64/IBase64Utils.d.ts +4 -0
  251. package/lib/utils/base64/IBase64Utils.js +2 -0
  252. package/lib/utils/base64/index.d.ts +2 -0
  253. package/lib/utils/base64/index.js +5 -0
  254. package/lib/utils/cipher/ICipherUtils.d.ts +7 -0
  255. package/lib/utils/cipher/ICipherUtils.js +2 -0
  256. package/lib/utils/cipher/index.d.ts +1 -0
  257. package/lib/utils/cipher/index.js +2 -0
  258. package/lib/utils/constants.d.ts +10 -0
  259. package/lib/utils/constants.js +13 -0
  260. package/lib/utils/cookie_parser/DefaultCookieParser.d.ts +4 -0
  261. package/lib/utils/cookie_parser/DefaultCookieParser.js +13 -0
  262. package/lib/utils/cookie_parser/ICookieParser.d.ts +3 -0
  263. package/lib/utils/cookie_parser/ICookieParser.js +2 -0
  264. package/lib/utils/cookie_parser/index.d.ts +2 -0
  265. package/lib/utils/cookie_parser/index.js +5 -0
  266. package/lib/utils/hash/DefaultHashUtils.d.ts +5 -0
  267. package/lib/utils/hash/DefaultHashUtils.js +19 -0
  268. package/lib/utils/hash/IHashUtils.d.ts +4 -0
  269. package/lib/utils/hash/IHashUtils.js +2 -0
  270. package/lib/utils/hash/index.d.ts +2 -0
  271. package/lib/utils/hash/index.js +5 -0
  272. package/lib/utils/index.d.ts +14 -0
  273. package/lib/utils/index.js +35 -0
  274. package/lib/utils/ip_range_checker/DefaultIpRangeChecker.d.ts +4 -0
  275. package/lib/utils/ip_range_checker/DefaultIpRangeChecker.js +13 -0
  276. package/lib/utils/ip_range_checker/IIpRangeChecker.d.ts +3 -0
  277. package/lib/utils/ip_range_checker/IIpRangeChecker.js +2 -0
  278. package/lib/utils/ip_range_checker/index.d.ts +2 -0
  279. package/lib/utils/ip_range_checker/index.js +5 -0
  280. package/lib/utils/utils.d.ts +13 -0
  281. package/lib/utils/utils.js +91 -0
  282. package/lib/utils/uuid_generator/DefaultUuidGenerator.d.ts +4 -0
  283. package/lib/utils/uuid_generator/DefaultUuidGenerator.js +13 -0
  284. package/lib/utils/uuid_generator/IUuidGenerator.d.ts +3 -0
  285. package/lib/utils/uuid_generator/IUuidGenerator.js +2 -0
  286. package/lib/utils/uuid_generator/index.d.ts +2 -0
  287. package/lib/utils/uuid_generator/index.js +5 -0
  288. package/package.json +52 -0
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.BotDefenderTokenBase = void 0;
40
+ var TokenVerificationResult_1 = require("../TokenVerificationResult");
41
+ var BotDefenderTokenBase = /** @class */ (function () {
42
+ function BotDefenderTokenBase(config, cookieString) {
43
+ this.cookieString = cookieString;
44
+ this.cookieSecret = config.cookieSecret;
45
+ this.cookieMaxLength = config.riskCookieMaxLength;
46
+ this.isValidated = false;
47
+ }
48
+ BotDefenderTokenBase.prototype.verify = function (context) {
49
+ return __awaiter(this, void 0, void 0, function () {
50
+ var _a, _b;
51
+ return __generator(this, function (_c) {
52
+ switch (_c.label) {
53
+ case 0:
54
+ if (!this.cookieString || this.cookieString.length > this.cookieMaxLength) {
55
+ return [2 /*return*/, TokenVerificationResult_1.TokenVerificationResult.CANNOT_VERIFY];
56
+ }
57
+ if (this.isValid()) {
58
+ return [2 /*return*/, TokenVerificationResult_1.TokenVerificationResult.SUCCESSFUL];
59
+ }
60
+ _a = this;
61
+ _b = this.payload;
62
+ if (_b) return [3 /*break*/, 2];
63
+ return [4 /*yield*/, this.decrypt(context)];
64
+ case 1:
65
+ _b = (_c.sent());
66
+ _c.label = 2;
67
+ case 2:
68
+ _a.payload = _b;
69
+ if (!this.payload) {
70
+ return [2 /*return*/, TokenVerificationResult_1.TokenVerificationResult.DECRYPTION_FAILED];
71
+ }
72
+ return [4 /*yield*/, this.validate(context)];
73
+ case 3:
74
+ if (!(_c.sent())) {
75
+ return [2 /*return*/, TokenVerificationResult_1.TokenVerificationResult.VALIDATION_FAILED];
76
+ }
77
+ this.isValidated = true;
78
+ return [2 /*return*/, TokenVerificationResult_1.TokenVerificationResult.SUCCESSFUL];
79
+ }
80
+ });
81
+ });
82
+ };
83
+ BotDefenderTokenBase.prototype.isValid = function () {
84
+ return this.isValidated;
85
+ };
86
+ BotDefenderTokenBase.prototype.getCookieString = function () {
87
+ return this.cookieString;
88
+ };
89
+ BotDefenderTokenBase.prototype.getPayloadString = function () {
90
+ return this.payload ? JSON.stringify(this.payload) : '';
91
+ };
92
+ return BotDefenderTokenBase;
93
+ }());
94
+ exports.BotDefenderTokenBase = BotDefenderTokenBase;
@@ -0,0 +1,54 @@
1
+ import { BlockAction } from '../../block_handler';
2
+ import { IContext } from '../../context';
3
+ import { TokenVerificationResult } from '../TokenVerificationResult';
4
+ export interface IBotDefenderToken {
5
+ /**
6
+ * The HMAC string on the cookie.
7
+ */
8
+ readonly hmac: string;
9
+ /**
10
+ * The client UUID on the cookie.
11
+ */
12
+ readonly uuid: string;
13
+ /**
14
+ * The VID on the cookie.
15
+ */
16
+ readonly vid: string;
17
+ /**
18
+ * The block action on the cookie.
19
+ */
20
+ readonly action: BlockAction;
21
+ /**
22
+ * The expiration timestamp of the cookie.
23
+ */
24
+ readonly timestamp: number;
25
+ /**
26
+ * The risk score on the cookie.
27
+ */
28
+ readonly score: number;
29
+ /**
30
+ * @param context - The request context.
31
+ * @returns Promise<boolean> - Whether or not the cookie was successfully decrypted and validated.
32
+ */
33
+ verify(context: IContext): Promise<TokenVerificationResult>;
34
+ /**
35
+ * @returns boolean - Whether the cookie's timestamp is expired.
36
+ */
37
+ isExpired(): boolean;
38
+ /**
39
+ * @returns boolean - Whether the cookie has been identified as high risk.
40
+ */
41
+ isHighScore(): boolean;
42
+ /**
43
+ * @returns boolean - Whether the cookie has been validated or not.
44
+ */
45
+ isValid(): boolean;
46
+ /**
47
+ * @returns string - The encrypted cookie string as received on the request.
48
+ */
49
+ getCookieString(): string;
50
+ /**
51
+ * @returns string - The decrypted cookie payload as a JSON string.
52
+ */
53
+ getPayloadString(): string;
54
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ export { IBotDefenderToken } from './IBotDefenderToken';
2
+ export { BotDefenderTokenBase } from './BotDefenderTokenBase';
3
+ export { BotDefenderTokenV2Payload } from './v2/BotDefenderTokenV2Payload';
4
+ export { DefaultBotDefenderTokenV2 } from './v2/DefaultBotDefenderTokenV2';
5
+ export { BotDefenderTokenV3Payload } from './v3/BotDefenderTokenV3Payload';
6
+ export { DefaultBotDefenderTokenV3 } from './v3/DefaultBotDefenderTokenV3';
7
+ export { TokenSignField } from './v3/TokenSignField';
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TokenSignField = exports.DefaultBotDefenderTokenV3 = exports.DefaultBotDefenderTokenV2 = exports.BotDefenderTokenBase = void 0;
4
+ var BotDefenderTokenBase_1 = require("./BotDefenderTokenBase");
5
+ Object.defineProperty(exports, "BotDefenderTokenBase", { enumerable: true, get: function () { return BotDefenderTokenBase_1.BotDefenderTokenBase; } });
6
+ var DefaultBotDefenderTokenV2_1 = require("./v2/DefaultBotDefenderTokenV2");
7
+ Object.defineProperty(exports, "DefaultBotDefenderTokenV2", { enumerable: true, get: function () { return DefaultBotDefenderTokenV2_1.DefaultBotDefenderTokenV2; } });
8
+ var DefaultBotDefenderTokenV3_1 = require("./v3/DefaultBotDefenderTokenV3");
9
+ Object.defineProperty(exports, "DefaultBotDefenderTokenV3", { enumerable: true, get: function () { return DefaultBotDefenderTokenV3_1.DefaultBotDefenderTokenV3; } });
10
+ var TokenSignField_1 = require("./v3/TokenSignField");
11
+ Object.defineProperty(exports, "TokenSignField", { enumerable: true, get: function () { return TokenSignField_1.TokenSignField; } });
@@ -0,0 +1,6 @@
1
+ export declare type BotDefenderTokenV2Payload = {
2
+ h: string;
3
+ u: string;
4
+ t: number;
5
+ v: string;
6
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,28 @@
1
+ import { IConfiguration } from '../../../config';
2
+ import { IContext } from '../../../context';
3
+ import { ILogger } from '../../../logger';
4
+ import { BlockAction } from '../../../block_handler';
5
+ import { IBase64Utils, IHashUtils } from '../../../utils';
6
+ import { BotDefenderTokenV2Payload } from './BotDefenderTokenV2Payload';
7
+ import { BotDefenderTokenBase } from '../BotDefenderTokenBase';
8
+ export declare class DefaultBotDefenderTokenV2 extends BotDefenderTokenBase<BotDefenderTokenV2Payload> {
9
+ protected readonly logger: ILogger;
10
+ protected readonly base64Utils: IBase64Utils;
11
+ protected readonly hashUtils: IHashUtils;
12
+ protected isHighRisk: boolean;
13
+ constructor(config: IConfiguration, cookieString: string, base64Utils: IBase64Utils, hashUtils: IHashUtils);
14
+ protected decrypt(context: IContext): Promise<BotDefenderTokenV2Payload>;
15
+ protected validate(context: IContext): Promise<boolean>;
16
+ private decode;
17
+ private getSigningFields;
18
+ private getHashParam;
19
+ private calculateHmac;
20
+ isExpired(): boolean;
21
+ isHighScore(): boolean;
22
+ get hmac(): string;
23
+ get timestamp(): number;
24
+ get uuid(): string;
25
+ get vid(): string;
26
+ get score(): number;
27
+ get action(): BlockAction;
28
+ }
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.DefaultBotDefenderTokenV2 = void 0;
55
+ var block_handler_1 = require("../../../block_handler");
56
+ var utils_1 = require("../../../utils");
57
+ var BotDefenderTokenBase_1 = require("../BotDefenderTokenBase");
58
+ var DefaultBotDefenderTokenV2 = /** @class */ (function (_super) {
59
+ __extends(DefaultBotDefenderTokenV2, _super);
60
+ function DefaultBotDefenderTokenV2(config, cookieString, base64Utils, hashUtils) {
61
+ var _this = _super.call(this, config, cookieString) || this;
62
+ _this.logger = config.logger;
63
+ _this.base64Utils = base64Utils;
64
+ _this.hashUtils = hashUtils;
65
+ _this.isHighRisk = undefined;
66
+ return _this;
67
+ }
68
+ DefaultBotDefenderTokenV2.prototype.decrypt = function (context) {
69
+ return __awaiter(this, void 0, void 0, function () {
70
+ var payload;
71
+ return __generator(this, function (_a) {
72
+ try {
73
+ payload = this.decode(this.cookieString);
74
+ if ((payload === null || payload === void 0 ? void 0 : payload.t) == null || (payload === null || payload === void 0 ? void 0 : payload.h) == null || (payload === null || payload === void 0 ? void 0 : payload.u) == null || (payload === null || payload === void 0 ? void 0 : payload.v) == null) {
75
+ return [2 /*return*/, null];
76
+ }
77
+ return [2 /*return*/, payload];
78
+ }
79
+ catch (e) {
80
+ this.logger.debug("cookie v2 decryption failed: ".concat(e));
81
+ }
82
+ return [2 /*return*/, null];
83
+ });
84
+ });
85
+ };
86
+ DefaultBotDefenderTokenV2.prototype.validate = function (context) {
87
+ return __awaiter(this, void 0, void 0, function () {
88
+ var signingFields, passHmac, blockHmac;
89
+ return __generator(this, function (_a) {
90
+ switch (_a.label) {
91
+ case 0:
92
+ if (typeof this.payload.t !== 'number' ||
93
+ !(0, utils_1.isValidUuid)(this.payload.v) ||
94
+ !(0, utils_1.isValidUuid)(this.payload.u) ||
95
+ !this.payload.h) {
96
+ return [2 /*return*/, false];
97
+ }
98
+ signingFields = this.getSigningFields(context);
99
+ return [4 /*yield*/, this.calculateHmac(this.getHashParam('0', signingFields))];
100
+ case 1:
101
+ passHmac = _a.sent();
102
+ if (passHmac === this.payload.h) {
103
+ this.isHighRisk = false;
104
+ return [2 /*return*/, true];
105
+ }
106
+ return [4 /*yield*/, this.calculateHmac(this.getHashParam('1', signingFields))];
107
+ case 2:
108
+ blockHmac = _a.sent();
109
+ if (blockHmac === this.payload.h) {
110
+ this.isHighRisk = true;
111
+ return [2 /*return*/, true];
112
+ }
113
+ return [2 /*return*/, false];
114
+ }
115
+ });
116
+ });
117
+ };
118
+ DefaultBotDefenderTokenV2.prototype.decode = function (cookieString) {
119
+ var decodedCookie = this.base64Utils.base64Decode(cookieString);
120
+ return JSON.parse(decodedCookie);
121
+ };
122
+ DefaultBotDefenderTokenV2.prototype.getSigningFields = function (context) {
123
+ return context.isMobile ? '' : context.requestData.userAgent;
124
+ };
125
+ DefaultBotDefenderTokenV2.prototype.getHashParam = function (startingValue, signingFields) {
126
+ var hashParam = startingValue;
127
+ if (signingFields) {
128
+ hashParam += signingFields;
129
+ }
130
+ return hashParam;
131
+ };
132
+ DefaultBotDefenderTokenV2.prototype.calculateHmac = function (param) {
133
+ return __awaiter(this, void 0, void 0, function () {
134
+ var payload;
135
+ return __generator(this, function (_a) {
136
+ payload = "".concat(this.payload.t).concat(this.payload.u).concat(this.payload.v).concat(param);
137
+ return [2 /*return*/, this.hashUtils.createHmac(utils_1.Algorithm.SHA256, payload, this.cookieSecret)];
138
+ });
139
+ });
140
+ };
141
+ DefaultBotDefenderTokenV2.prototype.isExpired = function () {
142
+ var _a;
143
+ return Date.now() > ((_a = this.payload) === null || _a === void 0 ? void 0 : _a.t);
144
+ };
145
+ DefaultBotDefenderTokenV2.prototype.isHighScore = function () {
146
+ return this.isHighRisk;
147
+ };
148
+ Object.defineProperty(DefaultBotDefenderTokenV2.prototype, "hmac", {
149
+ get: function () {
150
+ var _a;
151
+ return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.h;
152
+ },
153
+ enumerable: false,
154
+ configurable: true
155
+ });
156
+ Object.defineProperty(DefaultBotDefenderTokenV2.prototype, "timestamp", {
157
+ get: function () {
158
+ var _a;
159
+ return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.t;
160
+ },
161
+ enumerable: false,
162
+ configurable: true
163
+ });
164
+ Object.defineProperty(DefaultBotDefenderTokenV2.prototype, "uuid", {
165
+ get: function () {
166
+ var _a;
167
+ return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.u;
168
+ },
169
+ enumerable: false,
170
+ configurable: true
171
+ });
172
+ Object.defineProperty(DefaultBotDefenderTokenV2.prototype, "vid", {
173
+ get: function () {
174
+ var _a;
175
+ return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.v;
176
+ },
177
+ enumerable: false,
178
+ configurable: true
179
+ });
180
+ Object.defineProperty(DefaultBotDefenderTokenV2.prototype, "score", {
181
+ get: function () {
182
+ if (this.isValid()) {
183
+ return this.isHighScore() ? 100 : 0;
184
+ }
185
+ return undefined;
186
+ },
187
+ enumerable: false,
188
+ configurable: true
189
+ });
190
+ Object.defineProperty(DefaultBotDefenderTokenV2.prototype, "action", {
191
+ get: function () {
192
+ return block_handler_1.BlockAction.CAPTCHA;
193
+ },
194
+ enumerable: false,
195
+ configurable: true
196
+ });
197
+ return DefaultBotDefenderTokenV2;
198
+ }(BotDefenderTokenBase_1.BotDefenderTokenBase));
199
+ exports.DefaultBotDefenderTokenV2 = DefaultBotDefenderTokenV2;
@@ -0,0 +1,9 @@
1
+ import { BlockAction } from '../../../block_handler';
2
+ export declare type BotDefenderTokenV3Payload = {
3
+ a: BlockAction;
4
+ s: number;
5
+ t: number;
6
+ u: string;
7
+ v: string;
8
+ x?: string;
9
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,31 @@
1
+ import { IConfiguration } from '../../../config';
2
+ import { IContext } from '../../../context';
3
+ import { ILogger } from '../../../logger';
4
+ import { ICipherUtils, IHashUtils } from '../../../utils';
5
+ import { BlockAction } from '../../../block_handler';
6
+ import { BotDefenderTokenV3Payload } from './BotDefenderTokenV3Payload';
7
+ import { TokenSignField } from './TokenSignField';
8
+ import { BotDefenderTokenBase } from '../BotDefenderTokenBase';
9
+ export declare class DefaultBotDefenderTokenV3 extends BotDefenderTokenBase<BotDefenderTokenV3Payload> {
10
+ protected readonly logger: ILogger;
11
+ protected readonly maxIterations: number;
12
+ protected readonly minIterations: number;
13
+ protected readonly blockingScore: number;
14
+ protected readonly cipherUtils: ICipherUtils;
15
+ protected readonly hashUtils: IHashUtils;
16
+ protected hash: string;
17
+ constructor(config: IConfiguration, cookieString: string, cipherUtils: ICipherUtils, hashUtils: IHashUtils);
18
+ protected decrypt(context: IContext): Promise<BotDefenderTokenV3Payload>;
19
+ protected decryptPayload(encryptedCookie: string, salt: string, iterations: number): Promise<BotDefenderTokenV3Payload>;
20
+ protected validate(context: IContext): Promise<boolean>;
21
+ protected getSignedWithFields(context: IContext): string[];
22
+ protected getSignedWithField(char: TokenSignField, { requestData }: IContext): string;
23
+ isExpired(): boolean;
24
+ isHighScore(): boolean;
25
+ get timestamp(): number;
26
+ get uuid(): string;
27
+ get vid(): string;
28
+ get score(): number;
29
+ get action(): BlockAction;
30
+ get hmac(): string;
31
+ }