perimeterx-js-core 0.3.0 → 0.4.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 (119) hide show
  1. package/README.md +16 -9
  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 +7 -0
  54. package/lib/http/interfaces/index.js +23 -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
@@ -1,5 +1,20 @@
1
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
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DefaultBase64Utils = void 0;
4
- var DefaultBase64Utils_1 = require("./DefaultBase64Utils");
5
- Object.defineProperty(exports, "DefaultBase64Utils", { enumerable: true, get: function () { return DefaultBase64Utils_1.DefaultBase64Utils; } });
17
+ __exportStar(require("./IBase64Utils"), exports);
18
+ __exportStar(require("./JSBase64Base64Utils"), exports);
19
+ __exportStar(require("./BufferBase64Utils"), exports);
20
+ __exportStar(require("./AtobBase64Utils"), exports);
@@ -0,0 +1,12 @@
1
+ /// <reference types="node" />
2
+ import * as crypto from 'crypto';
3
+ import { IBase64Utils } from '../base64';
4
+ import { ICipherUtils, Pbkdf2DecryptOptions } from './ICipherUtils';
5
+ declare type CryptoModule = typeof crypto;
6
+ export declare class CryptoCipherUtils implements ICipherUtils {
7
+ private crypto;
8
+ private base64Utils;
9
+ constructor(base64Utils: IBase64Utils, cryptoModule?: CryptoModule);
10
+ pbkdf2Decrypt(secret: string, encryptedString: string, iterations: number, salt: string, options?: Pbkdf2DecryptOptions): Promise<string>;
11
+ }
12
+ export {};
@@ -36,35 +36,30 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.StringBodyReader = void 0;
40
- var StringBodyReader = /** @class */ (function () {
41
- function StringBodyReader(rawBody) {
42
- this.body = rawBody;
39
+ exports.CryptoCipherUtils = void 0;
40
+ var crypto = require("crypto");
41
+ var CryptoCipherUtils = /** @class */ (function () {
42
+ function CryptoCipherUtils(base64Utils, cryptoModule) {
43
+ if (cryptoModule === void 0) { cryptoModule = crypto; }
44
+ this.crypto = cryptoModule;
45
+ this.base64Utils = base64Utils;
43
46
  }
44
- StringBodyReader.prototype.readBody = function (contentType) {
47
+ CryptoCipherUtils.prototype.pbkdf2Decrypt = function (secret, encryptedString, iterations, salt, options) {
45
48
  return __awaiter(this, void 0, void 0, function () {
49
+ var keylen, ivlen, derivation, key, iv, cipher, decrypted;
46
50
  return __generator(this, function (_a) {
47
- if (!contentType) {
48
- return [2 /*return*/, this.body];
49
- }
50
- if (contentType.includes('json')) {
51
- return [2 /*return*/, JSON.parse(this.body)];
52
- }
53
- else if (contentType.includes('form-urlencoded')) {
54
- return [2 /*return*/, this.convertFormUrlEncodedStringToObject(this.body)];
55
- }
56
- return [2 /*return*/, this.body];
51
+ keylen = (options === null || options === void 0 ? void 0 : options.keylen) || 32;
52
+ ivlen = (options === null || options === void 0 ? void 0 : options.ivlen) || 16;
53
+ derivation = this.crypto.pbkdf2Sync(secret, this.base64Utils.base64Decode(salt), iterations, keylen + ivlen, 'sha256');
54
+ key = derivation.subarray(0, keylen);
55
+ iv = derivation.subarray(keylen);
56
+ cipher = this.crypto.createDecipheriv('aes-256-cbc', key, iv);
57
+ decrypted = cipher.update(encryptedString, 'base64', 'utf8');
58
+ decrypted += cipher.final('utf8');
59
+ return [2 /*return*/, decrypted];
57
60
  });
58
61
  });
59
62
  };
60
- StringBodyReader.prototype.convertFormUrlEncodedStringToObject = function (formUrlEncodedString) {
61
- var object = {};
62
- var params = new URLSearchParams(formUrlEncodedString);
63
- params.forEach(function (value, key) {
64
- object[key] = value;
65
- });
66
- return object;
67
- };
68
- return StringBodyReader;
63
+ return CryptoCipherUtils;
69
64
  }());
70
- exports.StringBodyReader = StringBodyReader;
65
+ exports.CryptoCipherUtils = CryptoCipherUtils;
@@ -0,0 +1,9 @@
1
+ import { IBase64Utils } from '../base64';
2
+ import { ICipherUtils, Pbkdf2DecryptOptions } from './ICipherUtils';
3
+ export declare class SubtleCryptoCipherUtils implements ICipherUtils {
4
+ private base64Utils;
5
+ private subtleCrypto;
6
+ constructor(base64Utils: IBase64Utils, subtleCrypto?: SubtleCrypto);
7
+ pbkdf2Decrypt(secret: string, encryptedString: string, iterations: number, salt: string, options?: Pbkdf2DecryptOptions): Promise<string>;
8
+ private base64ToArrayBuffer;
9
+ }
@@ -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.SubtleCryptoCipherUtils = void 0;
40
+ var SubtleCryptoCipherUtils = /** @class */ (function () {
41
+ function SubtleCryptoCipherUtils(base64Utils, subtleCrypto) {
42
+ if (subtleCrypto === void 0) { subtleCrypto = crypto.subtle; }
43
+ this.subtleCrypto = subtleCrypto;
44
+ this.base64Utils = base64Utils;
45
+ }
46
+ SubtleCryptoCipherUtils.prototype.pbkdf2Decrypt = function (secret, encryptedString, iterations, salt, options) {
47
+ return __awaiter(this, void 0, void 0, function () {
48
+ var ivlen, keylen, bitsLength, encodedPassword, encodedSalt, importedKey, params, derivation, derivedKey, iv, cookieBuffer, importedDecryptionKey, decrypted;
49
+ return __generator(this, function (_a) {
50
+ switch (_a.label) {
51
+ case 0:
52
+ ivlen = (options === null || options === void 0 ? void 0 : options.ivlen) || 16;
53
+ keylen = (options === null || options === void 0 ? void 0 : options.keylen) || 32;
54
+ bitsLength = (ivlen + keylen) * 8;
55
+ encodedPassword = new TextEncoder().encode(secret);
56
+ encodedSalt = this.base64ToArrayBuffer(salt);
57
+ return [4 /*yield*/, this.subtleCrypto.importKey('raw', encodedPassword, 'PBKDF2', false, ['deriveBits'])];
58
+ case 1:
59
+ importedKey = _a.sent();
60
+ params = { name: 'PBKDF2', hash: 'SHA-256', salt: encodedSalt, iterations: iterations };
61
+ return [4 /*yield*/, this.subtleCrypto.deriveBits(params, importedKey, bitsLength)];
62
+ case 2:
63
+ derivation = _a.sent();
64
+ derivedKey = derivation.slice(0, keylen);
65
+ iv = derivation.slice(keylen);
66
+ cookieBuffer = this.base64ToArrayBuffer(encryptedString);
67
+ return [4 /*yield*/, this.subtleCrypto.importKey('raw', derivedKey, { name: 'AES-CBC' }, false, [
68
+ 'decrypt',
69
+ ])];
70
+ case 3:
71
+ importedDecryptionKey = _a.sent();
72
+ return [4 /*yield*/, this.subtleCrypto.decrypt({
73
+ name: 'AES-CBC',
74
+ iv: iv,
75
+ }, importedDecryptionKey, cookieBuffer)];
76
+ case 4:
77
+ decrypted = _a.sent();
78
+ return [2 /*return*/, new TextDecoder('utf-8').decode(decrypted)];
79
+ }
80
+ });
81
+ });
82
+ };
83
+ SubtleCryptoCipherUtils.prototype.base64ToArrayBuffer = function (base64String) {
84
+ var binaryString = this.base64Utils.base64Decode(base64String);
85
+ var length = binaryString.length;
86
+ var bytes = new Uint8Array(length);
87
+ binaryString.split('').forEach(function (char, index) {
88
+ bytes[index] = char.charCodeAt(0);
89
+ });
90
+ return bytes.buffer;
91
+ };
92
+ return SubtleCryptoCipherUtils;
93
+ }());
94
+ exports.SubtleCryptoCipherUtils = SubtleCryptoCipherUtils;
@@ -1 +1,3 @@
1
1
  export { ICipherUtils, Pbkdf2DecryptOptions } from './ICipherUtils';
2
+ export * from './SubtleCryptoCipherUtils';
3
+ export * from './CryptoCipherUtils';
@@ -1,2 +1,18 @@
1
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
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./SubtleCryptoCipherUtils"), exports);
18
+ __exportStar(require("./CryptoCipherUtils"), exports);
@@ -7,4 +7,4 @@ export declare const BYPASS_MONITOR_HEADER_VALUE = "1";
7
7
  export declare const X_PX_AUTHORIZATION_HEADER_NAME = "x-px-authorization";
8
8
  export declare const X_PX_ORIGINAL_TOKEN_HEADER_NAME = "x-px-original-token";
9
9
  export declare const X_PX_BYPASS_REASON_HEADER_NAME = "x-px-bypass-reason";
10
- export declare const CORE_MODULE_VERSION = "JS Core 0.3.0";
10
+ export declare const CORE_MODULE_VERSION = "JS Core 0.4.0";
@@ -10,4 +10,4 @@ exports.BYPASS_MONITOR_HEADER_VALUE = '1';
10
10
  exports.X_PX_AUTHORIZATION_HEADER_NAME = 'x-px-authorization';
11
11
  exports.X_PX_ORIGINAL_TOKEN_HEADER_NAME = 'x-px-original-token';
12
12
  exports.X_PX_BYPASS_REASON_HEADER_NAME = 'x-px-bypass-reason';
13
- exports.CORE_MODULE_VERSION = 'JS Core 0.3.0';
13
+ exports.CORE_MODULE_VERSION = 'JS Core 0.4.0';
@@ -0,0 +1,11 @@
1
+ /// <reference types="node" />
2
+ import * as crypto from 'crypto';
3
+ import { IHashUtils } from './IHashUtils';
4
+ import { Algorithm } from '../Algorithm';
5
+ declare type CryptoModule = typeof crypto;
6
+ export declare class CryptoHmacUtils implements IHashUtils {
7
+ private readonly crypto;
8
+ constructor(cryptoModule?: CryptoModule);
9
+ createHmac(algo: Algorithm, payload: string, secret: string): string;
10
+ }
11
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CryptoHmacUtils = void 0;
4
+ var crypto = require("crypto");
5
+ var Algorithm_1 = require("../Algorithm");
6
+ var CryptoHmacUtils = /** @class */ (function () {
7
+ function CryptoHmacUtils(cryptoModule) {
8
+ if (cryptoModule === void 0) { cryptoModule = crypto; }
9
+ this.crypto = cryptoModule;
10
+ }
11
+ CryptoHmacUtils.prototype.createHmac = function (algo, payload, secret) {
12
+ switch (algo) {
13
+ case Algorithm_1.Algorithm.SHA256:
14
+ var hmac = this.crypto.createHmac('sha256', secret);
15
+ hmac.setEncoding('hex');
16
+ hmac.write(payload);
17
+ hmac.end();
18
+ return hmac.read();
19
+ default:
20
+ return '';
21
+ }
22
+ };
23
+ return CryptoHmacUtils;
24
+ }());
25
+ exports.CryptoHmacUtils = CryptoHmacUtils;
@@ -1,5 +1,5 @@
1
1
  import { IHashUtils } from './IHashUtils';
2
2
  import { Algorithm } from '../Algorithm';
3
- export declare class DefaultHashUtils implements IHashUtils {
3
+ export declare class CryptoJSHmacUtils implements IHashUtils {
4
4
  createHmac(algo: Algorithm, payload: string, secret: string): string;
5
5
  }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CryptoJSHmacUtils = void 0;
4
+ var crypto_js_1 = require("crypto-js");
5
+ var Algorithm_1 = require("../Algorithm");
6
+ var CryptoJSHmacUtils = /** @class */ (function () {
7
+ function CryptoJSHmacUtils() {
8
+ }
9
+ CryptoJSHmacUtils.prototype.createHmac = function (algo, payload, secret) {
10
+ switch (algo) {
11
+ case Algorithm_1.Algorithm.SHA256:
12
+ return (0, crypto_js_1.HmacSHA256)(payload, secret).toString(crypto_js_1.enc.Hex);
13
+ default:
14
+ return '';
15
+ }
16
+ };
17
+ return CryptoJSHmacUtils;
18
+ }());
19
+ exports.CryptoJSHmacUtils = CryptoJSHmacUtils;
@@ -1,2 +1,3 @@
1
- export { IHashUtils } from './IHashUtils';
2
- export { DefaultHashUtils } from './DefaultHashUtils';
1
+ export * from './IHashUtils';
2
+ export * from './CryptoHmacUtils';
3
+ export * from './CryptoJSHmacUtils';
@@ -1,5 +1,19 @@
1
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
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DefaultHashUtils = void 0;
4
- var DefaultHashUtils_1 = require("./DefaultHashUtils");
5
- Object.defineProperty(exports, "DefaultHashUtils", { enumerable: true, get: function () { return DefaultHashUtils_1.DefaultHashUtils; } });
17
+ __exportStar(require("./IHashUtils"), exports);
18
+ __exportStar(require("./CryptoHmacUtils"), exports);
19
+ __exportStar(require("./CryptoJSHmacUtils"), exports);
@@ -1,4 +1,3 @@
1
- import { HttpHeaders } from '../http';
2
1
  export declare const isValidEnumValue: <T>(options: Record<string, T>, selection: T) => boolean;
3
2
  export declare const isValidUuid: (uuid: string) => boolean;
4
3
  export declare const getScoreApiDomain: (appId: string) => string;
@@ -7,7 +6,7 @@ export declare const getAuthorizationHeader: (authToken: string) => string;
7
6
  export declare const getExtension: (route: string) => string;
8
7
  export declare const getDecodedUrl: (rawUrl: string) => URL;
9
8
  export declare const removeSensitiveFields: <T extends Record<string, any>>(object: T, sensitiveFields: (keyof T)[]) => T;
10
- export declare const removeSensitiveHeaders: (headers: HttpHeaders, sensitiveHeaderNames: string[]) => HttpHeaders;
9
+ export declare const removeSensitiveHeaders: (headers: Record<string, string[]>, sensitiveHeaderNames: string[]) => Record<string, string[]>;
11
10
  export declare const isRouteInPatterns: (route: string, patterns: Array<string | RegExp>) => boolean;
12
11
  export declare const isRouteMatch: (route: string, pattern: string | RegExp) => boolean;
13
12
  export declare const transferExistingProperties: <FromObj extends Record<string, any>, ToObj extends Record<string, any>>(fromObj: FromObj, toObj: ToObj, propertyMappings: Partial<Record<keyof FromObj, keyof ToObj>>) => void;
@@ -1,4 +1,15 @@
1
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
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -89,11 +100,11 @@ var removeSensitiveFields = function (object, sensitiveFields) {
89
100
  };
90
101
  exports.removeSensitiveFields = removeSensitiveFields;
91
102
  var removeSensitiveHeaders = function (headers, sensitiveHeaderNames) {
92
- var headersCopy = headers.copy();
103
+ var ret = __assign({}, headers);
93
104
  sensitiveHeaderNames.forEach(function (name) {
94
- headersCopy.delete(name);
105
+ delete ret[name];
95
106
  });
96
- return headersCopy;
107
+ return ret;
97
108
  };
98
109
  exports.removeSensitiveHeaders = removeSensitiveHeaders;
99
110
  var isRouteInPatterns = function (route, patterns) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "perimeterx-js-core",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -1,14 +0,0 @@
1
- import { IHttpRequest } from './request/IHttpRequest';
2
- import { IHttpResponse } from './response/IHttpResponse';
3
- export declare type HttpSendOptions = {
4
- timeoutMs?: number;
5
- };
6
- export interface IHttpClient<UnreadBodyType = unknown> {
7
- /**
8
- * A function that sends the provided IHttpRequest and returns a Promise resolving to an IHttpResponse.
9
- * @param request - The request to be sent by the client.
10
- * @param options - Options that the HttpClient should consider
11
- * @returns Promise<IHttpResponse> - A Promise resolving to the response the client received.
12
- */
13
- send(request: IHttpRequest<UnreadBodyType>, options?: HttpSendOptions): Promise<IHttpResponse<UnreadBodyType>>;
14
- }
@@ -1,5 +0,0 @@
1
- import { ContentType, HttpBody } from '../utils';
2
- export interface IBodyReader<UnreadBodyType> {
3
- readonly body: UnreadBodyType | string;
4
- readBody(contentType: ContentType): Promise<HttpBody>;
5
- }
@@ -1,8 +0,0 @@
1
- import { ContentType, HttpBody } from '../utils';
2
- import { IBodyReader } from './IBodyReader';
3
- export declare class StringBodyReader implements IBodyReader<string> {
4
- readonly body: string;
5
- constructor(rawBody: string);
6
- readBody(contentType: ContentType): Promise<HttpBody>;
7
- private convertFormUrlEncodedStringToObject;
8
- }
@@ -1,5 +0,0 @@
1
- import { HttpRequestBase, HttpRequestOptions } from './HttpRequestBase';
2
- export declare class DefaultHttpRequest extends HttpRequestBase<string, DefaultHttpRequest> {
3
- constructor(url: string, options?: HttpRequestOptions<string>);
4
- getUnderlyingRequest(): DefaultHttpRequest;
5
- }
@@ -1,42 +0,0 @@
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 __assign = (this && this.__assign) || function () {
18
- __assign = Object.assign || function(t) {
19
- for (var s, i = 1, n = arguments.length; i < n; i++) {
20
- s = arguments[i];
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
- t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.DefaultHttpRequest = void 0;
30
- var StringBodyReader_1 = require("../body_reader/StringBodyReader");
31
- var HttpRequestBase_1 = require("./HttpRequestBase");
32
- var DefaultHttpRequest = /** @class */ (function (_super) {
33
- __extends(DefaultHttpRequest, _super);
34
- function DefaultHttpRequest(url, options) {
35
- return _super.call(this, url, __assign(__assign({}, options), { bodyReader: (options === null || options === void 0 ? void 0 : options.body) ? new StringBodyReader_1.StringBodyReader(options.body) : undefined })) || this;
36
- }
37
- DefaultHttpRequest.prototype.getUnderlyingRequest = function () {
38
- return this;
39
- };
40
- return DefaultHttpRequest;
41
- }(HttpRequestBase_1.HttpRequestBase));
42
- exports.DefaultHttpRequest = DefaultHttpRequest;
@@ -1,24 +0,0 @@
1
- import { HttpHeaders, HttpHeadersInit, HttpMethod, HttpBody } from '../utils';
2
- import { IHttpRequest } from './IHttpRequest';
3
- import { IBodyReader } from '../body_reader/IBodyReader';
4
- export declare type HttpRequestOptions<UnreadBodyType> = {
5
- method?: HttpMethod;
6
- headers?: HttpHeaders | HttpHeadersInit;
7
- body?: UnreadBodyType;
8
- };
9
- export declare type HttpRequestBaseOptions<UnreadBodyType> = {
10
- method?: HttpMethod;
11
- headers?: HttpHeaders | HttpHeadersInit;
12
- bodyReader?: IBodyReader<UnreadBodyType>;
13
- };
14
- export declare abstract class HttpRequestBase<UnreadBodyType, Req> implements IHttpRequest<UnreadBodyType, Req> {
15
- readonly url: string;
16
- readonly method: HttpMethod;
17
- readonly headers: HttpHeaders;
18
- protected readonly bodyReader: IBodyReader<UnreadBodyType>;
19
- protected httpBody: HttpBody;
20
- abstract getUnderlyingRequest(): Req;
21
- protected constructor(url: string, options?: HttpRequestBaseOptions<UnreadBodyType>);
22
- get body(): UnreadBodyType | string;
23
- readBody(): Promise<HttpBody>;
24
- }
@@ -1,77 +0,0 @@
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.HttpRequestBase = void 0;
40
- var utils_1 = require("../utils");
41
- var HttpRequestBase = /** @class */ (function () {
42
- function HttpRequestBase(url, options) {
43
- this.url = url;
44
- this.method = (options === null || options === void 0 ? void 0 : options.method) || utils_1.HttpMethod.GET;
45
- this.headers = (options === null || options === void 0 ? void 0 : options.headers) instanceof utils_1.HttpHeaders ? options === null || options === void 0 ? void 0 : options.headers : new utils_1.HttpHeaders(options === null || options === void 0 ? void 0 : options.headers);
46
- this.bodyReader = options === null || options === void 0 ? void 0 : options.bodyReader;
47
- }
48
- Object.defineProperty(HttpRequestBase.prototype, "body", {
49
- get: function () {
50
- var _a;
51
- return (_a = this.bodyReader) === null || _a === void 0 ? void 0 : _a.body;
52
- },
53
- enumerable: false,
54
- configurable: true
55
- });
56
- HttpRequestBase.prototype.readBody = function () {
57
- var _a, _b;
58
- return __awaiter(this, void 0, void 0, function () {
59
- var _c;
60
- return __generator(this, function (_d) {
61
- switch (_d.label) {
62
- case 0:
63
- if (typeof this.httpBody !== 'undefined') {
64
- return [2 /*return*/, this.httpBody];
65
- }
66
- _c = this;
67
- return [4 /*yield*/, ((_a = this.bodyReader) === null || _a === void 0 ? void 0 : _a.readBody((_b = this.headers.get(utils_1.CONTENT_TYPE_HEADER_NAME)) === null || _b === void 0 ? void 0 : _b[0]))];
68
- case 1:
69
- _c.httpBody = _d.sent();
70
- return [2 /*return*/, this.httpBody];
71
- }
72
- });
73
- });
74
- };
75
- return HttpRequestBase;
76
- }());
77
- exports.HttpRequestBase = HttpRequestBase;
@@ -1,31 +0,0 @@
1
- import { HttpMethod, HttpHeaders, HttpBody } from '../utils';
2
- export interface IHttpRequest<UnreadBodyType = unknown, Req = unknown> {
3
- /**
4
- * The full URL of the request, including scheme, path, and query parameters
5
- * e.g., https://www.example.com/some/path?param1=value1&param2=value2
6
- */
7
- url: string;
8
- /**
9
- * The request method in all caps
10
- * e.g., GET, POST
11
- */
12
- method: HttpMethod;
13
- /**
14
- * An object representing the HTTP headers. For more information, see the documentation for the HttpHeaders class.
15
- */
16
- headers: HttpHeaders;
17
- /**
18
- * A representation of the request body. If the request does not have a body, it should be undefined.
19
- * This is called when the request body needs to be accessed or used to reconstruct a new request, but not read.
20
- */
21
- readonly body: UnreadBodyType | string;
22
- /**
23
- * The request body if it needs to be used in the enforcer. If read, the call to HttpRequest.body
24
- * should return the saved value so as not to consume streams twice.
25
- */
26
- readBody(): Promise<HttpBody>;
27
- /**
28
- * The underlying, platform-specific implementation of the request object.
29
- */
30
- getUnderlyingRequest(): Req;
31
- }
@@ -1,5 +0,0 @@
1
- import { HttpResponseBase, HttpResponseOptions } from './HttpResponseBase';
2
- export declare class DefaultHttpResponse extends HttpResponseBase<string, DefaultHttpResponse> {
3
- constructor(body?: string, options?: HttpResponseOptions);
4
- getUnderlyingResponse(): DefaultHttpResponse;
5
- }