perimeterx-js-core 0.7.1 → 0.8.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 (189) hide show
  1. package/lib/action/utils.js +1 -1
  2. package/lib/activities/HttpActivityClient.d.ts +5 -5
  3. package/lib/activities/HttpActivityClient.js +28 -39
  4. package/lib/activities/HttpBatchedActivityClient.d.ts +2 -2
  5. package/lib/activities/HttpBatchedActivityClient.js +19 -32
  6. package/lib/activities/model/AsyncActivity.d.ts +14 -0
  7. package/lib/activities/model/{ActivityDetails.d.ts → AsyncActivityDetails.d.ts} +6 -23
  8. package/lib/activities/model/CommonActivityDetails.d.ts +40 -0
  9. package/lib/activities/model/CommonActivityDetails.js +1 -0
  10. package/lib/activities/model/HeaderEntry.d.ts +4 -0
  11. package/lib/activities/model/HeaderEntry.js +1 -0
  12. package/lib/activities/model/index.d.ts +4 -2
  13. package/lib/activities/model/index.js +4 -2
  14. package/lib/activities/utils.d.ts +6071 -9
  15. package/lib/activities/utils.js +111 -35
  16. package/lib/additional_activity_handler/AdditionalActivityHandler.d.ts +1 -1
  17. package/lib/additional_activity_handler/AdditionalActivityHandlerUtils.js +4 -12
  18. package/lib/config/ConfigurationParams.d.ts +1 -1
  19. package/lib/config/IConfiguration.d.ts +1 -1
  20. package/lib/config/StaticConfigurationBase.d.ts +1 -1
  21. package/lib/config/StaticConfigurationBase.js +3 -1
  22. package/lib/context/DefaultContext.d.ts +1 -1
  23. package/lib/context/DefaultContext.js +40 -8
  24. package/lib/context/interfaces/RiskApiData.d.ts +4 -0
  25. package/lib/cors/DefaultCors.js +21 -31
  26. package/lib/custom_parameters/CustomParametersUtils.js +3 -12
  27. package/lib/enforcer/EnforcerBase.js +51 -62
  28. package/lib/graphql/DefaultGraphQLParser.js +30 -39
  29. package/lib/http/index.d.ts +0 -1
  30. package/lib/http/index.js +0 -1
  31. package/lib/http/interfaces/IBody.d.ts +3 -2
  32. package/lib/http/interfaces/IIncomingRequest.d.ts +4 -0
  33. package/lib/http/interfaces/IURL.d.ts +3 -0
  34. package/lib/http/interfaces/IURLSearchParams.d.ts +9 -0
  35. package/lib/http/interfaces/IURLSearchParams.js +1 -0
  36. package/lib/http/interfaces/index.d.ts +1 -0
  37. package/lib/http/interfaces/index.js +1 -0
  38. package/lib/http/{impl → utils}/FormDataImpl.js +4 -4
  39. package/lib/http/{impl → utils}/MinimalResponseImpl.js +3 -0
  40. package/lib/http/utils/MinimalResponseUtils.js +5 -7
  41. package/lib/http/utils/MultipartFormDataUtils.js +4 -5
  42. package/lib/http/{impl → utils}/OutgoingRequestImpl.d.ts +1 -1
  43. package/lib/http/{impl → utils}/OutgoingRequestImpl.js +5 -1
  44. package/lib/http/utils/URLUtils.d.ts +7 -0
  45. package/lib/http/utils/URLUtils.js +62 -0
  46. package/lib/http/utils/UrlImpl.d.ts +18 -0
  47. package/lib/http/utils/UrlImpl.js +54 -0
  48. package/lib/http/utils/UrlSearchParamsImpl.d.ts +19 -0
  49. package/lib/http/utils/UrlSearchParamsImpl.js +116 -0
  50. package/lib/http/utils/index.d.ts +6 -0
  51. package/lib/http/utils/index.js +6 -0
  52. package/lib/{utils → impl}/base64/AtobBase64Utils.d.ts +1 -1
  53. package/lib/{utils → impl}/base64/BufferBase64Utils.d.ts +1 -1
  54. package/lib/{utils → impl}/base64/BufferBase64Utils.js +1 -1
  55. package/lib/{utils → impl}/base64/JSBase64Base64Utils.d.ts +1 -1
  56. package/lib/{utils → impl}/cipher/CryptoCipherUtils.d.ts +1 -1
  57. package/lib/impl/cipher/CryptoCipherUtils.js +18 -0
  58. package/lib/{utils → impl}/cipher/SubtleCryptoCipherUtils.d.ts +1 -2
  59. package/lib/impl/cipher/SubtleCryptoCipherUtils.js +38 -0
  60. package/lib/{utils → impl}/hash/CryptoHashUtils.d.ts +1 -2
  61. package/lib/impl/hash/CryptoHashUtils.js +10 -0
  62. package/lib/{utils → impl}/hash/CryptoJSHashUtils.d.ts +1 -2
  63. package/lib/{utils → impl}/hash/CryptoJSHashUtils.js +1 -1
  64. package/lib/{utils → impl}/hash/SubtleCryptoHashUtils.d.ts +1 -2
  65. package/lib/impl/hash/SubtleCryptoHashUtils.js +21 -0
  66. package/lib/{utils → impl}/hmac/CryptoHmacUtils.d.ts +1 -2
  67. package/lib/{utils → impl}/hmac/CryptoHmacUtils.js +2 -1
  68. package/lib/{utils → impl}/hmac/CryptoJSHmacUtils.d.ts +1 -2
  69. package/lib/{utils → impl}/hmac/CryptoJSHmacUtils.js +1 -1
  70. package/lib/{http/impl → impl/http}/phin/PhinHttpClient.d.ts +1 -1
  71. package/lib/impl/http/phin/PhinHttpClient.js +31 -0
  72. package/lib/{http/impl → impl/http}/phin/PhinIncomingResponse.d.ts +2 -2
  73. package/lib/impl/http/phin/PhinIncomingResponse.js +23 -0
  74. package/lib/{utils → impl}/ip_range_checker/DefaultIpRangeChecker.d.ts +1 -1
  75. package/lib/{utils → impl}/request_id_generator/UuidRequestIdGenerator.d.ts +1 -1
  76. package/lib/{utils → impl}/url_parser/DefaultUrlParser.d.ts +4 -4
  77. package/lib/{utils → impl}/url_parser/DefaultUrlParser.js +9 -2
  78. package/lib/logger/HttpLogServiceClient.js +27 -35
  79. package/lib/logger/LoggerBase.js +2 -0
  80. package/lib/phase/impl/AdditionalActivityHandlerPhase.js +4 -15
  81. package/lib/phase/impl/CompositePhase.js +9 -19
  82. package/lib/phase/impl/CreateBlockResponsePhase.js +24 -35
  83. package/lib/phase/impl/DecideActionPhase.js +11 -21
  84. package/lib/phase/impl/EnrichContextFromRequestPhase.js +28 -45
  85. package/lib/phase/impl/EnrichContextFromResponsePhase.js +12 -23
  86. package/lib/phase/impl/FilterPhase.js +3 -13
  87. package/lib/phase/impl/FirstPartyPhase.js +21 -32
  88. package/lib/phase/impl/ModifyIncomingRequestPhase.js +4 -14
  89. package/lib/phase/impl/ModifyOutgoingResponsePhase.js +7 -18
  90. package/lib/phase/impl/ParseTokenPhase.js +5 -15
  91. package/lib/phase/impl/PreflightPhase.js +12 -20
  92. package/lib/phase/impl/RiskApiPhase.js +24 -36
  93. package/lib/phase/impl/SendAsyncActivitiesOnRequestPhase.js +8 -20
  94. package/lib/phase/impl/SendAsyncActivitiesOnResponsePhase.js +4 -14
  95. package/lib/phase/impl/SendLogsPhase.js +8 -18
  96. package/lib/phase/impl/TelemetryPhase.js +9 -21
  97. package/lib/products/account_defender/AccountDefender.js +16 -35
  98. package/lib/products/bot_defender/BotDefender.js +51 -72
  99. package/lib/products/bot_defender/BotDefenderActionData.js +2 -0
  100. package/lib/products/bot_defender/block/DefaultBotDefenderBlocker.d.ts +4 -4
  101. package/lib/products/bot_defender/block/DefaultBotDefenderBlocker.js +5 -3
  102. package/lib/products/bot_defender/block/captcha/CaptchaBlocker.d.ts +7 -4
  103. package/lib/products/bot_defender/block/captcha/CaptchaBlocker.js +12 -8
  104. package/lib/products/bot_defender/block/captcha/HtmlCaptchaBlocker.d.ts +1 -1
  105. package/lib/products/bot_defender/block/captcha/HtmlCaptchaBlocker.js +1 -0
  106. package/lib/products/bot_defender/block/captcha/JsonCaptchaBlocker.d.ts +1 -1
  107. package/lib/products/bot_defender/block/captcha/JsonCaptchaBlocker.js +5 -4
  108. package/lib/products/bot_defender/block/captcha/MobileCaptchaBlocker.d.ts +2 -2
  109. package/lib/products/bot_defender/block/captcha/MobileCaptchaBlocker.js +2 -0
  110. package/lib/products/bot_defender/block/utils.js +1 -2
  111. package/lib/products/bot_defender/filter/DefaultBotDefenderFilter.js +2 -0
  112. package/lib/products/bot_defender/first_party/DefaultBotDefenderFirstParty.js +51 -68
  113. package/lib/products/bot_defender/first_party/constants.d.ts +0 -4
  114. package/lib/products/bot_defender/first_party/constants.js +0 -7
  115. package/lib/products/credential_intelligence/CredentialIntelligence.js +42 -63
  116. package/lib/products/credential_intelligence/endpoint/CredentialEndpoint.js +15 -23
  117. package/lib/products/credential_intelligence/endpoint/CredentialEndpointManager.js +9 -22
  118. package/lib/products/credential_intelligence/endpoint/extractor/BodyCredentialExtractor.d.ts +5 -5
  119. package/lib/products/credential_intelligence/endpoint/extractor/BodyCredentialExtractor.js +22 -33
  120. package/lib/products/credential_intelligence/endpoint/extractor/CustomCredentialExtractor.js +8 -18
  121. package/lib/products/credential_intelligence/endpoint/extractor/HeaderCredentialExtractor.js +2 -0
  122. package/lib/products/credential_intelligence/endpoint/extractor/QueryParamCredentialExtractor.d.ts +1 -1
  123. package/lib/products/credential_intelligence/endpoint/extractor/QueryParamCredentialExtractor.js +11 -16
  124. package/lib/products/credential_intelligence/endpoint/hash_protocol/MultistepHashProtocol.js +11 -21
  125. package/lib/products/credential_intelligence/endpoint/hash_protocol/SingleStepAndMultistepHashProtocol.js +9 -18
  126. package/lib/products/credential_intelligence/endpoint/hash_protocol/SingleStepHashProtocol.js +13 -25
  127. package/lib/products/credential_intelligence/endpoint/login_successful/BodyLoginSuccessfulParser.js +4 -14
  128. package/lib/products/credential_intelligence/endpoint/login_successful/CustomLoginSuccessfulParser.js +10 -20
  129. package/lib/products/credential_intelligence/endpoint/login_successful/HeaderLoginSuccessfulParser.js +7 -16
  130. package/lib/products/credential_intelligence/endpoint/login_successful/LoginSuccessfulParserFactory.js +7 -6
  131. package/lib/products/credential_intelligence/endpoint/login_successful/StatusLoginSuccessfulParser.js +3 -13
  132. package/lib/products/credential_intelligence/endpoint/matcher/ExactPathEndpointMatcher.js +2 -0
  133. package/lib/products/credential_intelligence/endpoint/matcher/RegexPathEndpointMatcher.js +2 -0
  134. package/lib/pxde/DefaultDataEnrichment.js +47 -61
  135. package/lib/pxhd/PXHDUtils.js +2 -2
  136. package/lib/risk_api/client/PostRiskApiClientBase.d.ts +7 -12
  137. package/lib/risk_api/client/PostRiskApiClientBase.js +70 -171
  138. package/lib/risk_api/model/RiskActivity.d.ts +5 -45
  139. package/lib/risk_api/risk_response/IRiskResponse.d.ts +1 -0
  140. package/lib/risk_api/risk_response/RiskResponseBase.d.ts +1 -0
  141. package/lib/risk_api/risk_response/RiskResponseBase.js +20 -33
  142. package/lib/risk_api/risk_response/v2/DefaultRiskResponseV2.d.ts +1 -1
  143. package/lib/risk_api/risk_response/v2/DefaultRiskResponseV2.js +3 -5
  144. package/lib/risk_api/risk_response/v2/RiskResponseV2Payload.d.ts +1 -0
  145. package/lib/risk_api/risk_response/v3/DefaultRiskResponseV3.js +2 -4
  146. package/lib/risk_api/risk_response/v3/RiskResponseV3Payload.d.ts +1 -0
  147. package/lib/risk_token/parser/TokenParserBase.js +25 -36
  148. package/lib/risk_token/token/TokenBase.js +24 -29
  149. package/lib/risk_token/token/v2/DefaultTokenV2.js +34 -49
  150. package/lib/risk_token/token/v3/DefaultTokenV3.js +63 -77
  151. package/lib/telemetry/DefaultTelemetry.js +46 -58
  152. package/lib/utils/base64/index.d.ts +0 -3
  153. package/lib/utils/base64/index.js +0 -3
  154. package/lib/utils/cipher/index.d.ts +1 -3
  155. package/lib/utils/cipher/index.js +1 -2
  156. package/lib/utils/constants.d.ts +2 -1
  157. package/lib/utils/constants.js +2 -1
  158. package/lib/utils/hash/index.d.ts +0 -3
  159. package/lib/utils/hash/index.js +0 -3
  160. package/lib/utils/hmac/index.d.ts +0 -2
  161. package/lib/utils/hmac/index.js +0 -2
  162. package/lib/utils/ip_range_checker/index.d.ts +1 -2
  163. package/lib/utils/ip_range_checker/index.js +1 -1
  164. package/lib/utils/request_id_generator/index.d.ts +1 -2
  165. package/lib/utils/request_id_generator/index.js +1 -1
  166. package/lib/utils/url_parser/index.d.ts +0 -1
  167. package/lib/utils/url_parser/index.js +0 -1
  168. package/lib/utils/utils.d.ts +1 -7
  169. package/lib/utils/utils.js +3 -25
  170. package/package.json +12 -6
  171. package/lib/activities/model/Activity.d.ts +0 -13
  172. package/lib/http/impl/index.d.ts +0 -4
  173. package/lib/http/impl/index.js +0 -4
  174. package/lib/http/impl/phin/PhinHttpClient.js +0 -41
  175. package/lib/http/impl/phin/PhinIncomingResponse.js +0 -38
  176. package/lib/utils/cipher/CryptoCipherUtils.js +0 -28
  177. package/lib/utils/cipher/SubtleCryptoCipherUtils.js +0 -47
  178. package/lib/utils/hash/CryptoHashUtils.js +0 -20
  179. package/lib/utils/hash/SubtleCryptoHashUtils.js +0 -31
  180. /package/lib/activities/model/{Activity.js → AsyncActivity.js} +0 -0
  181. /package/lib/activities/model/{ActivityDetails.js → AsyncActivityDetails.js} +0 -0
  182. /package/lib/http/{impl → utils}/FormDataImpl.d.ts +0 -0
  183. /package/lib/http/{impl → utils}/MinimalResponseImpl.d.ts +0 -0
  184. /package/lib/{utils → impl}/base64/AtobBase64Utils.js +0 -0
  185. /package/lib/{utils → impl}/base64/JSBase64Base64Utils.js +0 -0
  186. /package/lib/{http/impl → impl/http}/phin/index.d.ts +0 -0
  187. /package/lib/{http/impl → impl/http}/phin/index.js +0 -0
  188. /package/lib/{utils → impl}/ip_range_checker/DefaultIpRangeChecker.js +0 -0
  189. /package/lib/{utils → impl}/request_id_generator/UuidRequestIdGenerator.js +0 -0
@@ -1,54 +1,41 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { RiskStatus } from '../model';
11
2
  export class RiskResponseBase {
3
+ response;
4
+ riskResponse;
12
5
  constructor(response) {
13
6
  this.response = response;
14
7
  }
15
- validate() {
16
- return __awaiter(this, void 0, void 0, function* () {
17
- try {
18
- this.riskResponse = (yield this.response.json());
19
- return this.status === RiskStatus.SUCCESS && this.validateRiskResponseScore();
20
- }
21
- catch (e) {
22
- return false;
23
- }
24
- });
8
+ async validate() {
9
+ try {
10
+ this.riskResponse = (await this.response.json());
11
+ return this.status === RiskStatus.SUCCESS && this.validateRiskResponseScore();
12
+ }
13
+ catch (e) {
14
+ return false;
15
+ }
25
16
  }
26
17
  get status() {
27
- var _a;
28
- return (_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.status;
18
+ return this.riskResponse?.status;
29
19
  }
30
20
  get action() {
31
- var _a;
32
- return (_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.action;
21
+ return this.riskResponse?.action;
33
22
  }
34
23
  get message() {
35
- var _a;
36
- return (_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.message;
24
+ return this.riskResponse?.message;
37
25
  }
38
26
  get pxhd() {
39
- var _a;
40
- return (_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.pxhd;
27
+ return this.riskResponse?.pxhd;
41
28
  }
42
29
  get uuid() {
43
- var _a;
44
- return (_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.uuid;
30
+ return this.riskResponse?.uuid;
45
31
  }
46
32
  get dataEnrichment() {
47
- var _a;
48
- return (_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.data_enrichment;
33
+ return this.riskResponse?.data_enrichment;
49
34
  }
50
35
  get pxhdDomain() {
51
- var _a;
52
- return (_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.pxhdDomain;
36
+ return this.riskResponse?.pxhdDomain;
37
+ }
38
+ get additionalRiskInfo() {
39
+ return this.riskResponse?.additional_risk_info;
53
40
  }
54
41
  }
@@ -1,6 +1,6 @@
1
1
  import { IIncomingResponse } from '../../../http';
2
- import { RiskResponseBase } from '../RiskResponseBase';
3
2
  import { TokenVersion } from '../../../risk_token';
3
+ import { RiskResponseBase } from '../RiskResponseBase';
4
4
  export declare class DefaultRiskResponseV2 extends RiskResponseBase<TokenVersion.V2> {
5
5
  constructor(response: IIncomingResponse);
6
6
  get score(): number;
@@ -4,15 +4,13 @@ export class DefaultRiskResponseV2 extends RiskResponseBase {
4
4
  super(response);
5
5
  }
6
6
  get score() {
7
- var _a, _b;
8
- return typeof ((_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.cookie_cfg_block_result) === 'undefined'
7
+ return typeof this.riskResponse?.cookie_cfg_block_result === 'undefined'
9
8
  ? undefined
10
- : ((_b = this.riskResponse) === null || _b === void 0 ? void 0 : _b.cookie_cfg_block_result) === '1'
9
+ : this.riskResponse?.cookie_cfg_block_result === '1'
11
10
  ? 100
12
11
  : 0;
13
12
  }
14
13
  validateRiskResponseScore() {
15
- var _a;
16
- return ['0', '1'].includes((_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.cookie_cfg_block_result);
14
+ return ['0', '1'].includes(this.riskResponse?.cookie_cfg_block_result);
17
15
  }
18
16
  }
@@ -10,4 +10,5 @@ export type RiskResponseV2Payload = {
10
10
  pxhdDomain?: string;
11
11
  message?: string;
12
12
  data_enrichment?: PXDE;
13
+ additional_risk_info?: string;
13
14
  };
@@ -4,11 +4,9 @@ export class DefaultRiskResponseV3 extends RiskResponseBase {
4
4
  super(response);
5
5
  }
6
6
  get score() {
7
- var _a;
8
- return (_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.score;
7
+ return this.riskResponse?.score;
9
8
  }
10
9
  validateRiskResponseScore() {
11
- var _a;
12
- return typeof ((_a = this.riskResponse) === null || _a === void 0 ? void 0 : _a.score) === 'number';
10
+ return typeof this.riskResponse?.score === 'number';
13
11
  }
14
12
  }
@@ -10,4 +10,5 @@ export type RiskResponseV3Payload = {
10
10
  pxhdDomain?: string;
11
11
  message?: string;
12
12
  data_enrichment?: PXDE;
13
+ additional_risk_info?: string;
13
14
  };
@@ -1,38 +1,29 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { X_PX_AUTHORIZATION_HEADER_NAME, X_PX_BYPASS_REASON_HEADER_NAME, X_PX_ORIGINAL_TOKEN_HEADER_NAME, } from '../../utils';
11
2
  import { TokenParseResult } from '../TokenParseResult';
12
3
  import { COOKIE_SPLIT_DELIMITER } from '../constants';
13
4
  import { convertMobileTokenVersionToCookieName } from '../utils';
14
5
  export class TokenParserBase {
6
+ config;
7
+ options;
15
8
  constructor(config, options) {
16
9
  this.config = config;
17
10
  this.options = options;
18
11
  }
19
- parseToken(context) {
20
- return __awaiter(this, void 0, void 0, function* () {
21
- const tokenData = this.initializeToken(context);
22
- try {
23
- if (!tokenData.token) {
24
- this.config.logger.debug('no token found');
25
- yield this.handleMobileErrorIfNeeded(context, tokenData);
26
- return tokenData;
27
- }
28
- tokenData.tokenParseResult = yield tokenData.token.verify(context);
29
- }
30
- catch (e) {
31
- this.config.logger.debug(`could not parse token - ${e}`);
32
- tokenData.tokenParseResult = tokenData.token ? TokenParseResult.DECRYPTION_FAILED : TokenParseResult.NONE;
12
+ async parseToken(context) {
13
+ const tokenData = this.initializeToken(context);
14
+ try {
15
+ if (!tokenData.token) {
16
+ this.config.logger.debug('no token found');
17
+ await this.handleMobileErrorIfNeeded(context, tokenData);
18
+ return tokenData;
33
19
  }
34
- return tokenData;
35
- });
20
+ tokenData.tokenParseResult = await tokenData.token.verify(context);
21
+ }
22
+ catch (e) {
23
+ this.config.logger.debug(`could not parse token - ${e}`);
24
+ tokenData.tokenParseResult = tokenData.token ? TokenParseResult.DECRYPTION_FAILED : TokenParseResult.NONE;
25
+ }
26
+ return tokenData;
36
27
  }
37
28
  initializeToken({ requestData, isMobile }) {
38
29
  if (isMobile) {
@@ -59,7 +50,7 @@ export class TokenParserBase {
59
50
  }
60
51
  getMobileToken(mobileToken) {
61
52
  try {
62
- if (!mobileToken || (mobileToken === null || mobileToken === void 0 ? void 0 : mobileToken.indexOf(COOKIE_SPLIT_DELIMITER)) === -1) {
53
+ if (!mobileToken || mobileToken?.indexOf(COOKIE_SPLIT_DELIMITER) === -1) {
63
54
  return null;
64
55
  }
65
56
  const [tokenVersion, ...cookieString] = mobileToken.split(COOKIE_SPLIT_DELIMITER);
@@ -80,15 +71,13 @@ export class TokenParserBase {
80
71
  token: this.createToken(this.config, cookies, this.options),
81
72
  };
82
73
  }
83
- handleMobileErrorIfNeeded(context, { mobileData }) {
84
- return __awaiter(this, void 0, void 0, function* () {
85
- if (!mobileData || !mobileData.mobileError || !mobileData.originalToken) {
86
- return;
87
- }
88
- mobileData.originalTokenParseResult = yield mobileData.originalToken.verify(context);
89
- if (mobileData.originalTokenParseResult === TokenParseResult.SUCCESSFUL) {
90
- mobileData.decodedOriginalToken = mobileData.originalToken.getPayloadString();
91
- }
92
- });
74
+ async handleMobileErrorIfNeeded(context, { mobileData }) {
75
+ if (!mobileData || !mobileData.mobileError || !mobileData.originalToken) {
76
+ return;
77
+ }
78
+ mobileData.originalTokenParseResult = await mobileData.originalToken.verify(context);
79
+ if (mobileData.originalTokenParseResult === TokenParseResult.SUCCESSFUL) {
80
+ mobileData.decodedOriginalToken = mobileData.originalToken.getPayloadString();
81
+ }
93
82
  }
94
83
  }
@@ -1,14 +1,11 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { TokenParseResult } from '../TokenParseResult';
11
2
  export class TokenBase {
3
+ logger;
4
+ payload;
5
+ cookieString;
6
+ cookieSecret;
7
+ cookieMaxLength;
8
+ isValidated;
12
9
  constructor(config, cookieString) {
13
10
  this.logger = config.logger;
14
11
  this.cookieSecret = config.cookieSecret;
@@ -16,27 +13,25 @@ export class TokenBase {
16
13
  this.cookieString = cookieString;
17
14
  this.isValidated = false;
18
15
  }
19
- verify(context) {
20
- return __awaiter(this, void 0, void 0, function* () {
21
- if (!this.cookieString) {
22
- return TokenParseResult.NONE;
23
- }
24
- if (this.isValid()) {
25
- return TokenParseResult.SUCCESSFUL;
26
- }
27
- if (this.cookieString.length > this.cookieMaxLength) {
28
- return TokenParseResult.DECRYPTION_FAILED;
29
- }
30
- this.payload = this.payload || (yield this.decrypt(context));
31
- if (!this.payload) {
32
- return TokenParseResult.DECRYPTION_FAILED;
33
- }
34
- if (!(yield this.validate(context))) {
35
- return TokenParseResult.VALIDATION_FAILED;
36
- }
37
- this.isValidated = true;
16
+ async verify(context) {
17
+ if (!this.cookieString) {
18
+ return TokenParseResult.NONE;
19
+ }
20
+ if (this.isValid()) {
38
21
  return TokenParseResult.SUCCESSFUL;
39
- });
22
+ }
23
+ if (this.cookieString.length > this.cookieMaxLength) {
24
+ return TokenParseResult.DECRYPTION_FAILED;
25
+ }
26
+ this.payload = this.payload || (await this.decrypt(context));
27
+ if (!this.payload) {
28
+ return TokenParseResult.DECRYPTION_FAILED;
29
+ }
30
+ if (!(await this.validate(context))) {
31
+ return TokenParseResult.VALIDATION_FAILED;
32
+ }
33
+ this.isValidated = true;
34
+ return TokenParseResult.SUCCESSFUL;
40
35
  }
41
36
  isValid() {
42
37
  return this.isValidated;
@@ -1,16 +1,10 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { BlockAction } from '../../../blocker';
11
2
  import { Algorithm, isValidUuid } from '../../../utils';
12
3
  import { TokenBase } from '../TokenBase';
13
4
  export class DefaultTokenV2 extends TokenBase {
5
+ base64Utils;
6
+ hmacUtils;
7
+ isHighRisk;
14
8
  constructor(config, cookieString, base64Utils, hmacUtils) {
15
9
  super(config, cookieString);
16
10
  this.base64Utils = base64Utils;
@@ -20,7 +14,7 @@ export class DefaultTokenV2 extends TokenBase {
20
14
  decrypt(context) {
21
15
  try {
22
16
  const payload = this.decode(this.cookieString);
23
- 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) {
17
+ if (payload?.t == null || payload?.h == null || payload?.u == null || payload?.v == null) {
24
18
  return null;
25
19
  }
26
20
  return payload;
@@ -30,32 +24,30 @@ export class DefaultTokenV2 extends TokenBase {
30
24
  }
31
25
  return null;
32
26
  }
33
- validate(context) {
34
- return __awaiter(this, void 0, void 0, function* () {
35
- if (typeof this.payload.t !== 'number' ||
36
- !isValidUuid(this.payload.v) ||
37
- !isValidUuid(this.payload.u) ||
38
- !this.payload.h) {
39
- return false;
40
- }
41
- try {
42
- const signingFields = this.getSigningFields(context);
43
- const passHmac = yield this.calculateHmac(this.getHashParam('0', signingFields));
44
- if (passHmac === this.payload.h) {
45
- this.isHighRisk = false;
46
- return true;
47
- }
48
- const blockHmac = yield this.calculateHmac(this.getHashParam('1', signingFields));
49
- if (blockHmac === this.payload.h) {
50
- this.isHighRisk = true;
51
- return true;
52
- }
27
+ async validate(context) {
28
+ if (typeof this.payload.t !== 'number' ||
29
+ !isValidUuid(this.payload.v) ||
30
+ !isValidUuid(this.payload.u) ||
31
+ !this.payload.h) {
32
+ return false;
33
+ }
34
+ try {
35
+ const signingFields = this.getSigningFields(context);
36
+ const passHmac = await this.calculateHmac(this.getHashParam('0', signingFields));
37
+ if (passHmac === this.payload.h) {
38
+ this.isHighRisk = false;
39
+ return true;
53
40
  }
54
- catch (e) {
55
- this.logger.debug(`cookie v2 validation caught error: ${e}`);
41
+ const blockHmac = await this.calculateHmac(this.getHashParam('1', signingFields));
42
+ if (blockHmac === this.payload.h) {
43
+ this.isHighRisk = true;
44
+ return true;
56
45
  }
57
- return false;
58
- });
46
+ }
47
+ catch (e) {
48
+ this.logger.debug(`cookie v2 validation caught error: ${e}`);
49
+ }
50
+ return false;
59
51
  }
60
52
  decode(cookieString) {
61
53
  const decodedCookie = this.base64Utils.base64Decode(cookieString);
@@ -71,34 +63,27 @@ export class DefaultTokenV2 extends TokenBase {
71
63
  }
72
64
  return hashParam;
73
65
  }
74
- calculateHmac(param) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- const payload = `${this.payload.t}${this.payload.u}${this.payload.v}${param}`;
77
- return this.hmacUtils.createHmac(Algorithm.SHA256, payload, this.cookieSecret);
78
- });
66
+ async calculateHmac(param) {
67
+ const payload = `${this.payload.t}${this.payload.u}${this.payload.v}${param}`;
68
+ return this.hmacUtils.createHmac(Algorithm.SHA256, payload, this.cookieSecret);
79
69
  }
80
70
  isExpired() {
81
- var _a;
82
- return Date.now() > ((_a = this.payload) === null || _a === void 0 ? void 0 : _a.t);
71
+ return Date.now() > this.payload?.t;
83
72
  }
84
73
  isHighScore() {
85
74
  return this.isHighRisk;
86
75
  }
87
76
  get hmac() {
88
- var _a;
89
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.h;
77
+ return this.payload?.h;
90
78
  }
91
79
  get timestamp() {
92
- var _a;
93
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.t;
80
+ return this.payload?.t;
94
81
  }
95
82
  get uuid() {
96
- var _a;
97
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.u;
83
+ return this.payload?.u;
98
84
  }
99
85
  get vid() {
100
- var _a;
101
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.v;
86
+ return this.payload?.v;
102
87
  }
103
88
  get score() {
104
89
  if (this.isValid()) {
@@ -1,17 +1,14 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { Algorithm } from '../../../utils';
11
2
  import { COOKIE_SPLIT_DELIMITER, COOKIE_V3_HMAC_INDEX, COOKIE_V3_ITERATIONS_INDEX, COOKIE_V3_MAXIMUM_SALT_LENGTH, COOKIE_V3_PARTS_COUNT, COOKIE_V3_PAYLOAD_INDEX, COOKIE_V3_SALT_INDEX, } from '../../constants';
12
3
  import { TokenSignField } from './TokenSignField';
13
4
  import { TokenBase } from '../TokenBase';
14
5
  export class DefaultTokenV3 extends TokenBase {
6
+ maxIterations;
7
+ minIterations;
8
+ blockingScore;
9
+ cipherUtils;
10
+ hmacUtils;
11
+ hash;
15
12
  constructor(config, cookieString, cipherUtils, hmacUtils) {
16
13
  super(config, cookieString);
17
14
  this.maxIterations = config.riskCookieMaxIterations;
@@ -21,72 +18,65 @@ export class DefaultTokenV3 extends TokenBase {
21
18
  this.cipherUtils = cipherUtils;
22
19
  this.hmacUtils = hmacUtils;
23
20
  }
24
- decrypt(context) {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- try {
27
- const data = this.cookieString.split(COOKIE_SPLIT_DELIMITER);
28
- if (data.length !== COOKIE_V3_PARTS_COUNT) {
29
- return null;
30
- }
31
- this.hash = data[COOKIE_V3_HMAC_INDEX];
32
- const salt = data[COOKIE_V3_SALT_INDEX];
33
- const iterations = parseInt
34
- ? parseInt(data[COOKIE_V3_ITERATIONS_INDEX])
35
- : Number(data[COOKIE_V3_ITERATIONS_INDEX]);
36
- const encryptedCookie = data[COOKIE_V3_PAYLOAD_INDEX];
37
- if (!iterations || iterations > this.maxIterations || iterations < this.minIterations) {
38
- return null;
39
- }
40
- if (!salt || typeof salt !== 'string' || salt.length > COOKIE_V3_MAXIMUM_SALT_LENGTH) {
41
- return null;
42
- }
43
- if (!encryptedCookie || typeof encryptedCookie !== 'string') {
44
- return null;
45
- }
46
- return yield this.decryptPayload(encryptedCookie, salt, iterations);
21
+ async decrypt(context) {
22
+ try {
23
+ const data = this.cookieString.split(COOKIE_SPLIT_DELIMITER);
24
+ if (data.length !== COOKIE_V3_PARTS_COUNT) {
25
+ return null;
47
26
  }
48
- catch (e) {
49
- this.logger.debug(`cookie v3 decryption failed: ${e}`);
27
+ this.hash = data[COOKIE_V3_HMAC_INDEX];
28
+ const salt = data[COOKIE_V3_SALT_INDEX];
29
+ const iterations = parseInt
30
+ ? parseInt(data[COOKIE_V3_ITERATIONS_INDEX])
31
+ : Number(data[COOKIE_V3_ITERATIONS_INDEX]);
32
+ const encryptedCookie = data[COOKIE_V3_PAYLOAD_INDEX];
33
+ if (!iterations || iterations > this.maxIterations || iterations < this.minIterations) {
34
+ return null;
50
35
  }
51
- return null;
52
- });
53
- }
54
- decryptPayload(encryptedCookie, salt, iterations) {
55
- return __awaiter(this, void 0, void 0, function* () {
56
- try {
57
- const decryptedCookie = yield this.cipherUtils.pbkdf2Decrypt(this.cookieSecret, encryptedCookie, iterations, salt);
58
- if (!decryptedCookie) {
59
- return null;
60
- }
61
- return JSON.parse(decryptedCookie);
36
+ if (!salt || typeof salt !== 'string' || salt.length > COOKIE_V3_MAXIMUM_SALT_LENGTH) {
37
+ return null;
62
38
  }
63
- catch (e) {
64
- this.logger.debug(`error decrypting cookie v3: ${e}`);
39
+ if (!encryptedCookie || typeof encryptedCookie !== 'string') {
65
40
  return null;
66
41
  }
67
- });
42
+ return await this.decryptPayload(encryptedCookie, salt, iterations);
43
+ }
44
+ catch (e) {
45
+ this.logger.debug(`cookie v3 decryption failed: ${e}`);
46
+ }
47
+ return null;
68
48
  }
69
- validate(context) {
70
- return __awaiter(this, void 0, void 0, function* () {
71
- try {
72
- const signedFields = this.getSignedWithFields(context);
73
- const hmacStrBase = this.cookieString.substring(this.cookieString.indexOf(COOKIE_SPLIT_DELIMITER) + 1);
74
- const payload = `${hmacStrBase}${signedFields.join('')}`;
75
- const hash = this.hmacUtils.createHmac(Algorithm.SHA256, payload, this.cookieSecret);
76
- return hash === this.hmac;
77
- }
78
- catch (e) {
79
- this.logger.debug(`error validating cookie v3: ${e}`);
80
- return false;
49
+ async decryptPayload(encryptedCookie, salt, iterations) {
50
+ try {
51
+ const decryptedCookie = await this.cipherUtils.pbkdf2Decrypt(this.cookieSecret, encryptedCookie, iterations, salt);
52
+ if (!decryptedCookie) {
53
+ return null;
81
54
  }
82
- });
55
+ return JSON.parse(decryptedCookie);
56
+ }
57
+ catch (e) {
58
+ this.logger.debug(`error decrypting cookie v3: ${e}`);
59
+ return null;
60
+ }
61
+ }
62
+ async validate(context) {
63
+ try {
64
+ const signedFields = this.getSignedWithFields(context);
65
+ const hmacStrBase = this.cookieString.substring(this.cookieString.indexOf(COOKIE_SPLIT_DELIMITER) + 1);
66
+ const payload = `${hmacStrBase}${signedFields.join('')}`;
67
+ const hash = this.hmacUtils.createHmac(Algorithm.SHA256, payload, this.cookieSecret);
68
+ return hash === this.hmac;
69
+ }
70
+ catch (e) {
71
+ this.logger.debug(`error validating cookie v3: ${e}`);
72
+ return false;
73
+ }
83
74
  }
84
75
  getSignedWithFields(context) {
85
- var _a;
86
76
  if (context.isMobile) {
87
77
  return [];
88
78
  }
89
- else if ((_a = this.payload) === null || _a === void 0 ? void 0 : _a.x) {
79
+ else if (this.payload?.x) {
90
80
  return this.payload.x.split('').map((char) => this.getSignedWithField(char, context));
91
81
  }
92
82
  else {
@@ -104,32 +94,28 @@ export class DefaultTokenV3 extends TokenBase {
104
94
  }
105
95
  }
106
96
  isExpired() {
107
- var _a;
108
- return Date.now() > ((_a = this.payload) === null || _a === void 0 ? void 0 : _a.t);
97
+ return Date.now() > this.payload?.t;
109
98
  }
110
99
  isHighScore() {
111
- var _a;
112
- return ((_a = this.payload) === null || _a === void 0 ? void 0 : _a.s) >= this.blockingScore;
100
+ if (!this.payload) {
101
+ return undefined;
102
+ }
103
+ return this.payload.s >= this.blockingScore;
113
104
  }
114
105
  get timestamp() {
115
- var _a;
116
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.t;
106
+ return this.payload?.t;
117
107
  }
118
108
  get uuid() {
119
- var _a;
120
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.u;
109
+ return this.payload?.u;
121
110
  }
122
111
  get vid() {
123
- var _a;
124
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.v;
112
+ return this.payload?.v;
125
113
  }
126
114
  get score() {
127
- var _a;
128
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.s;
115
+ return this.payload?.s;
129
116
  }
130
117
  get action() {
131
- var _a;
132
- return (_a = this.payload) === null || _a === void 0 ? void 0 : _a.a;
118
+ return this.payload?.a;
133
119
  }
134
120
  get hmac() {
135
121
  return this.hash;