perimeterx-js-core 0.3.0 → 0.4.1

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 (119) hide show
  1. package/README.md +24 -20
  2. package/lib/activities/HttpActivityClient.d.ts +1 -1
  3. package/lib/activities/HttpActivityClient.js +7 -4
  4. package/lib/activities/HttpBatchedActivityClient.d.ts +1 -1
  5. package/lib/activities/utils.js +1 -1
  6. package/lib/block_handler/DefaultBlockResponseGenerator.d.ts +2 -2
  7. package/lib/block_handler/DefaultBlockResponseGenerator.js +7 -2
  8. package/lib/block_handler/IBlockResponseGenerator.d.ts +2 -2
  9. package/lib/block_handler/captcha/CaptchaBlockResponseGenerator.d.ts +2 -2
  10. package/lib/block_handler/captcha/HtmlCaptchaResponseGenerator.d.ts +2 -2
  11. package/lib/block_handler/captcha/HtmlCaptchaResponseGenerator.js +6 -1
  12. package/lib/block_handler/captcha/JsonCaptchaResponseGenerator.d.ts +2 -2
  13. package/lib/block_handler/captcha/JsonCaptchaResponseGenerator.js +8 -4
  14. package/lib/block_handler/captcha/MobileCaptchaResponseGenerator.d.ts +2 -2
  15. package/lib/block_handler/captcha/MobileCaptchaResponseGenerator.js +6 -1
  16. package/lib/config/DefaultConfigurations.js +4 -4
  17. package/lib/context/ContextBase.d.ts +6 -5
  18. package/lib/context/ContextBase.js +11 -14
  19. package/lib/context/DefaultTokenV2Context.d.ts +2 -2
  20. package/lib/context/DefaultTokenV2Context.js +1 -1
  21. package/lib/context/DefaultTokenV3Context.d.ts +2 -2
  22. package/lib/context/DefaultTokenV3Context.js +1 -1
  23. package/lib/context/IContext.d.ts +6 -4
  24. package/lib/custom_parameters/CustomParametersFunction.d.ts +2 -2
  25. package/lib/enforcer/EnforcerBase.d.ts +14 -26
  26. package/lib/enforcer/EnforcerBase.js +13 -12
  27. package/lib/filter/DefaultFilter.js +1 -1
  28. package/lib/first_party/DefaultFirstParty.d.ts +3 -2
  29. package/lib/first_party/DefaultFirstParty.js +44 -34
  30. package/lib/first_party/IFirstParty.d.ts +2 -2
  31. package/lib/first_party/constants.d.ts +2 -9
  32. package/lib/first_party/constants.js +7 -9
  33. package/lib/graphql/DefaultGraphQLParser.js +7 -11
  34. package/lib/http/impl/MinimalResponseImpl.d.ts +13 -0
  35. package/lib/http/impl/MinimalResponseImpl.js +12 -0
  36. package/lib/http/impl/OutgoingRequestImpl.d.ts +14 -0
  37. package/lib/http/impl/OutgoingRequestImpl.js +14 -0
  38. package/lib/http/impl/index.d.ts +2 -0
  39. package/lib/http/impl/index.js +18 -0
  40. package/lib/http/index.d.ts +2 -9
  41. package/lib/http/index.js +2 -11
  42. package/lib/http/interfaces/IBody.d.ts +26 -0
  43. package/lib/http/interfaces/IHttpClient.d.ts +8 -0
  44. package/lib/http/interfaces/IIncomingRequest.d.ts +23 -0
  45. package/lib/http/interfaces/IIncomingResponse.d.ts +9 -0
  46. package/lib/http/interfaces/IMinimalResponse.d.ts +6 -0
  47. package/lib/http/interfaces/IOutgoingRequest.d.ts +11 -0
  48. package/lib/http/interfaces/IOutgoingRequest.js +2 -0
  49. package/lib/http/interfaces/IOutgoingResponse.d.ts +17 -0
  50. package/lib/http/interfaces/IOutgoingResponse.js +2 -0
  51. package/lib/http/interfaces/ReadOnlyHeaders.d.ts +3 -0
  52. package/lib/http/interfaces/ReadOnlyHeaders.js +25 -0
  53. package/lib/http/interfaces/index.d.ts +8 -0
  54. package/lib/http/interfaces/index.js +24 -0
  55. package/lib/http/utils/MinimalResponseUtils.d.ts +4 -0
  56. package/lib/http/utils/MinimalResponseUtils.js +39 -0
  57. package/lib/http/utils/index.d.ts +1 -3
  58. package/lib/http/utils/index.js +2 -5
  59. package/lib/pxde/IDataEnrichment.d.ts +1 -1
  60. package/lib/pxhd/PXHDUtils.d.ts +5 -2
  61. package/lib/pxhd/PXHDUtils.js +17 -2
  62. package/lib/risk_api/PostRiskApiClient.d.ts +3 -2
  63. package/lib/risk_api/PostRiskApiClient.js +14 -11
  64. package/lib/risk_api/S2SErrorHandler.d.ts +2 -2
  65. package/lib/risk_api/S2SErrorHandler.js +1 -1
  66. package/lib/risk_api/risk_response_handler/IRiskResponseHandler.d.ts +2 -2
  67. package/lib/risk_api/risk_response_handler/RiskResponseHandlerBase.d.ts +2 -2
  68. package/lib/risk_token/token/v2/DefaultBotDefenderTokenV2.js +12 -4
  69. package/lib/telemetry/DefaultTelemetry.d.ts +2 -1
  70. package/lib/telemetry/DefaultTelemetry.js +16 -7
  71. package/lib/utils/IScoreRetriever.d.ts +1 -1
  72. package/lib/utils/base64/{DefaultBase64Utils.d.ts → AtobBase64Utils.d.ts} +2 -2
  73. package/lib/utils/base64/AtobBase64Utils.js +15 -0
  74. package/lib/utils/base64/BufferBase64Utils.d.ts +6 -0
  75. package/lib/utils/base64/BufferBase64Utils.js +18 -0
  76. package/lib/utils/base64/JSBase64Base64Utils.d.ts +5 -0
  77. package/lib/utils/base64/JSBase64Base64Utils.js +16 -0
  78. package/lib/utils/base64/index.d.ts +4 -2
  79. package/lib/utils/base64/index.js +18 -3
  80. package/lib/utils/cipher/CryptoCipherUtils.d.ts +12 -0
  81. package/lib/{http/body_reader/StringBodyReader.js → utils/cipher/CryptoCipherUtils.js} +20 -25
  82. package/lib/utils/cipher/SubtleCryptoCipherUtils.d.ts +9 -0
  83. package/lib/utils/cipher/SubtleCryptoCipherUtils.js +94 -0
  84. package/lib/utils/cipher/index.d.ts +2 -0
  85. package/lib/utils/cipher/index.js +16 -0
  86. package/lib/utils/constants.d.ts +1 -1
  87. package/lib/utils/constants.js +1 -1
  88. package/lib/utils/hash/CryptoHmacUtils.d.ts +11 -0
  89. package/lib/utils/hash/CryptoHmacUtils.js +25 -0
  90. package/lib/utils/hash/{DefaultHashUtils.d.ts → CryptoJSHmacUtils.d.ts} +1 -1
  91. package/lib/utils/hash/CryptoJSHmacUtils.js +19 -0
  92. package/lib/utils/hash/index.d.ts +3 -2
  93. package/lib/utils/hash/index.js +17 -3
  94. package/lib/utils/utils.d.ts +1 -2
  95. package/lib/utils/utils.js +14 -3
  96. package/package.json +1 -1
  97. package/lib/http/IHttpClient.d.ts +0 -14
  98. package/lib/http/body_reader/IBodyReader.d.ts +0 -5
  99. package/lib/http/body_reader/StringBodyReader.d.ts +0 -8
  100. package/lib/http/request/DefaultHttpRequest.d.ts +0 -5
  101. package/lib/http/request/DefaultHttpRequest.js +0 -42
  102. package/lib/http/request/HttpRequestBase.d.ts +0 -24
  103. package/lib/http/request/HttpRequestBase.js +0 -77
  104. package/lib/http/request/IHttpRequest.d.ts +0 -31
  105. package/lib/http/response/DefaultHttpResponse.d.ts +0 -5
  106. package/lib/http/response/DefaultHttpResponse.js +0 -32
  107. package/lib/http/response/HttpResponseBase.d.ts +0 -17
  108. package/lib/http/response/HttpResponseBase.js +0 -75
  109. package/lib/http/response/IHttpResponse.d.ts +0 -26
  110. package/lib/http/utils/HttpBody.d.ts +0 -4
  111. package/lib/http/utils/HttpHeaders.d.ts +0 -53
  112. package/lib/http/utils/HttpHeaders.js +0 -123
  113. package/lib/utils/base64/DefaultBase64Utils.js +0 -16
  114. package/lib/utils/hash/DefaultHashUtils.js +0 -19
  115. /package/lib/http/{IHttpClient.js → interfaces/IBody.js} +0 -0
  116. /package/lib/http/{body_reader/IBodyReader.js → interfaces/IHttpClient.js} +0 -0
  117. /package/lib/http/{request/IHttpRequest.js → interfaces/IIncomingRequest.js} +0 -0
  118. /package/lib/http/{response/IHttpResponse.js → interfaces/IIncomingResponse.js} +0 -0
  119. /package/lib/http/{utils/HttpBody.js → interfaces/IMinimalResponse.js} +0 -0
@@ -0,0 +1,23 @@
1
+ import { HttpMethod } from '../utils';
2
+ import { IBody } from './IBody';
3
+ /**
4
+ * describes a request that comes from the end user.
5
+ */
6
+ export interface IIncomingRequest extends IBody {
7
+ /**
8
+ * returns a url.
9
+ */
10
+ url: string;
11
+ /**
12
+ * returns the method.
13
+ */
14
+ readonly method: HttpMethod;
15
+ /**
16
+ * returns the request headers (for read usage).
17
+ */
18
+ readonly headers: Headers;
19
+ /**
20
+ * return the client ip or null.
21
+ */
22
+ readonly clientIP: string | null;
23
+ }
@@ -0,0 +1,9 @@
1
+ import { ReadOnlyHeaders } from './ReadOnlyHeaders';
2
+ import { IBody } from './IBody';
3
+ /**
4
+ * describes a response that is fetched from IHttpClient
5
+ */
6
+ export interface IIncomingResponse extends IBody {
7
+ readonly status: number;
8
+ readonly headers: ReadOnlyHeaders;
9
+ }
@@ -0,0 +1,6 @@
1
+ import { ReadOnlyHeaders } from './ReadOnlyHeaders';
2
+ export interface IMinimalResponse {
3
+ readonly headers: ReadOnlyHeaders;
4
+ readonly body: any;
5
+ readonly status: number;
6
+ }
@@ -0,0 +1,11 @@
1
+ import { HttpMethod } from '../utils';
2
+ import { ReadOnlyHeaders } from './ReadOnlyHeaders';
3
+ /**
4
+ * describes a request that can be sent via IHttpClient.
5
+ */
6
+ export interface IOutgoingRequest {
7
+ readonly url: string;
8
+ readonly method: HttpMethod;
9
+ readonly body: any;
10
+ readonly headers: ReadOnlyHeaders;
11
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Describes a response that is going to be returned to the end user.
3
+ */
4
+ export interface IOutgoingResponse {
5
+ /**
6
+ * the status code, null if it cannot be fetched
7
+ */
8
+ readonly status: number | null;
9
+ /**
10
+ * the response headers (for read usage)
11
+ */
12
+ readonly headers: Headers | null;
13
+ /**
14
+ * the response body.
15
+ */
16
+ readonly body: any;
17
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export declare type ReadOnlyHeaders = Readonly<Record<string, string[]>>;
2
+ export declare const toReadOnlyHeaders: (headers: Headers) => ReadOnlyHeaders;
3
+ export declare const fromReadOnlyHeaders: (headers?: ReadOnlyHeaders) => Headers;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromReadOnlyHeaders = exports.toReadOnlyHeaders = void 0;
4
+ var toReadOnlyHeaders = function (headers) {
5
+ var readonlyHeaders = {};
6
+ headers.forEach(function (headerValue, headerName) {
7
+ var current = readonlyHeaders[headerName] || [];
8
+ readonlyHeaders[headerName] = current.concat([headerValue]);
9
+ });
10
+ return readonlyHeaders;
11
+ };
12
+ exports.toReadOnlyHeaders = toReadOnlyHeaders;
13
+ var fromReadOnlyHeaders = function (headers) {
14
+ if (!headers) {
15
+ return new Headers();
16
+ }
17
+ var init = Object.entries(headers)
18
+ .filter(function (e) { return (e === null || e === void 0 ? void 0 : e[0]) && (e === null || e === void 0 ? void 0 : e[1]); })
19
+ .flatMap(function (_a) {
20
+ var key = _a[0], values = _a[1];
21
+ return values.map(function (value) { return [key, value]; });
22
+ });
23
+ return new Headers(init);
24
+ };
25
+ exports.fromReadOnlyHeaders = fromReadOnlyHeaders;
@@ -0,0 +1,8 @@
1
+ export * from './ReadOnlyHeaders';
2
+ export * from './IHttpClient';
3
+ export * from './IIncomingResponse';
4
+ export * from './IIncomingRequest';
5
+ export * from './IMinimalResponse';
6
+ export * from './IOutgoingRequest';
7
+ export * from './IOutgoingResponse';
8
+ export * from './IBody';
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./ReadOnlyHeaders"), exports);
18
+ __exportStar(require("./IHttpClient"), exports);
19
+ __exportStar(require("./IIncomingResponse"), exports);
20
+ __exportStar(require("./IIncomingRequest"), exports);
21
+ __exportStar(require("./IMinimalResponse"), exports);
22
+ __exportStar(require("./IOutgoingRequest"), exports);
23
+ __exportStar(require("./IOutgoingResponse"), exports);
24
+ __exportStar(require("./IBody"), exports);
@@ -0,0 +1,4 @@
1
+ import { IMinimalResponse } from '..';
2
+ import { IIncomingResponse } from '..';
3
+ export declare const appendHeader: (response: IMinimalResponse, name: string, value: string) => IMinimalResponse;
4
+ export declare const from: (response: IIncomingResponse) => IMinimalResponse;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.from = exports.appendHeader = void 0;
15
+ var __1 = require("..");
16
+ var appendHeader = function (response, name, value) {
17
+ var body = response.body;
18
+ var statusCode = response.status;
19
+ var headers = response.headers;
20
+ var newHeaders = __assign({}, headers);
21
+ newHeaders[name] = (newHeaders[name] || []).concat([value]);
22
+ return new __1.MinimalResponseImpl({
23
+ body: body,
24
+ headers: newHeaders,
25
+ status: statusCode,
26
+ });
27
+ };
28
+ exports.appendHeader = appendHeader;
29
+ var from = function (response) {
30
+ return new (/** @class */ (function () {
31
+ function class_1() {
32
+ this.body = response.body;
33
+ this.headers = response.headers;
34
+ this.status = response.status;
35
+ }
36
+ return class_1;
37
+ }()))();
38
+ };
39
+ exports.from = from;
@@ -1,5 +1,3 @@
1
- export { HttpMethod } from './HttpMethod';
2
1
  export { ContentType } from './ContentType';
3
- export { HttpHeaders, HttpHeadersInit } from './HttpHeaders';
4
- export { HttpBody } from './HttpBody';
5
2
  export * from './constants';
3
+ export * from './HttpMethod';
@@ -14,11 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.HttpHeaders = exports.ContentType = exports.HttpMethod = void 0;
18
- var HttpMethod_1 = require("./HttpMethod");
19
- Object.defineProperty(exports, "HttpMethod", { enumerable: true, get: function () { return HttpMethod_1.HttpMethod; } });
17
+ exports.ContentType = void 0;
20
18
  var ContentType_1 = require("./ContentType");
21
19
  Object.defineProperty(exports, "ContentType", { enumerable: true, get: function () { return ContentType_1.ContentType; } });
22
- var HttpHeaders_1 = require("./HttpHeaders");
23
- Object.defineProperty(exports, "HttpHeaders", { enumerable: true, get: function () { return HttpHeaders_1.HttpHeaders; } });
24
20
  __exportStar(require("./constants"), exports);
21
+ __exportStar(require("./HttpMethod"), exports);
@@ -1,4 +1,4 @@
1
- import { IContext } from '../context/IContext';
1
+ import { IContext } from '../context';
2
2
  export interface IDataEnrichment {
3
3
  /**
4
4
  * If the PXDE cookie is present on the request, parses it and enriches the context with its contents.
@@ -1,8 +1,11 @@
1
- import { IHttpResponse } from '../http';
2
1
  import { IContext } from '../context';
2
+ import { IOutgoingResponse } from '../http';
3
+ import { IMinimalResponse } from '../http';
3
4
  export declare namespace PXHDUtils {
4
5
  const PXHD_SAMESITE_VALUE = "Lax";
5
6
  const PXHD_PATH_VALUE = "/";
6
- const addPxhdToResponse: (context: IContext, httpResponse: IHttpResponse) => IHttpResponse;
7
+ const addPxhdToResponse: (context: IContext, httpResponse: IMinimalResponse) => IMinimalResponse;
8
+ const addPxhdToOutgoingResponse: (context: IContext, response: IOutgoingResponse) => void;
9
+ const addPxhdToMinimalResponse: (context: IContext, response: IMinimalResponse) => IMinimalResponse;
7
10
  const getPxhdCookieValue: (pxhdValue: string) => string;
8
11
  }
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PXHDUtils = void 0;
4
- var http_1 = require("../http");
5
4
  var utils_1 = require("../utils");
5
+ var http_1 = require("../http");
6
+ var MinimalResponseUtils_1 = require("../http/utils/MinimalResponseUtils");
6
7
  var PXHDUtils;
7
8
  (function (PXHDUtils) {
8
9
  PXHDUtils.PXHD_SAMESITE_VALUE = 'Lax';
@@ -10,10 +11,24 @@ var PXHDUtils;
10
11
  PXHDUtils.addPxhdToResponse = function (context, httpResponse) {
11
12
  if (context === null || context === void 0 ? void 0 : context.pxhd) {
12
13
  var setPxhdCookie = PXHDUtils.getPxhdCookieValue(context.pxhd);
13
- httpResponse.headers.append(http_1.SET_COOKIE_HEADER_NAME, setPxhdCookie);
14
+ return (0, MinimalResponseUtils_1.appendHeader)(httpResponse, http_1.SET_COOKIE_HEADER_NAME, setPxhdCookie);
14
15
  }
15
16
  return httpResponse;
16
17
  };
18
+ PXHDUtils.addPxhdToOutgoingResponse = function (context, response) {
19
+ if (!(context === null || context === void 0 ? void 0 : context.pxhd)) {
20
+ return;
21
+ }
22
+ var setPxhdCookie = PXHDUtils.getPxhdCookieValue(context.pxhd);
23
+ response.headers.append(http_1.SET_COOKIE_HEADER_NAME, setPxhdCookie);
24
+ };
25
+ PXHDUtils.addPxhdToMinimalResponse = function (context, response) {
26
+ if (context === null || context === void 0 ? void 0 : context.pxhd) {
27
+ var setPxhdCookie = PXHDUtils.getPxhdCookieValue(context.pxhd);
28
+ return (0, MinimalResponseUtils_1.appendHeader)(response, http_1.SET_COOKIE_HEADER_NAME, setPxhdCookie);
29
+ }
30
+ return response;
31
+ };
17
32
  PXHDUtils.getPxhdCookieValue = function (pxhdValue) {
18
33
  return "".concat(utils_1.PXHD_COOKIE_NAME, "=").concat(pxhdValue, "; path=").concat(PXHDUtils.PXHD_PATH_VALUE, "; SameSite=").concat(PXHDUtils.PXHD_SAMESITE_VALUE);
19
34
  };
@@ -1,11 +1,12 @@
1
1
  import { IContext } from '../context';
2
2
  import { IConfiguration } from '../config';
3
3
  import { RiskActivity } from './model/RiskActivity';
4
- import { IHttpClient, IHttpResponse } from '../http';
5
4
  import { IScoreRetriever } from '../utils';
6
5
  import { IRiskResponseHandler } from './risk_response_handler/IRiskResponseHandler';
7
6
  import { RiskResponseV2 } from './model/RiskResponseV2';
8
7
  import { RiskResponseV3 } from './model/RiskResponseV3';
8
+ import { IHttpClient } from '../http';
9
+ import { IIncomingResponse } from '../http';
9
10
  export declare class PostRiskApiClient<RiskResponseType extends RiskResponseV2 | RiskResponseV3> implements IScoreRetriever {
10
11
  private readonly config;
11
12
  private readonly httpClient;
@@ -35,7 +36,7 @@ export declare class PostRiskApiClient<RiskResponseType extends RiskResponseV2 |
35
36
  private addCustomParamsToAdditional;
36
37
  private addCookieRiskFieldsToAdditional;
37
38
  private formatRiskHeadersField;
38
- protected sendRiskActivity(context: IContext, riskActivity: RiskActivity): Promise<IHttpResponse>;
39
+ protected sendRiskActivity(context: IContext, riskActivity: RiskActivity): Promise<IIncomingResponse>;
39
40
  private getRiskUrl;
40
41
  private getRiskHeaders;
41
42
  private handleS2STimeout;
@@ -43,6 +43,8 @@ var utils_1 = require("../utils");
43
43
  var S2SCallReason_1 = require("./S2SCallReason");
44
44
  var S2SErrorHandler_1 = require("./S2SErrorHandler");
45
45
  var constants_1 = require("./constants");
46
+ var http_2 = require("../http");
47
+ var http_3 = require("../http");
46
48
  var PostRiskApiClient = /** @class */ (function () {
47
49
  function PostRiskApiClient(config, httpClient, riskResponseHandler) {
48
50
  this.config = config;
@@ -94,7 +96,7 @@ var PostRiskApiClient = /** @class */ (function () {
94
96
  case 2:
95
97
  _a.sent();
96
98
  return [2 /*return*/, false];
97
- case 3: return [4 /*yield*/, response.readBody()];
99
+ case 3: return [4 /*yield*/, response.json()];
98
100
  case 4:
99
101
  riskResponse = (_a.sent());
100
102
  return [4 /*yield*/, this.riskResponseHandler.handleRiskResponse(context, response, riskResponse)];
@@ -208,13 +210,14 @@ var PostRiskApiClient = /** @class */ (function () {
208
210
  }
209
211
  };
210
212
  PostRiskApiClient.prototype.formatRiskHeadersField = function (headers) {
211
- var headerEntries = [];
212
- (0, utils_1.removeSensitiveHeaders)(headers, this.config.sensitiveHeaders).forEach(function (values, name) {
213
- values.forEach(function (value) {
214
- headerEntries.push({ name: name, value: value });
215
- });
213
+ var headersWithoutSensitive = (0, utils_1.removeSensitiveHeaders)(headers, this.config.sensitiveHeaders);
214
+ return Object.entries(headersWithoutSensitive).flatMap(function (_a) {
215
+ var key = _a[0], values = _a[1];
216
+ return values.map(function (value) { return ({
217
+ name: key,
218
+ value: value,
219
+ }); });
216
220
  });
217
- return headerEntries;
218
221
  };
219
222
  PostRiskApiClient.prototype.sendRiskActivity = function (context, riskActivity) {
220
223
  return __awaiter(this, void 0, void 0, function () {
@@ -225,8 +228,8 @@ var PostRiskApiClient = /** @class */ (function () {
225
228
  url = this.getRiskUrl();
226
229
  headers = this.getRiskHeaders();
227
230
  body = JSON.stringify(riskActivity);
228
- method = http_1.HttpMethod.POST;
229
- riskRequest = new http_1.DefaultHttpRequest(url, { headers: headers, method: method, body: body });
231
+ method = http_2.HttpMethod.POST;
232
+ riskRequest = new http_3.OutgoingRequestImpl({ url: url, method: method, headers: headers, body: body });
230
233
  this.config.logger.debug("sending risk api to ".concat(url));
231
234
  startTime = Date.now();
232
235
  return [4 /*yield*/, this.httpClient.send(riskRequest, { timeoutMs: this.config.s2sTimeout })];
@@ -245,10 +248,10 @@ var PostRiskApiClient = /** @class */ (function () {
245
248
  };
246
249
  PostRiskApiClient.prototype.getRiskHeaders = function () {
247
250
  var _a;
248
- return new http_1.HttpHeaders((_a = {},
251
+ return _a = {},
249
252
  _a[http_1.CONTENT_TYPE_HEADER_NAME] = [http_1.ContentType.APPLICATION_JSON],
250
253
  _a[http_1.AUTHORIZATION_HEADER_NAME] = [(0, utils_1.getAuthorizationHeader)(this.config.authToken)],
251
- _a));
254
+ _a;
252
255
  };
253
256
  PostRiskApiClient.prototype.handleS2STimeout = function (context) {
254
257
  context.passReason = utils_1.PassReason.S2S_TIMEOUT;
@@ -1,9 +1,9 @@
1
1
  import { IContext } from '../context';
2
- import { IHttpResponse } from '../http';
3
2
  import { RiskResponseV2 } from './model/RiskResponseV2';
4
3
  import { RiskResponseV3 } from './model/RiskResponseV3';
4
+ import { IIncomingResponse } from '../http';
5
5
  export declare class S2SErrorHandler {
6
- handleS2SError(context: IContext, response?: IHttpResponse, responseBody?: RiskResponseV2 | RiskResponseV3, error?: Error): Promise<void>;
6
+ handleS2SError(context: IContext, response?: IIncomingResponse, responseBody?: RiskResponseV2 | RiskResponseV3, error?: Error): Promise<void>;
7
7
  private enrichS2SErrorWithResponse;
8
8
  private enrichS2SErrorWithError;
9
9
  }
@@ -75,7 +75,7 @@ var S2SErrorHandler = /** @class */ (function () {
75
75
  if (!responseBody) return [3 /*break*/, 1];
76
76
  _b = JSON.stringify(responseBody);
77
77
  return [3 /*break*/, 3];
78
- case 1: return [4 /*yield*/, response.readBody()];
78
+ case 1: return [4 /*yield*/, response.text()];
79
79
  case 2:
80
80
  _b = _c.sent();
81
81
  _c.label = 3;
@@ -1,5 +1,5 @@
1
1
  import { IContext } from '../../context';
2
- import { IHttpResponse } from '../../http';
2
+ import { IIncomingResponse } from '../../http';
3
3
  export interface IRiskResponseHandler<RiskResponseType> {
4
- handleRiskResponse(context: IContext, response: IHttpResponse, riskResponse: RiskResponseType): Promise<void>;
4
+ handleRiskResponse(context: IContext, response: IIncomingResponse, riskResponse: RiskResponseType): Promise<void>;
5
5
  }
@@ -1,16 +1,16 @@
1
1
  import { IConfiguration } from '../../config';
2
2
  import { IContext } from '../../context';
3
- import { IHttpResponse } from '../../http';
4
3
  import { IRiskResponseHandler } from './IRiskResponseHandler';
5
4
  import { RiskResponseV2 } from '../model/RiskResponseV2';
6
5
  import { RiskResponseV3 } from '../model/RiskResponseV3';
6
+ import { IIncomingResponse } from '../../http';
7
7
  export declare abstract class RiskResponseHandlerBase<RiskResponseType extends RiskResponseV2 | RiskResponseV3> implements IRiskResponseHandler<RiskResponseType> {
8
8
  protected readonly config: IConfiguration;
9
9
  protected abstract isRiskResponseValid(riskResponse: RiskResponseType): boolean;
10
10
  protected abstract extractScoreFromRiskResponse(riskResponse: RiskResponseType): number;
11
11
  protected abstract setRiskResponseContextFields(context: IContext, riskResponse: RiskResponseType): void;
12
12
  protected constructor(config: IConfiguration);
13
- handleRiskResponse(context: IContext, response: IHttpResponse, riskResponse: RiskResponseType): Promise<void>;
13
+ handleRiskResponse(context: IContext, response: IIncomingResponse, riskResponse: RiskResponseType): Promise<void>;
14
14
  protected handleValidRiskResponse(context: IContext, riskResponse: RiskResponseType): void;
15
15
  protected shouldBlock(score: number): boolean;
16
16
  }
@@ -84,7 +84,7 @@ var DefaultBotDefenderTokenV2 = /** @class */ (function (_super) {
84
84
  };
85
85
  DefaultBotDefenderTokenV2.prototype.validate = function (context) {
86
86
  return __awaiter(this, void 0, void 0, function () {
87
- var signingFields, passHmac, blockHmac;
87
+ var signingFields, passHmac, blockHmac, e_1;
88
88
  return __generator(this, function (_a) {
89
89
  switch (_a.label) {
90
90
  case 0:
@@ -94,22 +94,30 @@ var DefaultBotDefenderTokenV2 = /** @class */ (function (_super) {
94
94
  !this.payload.h) {
95
95
  return [2 /*return*/, false];
96
96
  }
97
+ _a.label = 1;
98
+ case 1:
99
+ _a.trys.push([1, 4, , 5]);
97
100
  signingFields = this.getSigningFields(context);
98
101
  return [4 /*yield*/, this.calculateHmac(this.getHashParam('0', signingFields))];
99
- case 1:
102
+ case 2:
100
103
  passHmac = _a.sent();
101
104
  if (passHmac === this.payload.h) {
102
105
  this.isHighRisk = false;
103
106
  return [2 /*return*/, true];
104
107
  }
105
108
  return [4 /*yield*/, this.calculateHmac(this.getHashParam('1', signingFields))];
106
- case 2:
109
+ case 3:
107
110
  blockHmac = _a.sent();
108
111
  if (blockHmac === this.payload.h) {
109
112
  this.isHighRisk = true;
110
113
  return [2 /*return*/, true];
111
114
  }
112
- return [2 /*return*/, false];
115
+ return [3 /*break*/, 5];
116
+ case 4:
117
+ e_1 = _a.sent();
118
+ this.logger.debug("cookie v2 validation caught error: ".concat(e_1));
119
+ return [3 /*break*/, 5];
120
+ case 5: return [2 /*return*/, false];
113
121
  }
114
122
  });
115
123
  });
@@ -1,8 +1,8 @@
1
1
  import { IConfiguration } from '../config';
2
2
  import { IContext } from '../context';
3
- import { IHttpClient } from '../http';
4
3
  import { IBase64Utils, IHashUtils } from '../utils';
5
4
  import { ITelemetry } from './ITelemetry';
5
+ import { IHttpClient } from '..';
6
6
  export declare class DefaultTelemetry implements ITelemetry {
7
7
  private readonly config;
8
8
  private readonly httpClient;
@@ -13,6 +13,7 @@ export declare class DefaultTelemetry implements ITelemetry {
13
13
  sendTelemetry(context: IContext): Promise<void>;
14
14
  private getTelemetryHeader;
15
15
  private isTelemetryHeaderValid;
16
+ private isTelemetryHmacValid;
16
17
  private sendTelemetryActivity;
17
18
  private createTelemetryRequest;
18
19
  private createTelemetryActivity;
@@ -41,6 +41,8 @@ var http_1 = require("../http");
41
41
  var activities_1 = require("../activities");
42
42
  var utils_1 = require("../utils");
43
43
  var constants_1 = require("./constants");
44
+ var __1 = require("..");
45
+ var __2 = require("..");
44
46
  var DefaultTelemetry = /** @class */ (function () {
45
47
  function DefaultTelemetry(config, httpClient, base64Utils, hashUtils) {
46
48
  this.config = config;
@@ -93,12 +95,11 @@ var DefaultTelemetry = /** @class */ (function () {
93
95
  });
94
96
  };
95
97
  DefaultTelemetry.prototype.getTelemetryHeader = function (context) {
96
- var _a;
97
- return ((_a = context.requestData.headers.get(constants_1.TELEMETRY_HEADER_NAME)) === null || _a === void 0 ? void 0 : _a[0]) || '';
98
+ return context.requestData.request.headers.get(constants_1.TELEMETRY_HEADER_NAME) || '';
98
99
  };
99
100
  DefaultTelemetry.prototype.isTelemetryHeaderValid = function (headerValue) {
100
101
  return __awaiter(this, void 0, void 0, function () {
101
- var decodedValue, splitValue, timestamp, givenHmac, calculatedHmac, curUnixTime, timestampNumber;
102
+ var decodedValue, splitValue, timestamp, givenHmac, curUnixTime, timestampNumber;
102
103
  return __generator(this, function (_a) {
103
104
  this.config.logger.debug('received command to send enforcer telemetry');
104
105
  decodedValue = this.base64Utils.base64Decode(headerValue);
@@ -109,8 +110,7 @@ var DefaultTelemetry = /** @class */ (function () {
109
110
  }
110
111
  timestamp = splitValue[constants_1.TELEMETRY_HEADER_TIMESTAMP_INDEX];
111
112
  givenHmac = splitValue[constants_1.TELEMETRY_HEADER_HMAC_INDEX];
112
- calculatedHmac = this.hashUtils.createHmac(utils_1.Algorithm.SHA256, timestamp, this.config.cookieSecret);
113
- if (givenHmac !== calculatedHmac) {
113
+ if (!this.isTelemetryHmacValid(givenHmac, timestamp)) {
114
114
  this.config.logger.debug("".concat(constants_1.TELEMETRY_HEADER_NAME, " hmac validation failed. original hmac: ").concat(givenHmac, ", timestamp: ").concat(timestamp, "."));
115
115
  return [2 /*return*/, false];
116
116
  }
@@ -124,6 +124,15 @@ var DefaultTelemetry = /** @class */ (function () {
124
124
  });
125
125
  });
126
126
  };
127
+ DefaultTelemetry.prototype.isTelemetryHmacValid = function (givenHmac, timestamp) {
128
+ try {
129
+ return givenHmac === this.hashUtils.createHmac(utils_1.Algorithm.SHA256, timestamp, this.config.cookieSecret);
130
+ }
131
+ catch (e) {
132
+ this.config.logger.debug("caught error validating telemetry hmac: ".concat(e));
133
+ return false;
134
+ }
135
+ };
127
136
  DefaultTelemetry.prototype.sendTelemetryActivity = function (context) {
128
137
  return __awaiter(this, void 0, void 0, function () {
129
138
  var telemetryRequest;
@@ -143,13 +152,13 @@ var DefaultTelemetry = /** @class */ (function () {
143
152
  DefaultTelemetry.prototype.createTelemetryRequest = function (context) {
144
153
  var _a;
145
154
  var url = "".concat(this.config.backendScoreApiUrl).concat(constants_1.TELEMETRY_ENDPOINT);
146
- var method = http_1.HttpMethod.POST;
155
+ var method = __1.HttpMethod.POST;
147
156
  var headers = (_a = {},
148
157
  _a[http_1.CONTENT_TYPE_HEADER_NAME] = [http_1.ContentType.APPLICATION_JSON],
149
158
  _a[http_1.AUTHORIZATION_HEADER_NAME] = [(0, utils_1.getAuthorizationHeader)(this.config.authToken)],
150
159
  _a);
151
160
  var activity = this.createTelemetryActivity(context);
152
- return new http_1.DefaultHttpRequest(url, { method: method, headers: headers, body: JSON.stringify(activity) });
161
+ return new __2.OutgoingRequestImpl({ url: url, method: method, headers: headers, body: JSON.stringify(activity) });
153
162
  };
154
163
  DefaultTelemetry.prototype.createTelemetryActivity = function (context) {
155
164
  var SENSITIVE_CONFIG_FIELDS = ['px_auth_token', 'px_cookie_secret'];
@@ -1,4 +1,4 @@
1
- import { IContext } from '../context/IContext';
1
+ import { IContext } from '../context';
2
2
  export interface IScoreRetriever {
3
3
  /**
4
4
  * @param context - The request context.
@@ -1,5 +1,5 @@
1
1
  import { IBase64Utils } from './IBase64Utils';
2
- export declare class DefaultBase64Utils implements IBase64Utils {
3
- base64Encode(stringToEncode: string): string;
2
+ export declare class AtobBase64Utils implements IBase64Utils {
4
3
  base64Decode(stringToDecode: string): string;
4
+ base64Encode(stringToEncode: string): string;
5
5
  }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AtobBase64Utils = void 0;
4
+ var AtobBase64Utils = /** @class */ (function () {
5
+ function AtobBase64Utils() {
6
+ }
7
+ AtobBase64Utils.prototype.base64Decode = function (stringToDecode) {
8
+ return atob(stringToDecode);
9
+ };
10
+ AtobBase64Utils.prototype.base64Encode = function (stringToEncode) {
11
+ return btoa(stringToEncode);
12
+ };
13
+ return AtobBase64Utils;
14
+ }());
15
+ exports.AtobBase64Utils = AtobBase64Utils;
@@ -0,0 +1,6 @@
1
+ import { IBase64Utils } from './IBase64Utils';
2
+ export declare class BufferBase64Utils implements IBase64Utils {
3
+ static BASE_64_ENCODING: 'base64';
4
+ base64Decode(stringToDecode: string): string;
5
+ base64Encode(stringToEncode: string): string;
6
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BufferBase64Utils = void 0;
4
+ var BufferBase64Utils = /** @class */ (function () {
5
+ function BufferBase64Utils() {
6
+ }
7
+ BufferBase64Utils.prototype.base64Decode = function (stringToDecode) {
8
+ var buffer = Buffer.from(stringToDecode, BufferBase64Utils.BASE_64_ENCODING);
9
+ return buffer.toString();
10
+ };
11
+ BufferBase64Utils.prototype.base64Encode = function (stringToEncode) {
12
+ var buffer = Buffer.from(stringToEncode);
13
+ return buffer.toString(BufferBase64Utils.BASE_64_ENCODING);
14
+ };
15
+ BufferBase64Utils.BASE_64_ENCODING = 'base64';
16
+ return BufferBase64Utils;
17
+ }());
18
+ exports.BufferBase64Utils = BufferBase64Utils;
@@ -0,0 +1,5 @@
1
+ import { IBase64Utils } from './IBase64Utils';
2
+ export declare class JSBase64Base64Utils implements IBase64Utils {
3
+ base64Encode(stringToEncode: string): string;
4
+ base64Decode(stringToDecode: string): string;
5
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JSBase64Base64Utils = void 0;
4
+ var js_base64_1 = require("js-base64");
5
+ var JSBase64Base64Utils = /** @class */ (function () {
6
+ function JSBase64Base64Utils() {
7
+ }
8
+ JSBase64Base64Utils.prototype.base64Encode = function (stringToEncode) {
9
+ return (0, js_base64_1.encode)(stringToEncode);
10
+ };
11
+ JSBase64Base64Utils.prototype.base64Decode = function (stringToDecode) {
12
+ return (0, js_base64_1.decode)(stringToDecode);
13
+ };
14
+ return JSBase64Base64Utils;
15
+ }());
16
+ exports.JSBase64Base64Utils = JSBase64Base64Utils;
@@ -1,2 +1,4 @@
1
- export { IBase64Utils } from './IBase64Utils';
2
- export { DefaultBase64Utils } from './DefaultBase64Utils';
1
+ export * from './IBase64Utils';
2
+ export * from './JSBase64Base64Utils';
3
+ export * from './BufferBase64Utils';
4
+ export * from './AtobBase64Utils';