dce-expresskit 4.0.0-beta.15 → 4.0.0-beta.16
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.
- package/lib/helpers/visitEndpointOnAnotherServer/index.js +11 -78
- package/lib/helpers/visitEndpointOnAnotherServer/index.js.map +1 -1
- package/lib/helpers/visitEndpointOnAnotherServer/sendServerToServerRequest.js +98 -31
- package/lib/helpers/visitEndpointOnAnotherServer/sendServerToServerRequest.js.map +1 -1
- package/package.json +1 -1
- package/src/helpers/visitEndpointOnAnotherServer/index.ts +1 -88
- package/src/helpers/visitEndpointOnAnotherServer/sendServerToServerRequest.ts +99 -6
|
@@ -38,67 +38,12 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
38
38
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
39
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
40
|
};
|
|
41
|
-
var _a;
|
|
42
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
42
|
// Import dce-reactkit
|
|
44
43
|
var dce_reactkit_1 = require("dce-reactkit");
|
|
45
|
-
// Import data signer
|
|
46
|
-
var dataSigner_1 = require("../dataSigner");
|
|
47
44
|
// Import shared types
|
|
48
|
-
var ExpressKitErrorCode_1 = __importDefault(require("../../types/ExpressKitErrorCode"));
|
|
49
45
|
var sendServerToServerRequest_1 = __importDefault(require("./sendServerToServerRequest"));
|
|
50
46
|
/*------------------------------------------------------------------------*/
|
|
51
|
-
/* ----------------------------- Credentials ---------------------------- */
|
|
52
|
-
/*------------------------------------------------------------------------*/
|
|
53
|
-
/*
|
|
54
|
-
DCEKIT_CROSS_SERVER_CREDENTIALS format:
|
|
55
|
-
|host:key:secret||host:key:secret|...
|
|
56
|
-
*/
|
|
57
|
-
var credentials = (((_a = process.env.DCEKIT_CROSS_SERVER_CREDENTIALS) !== null && _a !== void 0 ? _a : '')
|
|
58
|
-
// Replace multiple | with a single one
|
|
59
|
-
.replace(/\|+/g, '|')
|
|
60
|
-
// Split by |
|
|
61
|
-
.split('|')
|
|
62
|
-
// Remove empty strings
|
|
63
|
-
.filter(function (str) {
|
|
64
|
-
return str.trim().length > 0;
|
|
65
|
-
})
|
|
66
|
-
// Process each credential
|
|
67
|
-
.map(function (str) {
|
|
68
|
-
// Split by :
|
|
69
|
-
var parts = str.split(':');
|
|
70
|
-
// Check for errors
|
|
71
|
-
if (parts.length !== 3) {
|
|
72
|
-
throw new dce_reactkit_1.ErrorWithCode('Invalid DCEKIT_CROSS_SERVER_CREDENTIALS format. Each credential must be in the format |host:key:secret|', ExpressKitErrorCode_1.default.InvalidCrossServerCredentialsFormat);
|
|
73
|
-
}
|
|
74
|
-
// Return the credential
|
|
75
|
-
return {
|
|
76
|
-
host: parts[0].trim(),
|
|
77
|
-
key: parts[1].trim(),
|
|
78
|
-
secret: parts[2].trim(),
|
|
79
|
-
};
|
|
80
|
-
}));
|
|
81
|
-
/*------------------------------------------------------------------------*/
|
|
82
|
-
/* ------------------------------- Helpers ------------------------------ */
|
|
83
|
-
/*------------------------------------------------------------------------*/
|
|
84
|
-
/**
|
|
85
|
-
* Get the credential to use for the request to another server
|
|
86
|
-
* @author Gabe Abrams
|
|
87
|
-
* @param host the host of the other server
|
|
88
|
-
* @return the credential to use
|
|
89
|
-
*/
|
|
90
|
-
var getCrossServerCredential = function (host) {
|
|
91
|
-
// Find the credential
|
|
92
|
-
var credential = credentials.find(function (cred) {
|
|
93
|
-
return cred.host.toLowerCase() === host.toLowerCase();
|
|
94
|
-
});
|
|
95
|
-
if (!credential) {
|
|
96
|
-
throw new dce_reactkit_1.ErrorWithCode('Cannot send cross-server signed request there was no credential that matched the host that the request is being sent to.', ExpressKitErrorCode_1.default.CrossServerNoCredentialsToSignWith);
|
|
97
|
-
}
|
|
98
|
-
// Return credential
|
|
99
|
-
return credential;
|
|
100
|
-
};
|
|
101
|
-
/*------------------------------------------------------------------------*/
|
|
102
47
|
/* -------------------------------- Main -------------------------------- */
|
|
103
48
|
/*------------------------------------------------------------------------*/
|
|
104
49
|
/**
|
|
@@ -112,30 +57,18 @@ var getCrossServerCredential = function (host) {
|
|
|
112
57
|
* @param [opts.responseType=JSON] the response type from the other server
|
|
113
58
|
*/
|
|
114
59
|
var visitEndpointOnAnotherServer = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
|
|
115
|
-
var
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
key: credential.key,
|
|
126
|
-
secret: credential.secret,
|
|
127
|
-
})];
|
|
60
|
+
var response, body;
|
|
61
|
+
return __generator(this, function (_a) {
|
|
62
|
+
switch (_a.label) {
|
|
63
|
+
case 0: return [4 /*yield*/, (0, sendServerToServerRequest_1.default)({
|
|
64
|
+
path: opts.path,
|
|
65
|
+
host: opts.host,
|
|
66
|
+
method: opts.method,
|
|
67
|
+
params: opts.params,
|
|
68
|
+
responseType: opts.responseType,
|
|
69
|
+
})];
|
|
128
70
|
case 1:
|
|
129
|
-
|
|
130
|
-
return [4 /*yield*/, (0, sendServerToServerRequest_1.default)({
|
|
131
|
-
path: opts.path,
|
|
132
|
-
host: opts.host,
|
|
133
|
-
method: opts.method,
|
|
134
|
-
params: augmentedParams,
|
|
135
|
-
responseType: opts.responseType,
|
|
136
|
-
})];
|
|
137
|
-
case 2:
|
|
138
|
-
response = _b.sent();
|
|
71
|
+
response = _a.sent();
|
|
139
72
|
// Check for failure
|
|
140
73
|
if (!response || !response.body) {
|
|
141
74
|
throw new dce_reactkit_1.ErrorWithCode('We didn\'t get a response from the other server. Please check the network between the two connection.', dce_reactkit_1.ReactKitErrorCode.NoResponse);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/helpers/visitEndpointOnAnotherServer/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/helpers/visitEndpointOnAnotherServer/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sBAAsB;AACtB,6CAGsB;AAEtB,sBAAsB;AACtB,0FAAoE;AAEpE,4EAA4E;AAC5E,4EAA4E;AAC5E,4EAA4E;AAE5E;;;;;;;;;GASG;AACH,IAAM,4BAA4B,GAAG,UACnC,IAMC;;;;oBAGgB,qBAAM,IAAA,mCAAyB,EAAC;oBAC/C,IAAI,EAAE,IAAI,CAAC,IAAI;oBACf,IAAI,EAAE,IAAI,CAAC,IAAI;oBACf,MAAM,EAAE,IAAI,CAAC,MAAM;oBACnB,MAAM,EAAE,IAAI,CAAC,MAAM;oBACnB,YAAY,EAAE,IAAI,CAAC,YAAY;iBAChC,CAAC,EAAA;;gBANI,QAAQ,GAAG,SAMf;gBAEF,oBAAoB;gBACpB,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;oBAChC,MAAM,IAAI,4BAAa,CACrB,uGAAuG,EACvG,gCAAiB,CAAC,UAAU,CAC7B,CAAC;gBACJ,CAAC;gBACD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;oBAC3B,eAAe;oBACf,MAAM,IAAI,4BAAa,CACrB,CACE,QAAQ,CAAC,IAAI,CAAC,OAAO;2BAClB,qDAAqD,CACzD,EACD,CACE,QAAQ,CAAC,IAAI,CAAC,IAAI;2BACf,gCAAiB,CAAC,MAAM,CAC5B,CACF,CAAC;gBACJ,CAAC;gBAGO,IAAI,GAAK,QAAQ,CAAC,IAAI,KAAlB,CAAmB;gBAE/B,SAAS;gBACT,sBAAO,IAAI,EAAC;;;KACb,CAAC;AAEF,kBAAe,4BAA4B,CAAC"}
|
|
@@ -38,13 +38,70 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
38
38
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
39
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
40
|
};
|
|
41
|
+
var _a;
|
|
41
42
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
43
|
// Import libs
|
|
43
44
|
var qs_1 = __importDefault(require("qs"));
|
|
44
45
|
// Import dce-reactkit
|
|
45
46
|
var dce_reactkit_1 = require("dce-reactkit");
|
|
47
|
+
// Import data signer
|
|
48
|
+
var dataSigner_1 = require("../dataSigner");
|
|
46
49
|
// Import shared types
|
|
47
50
|
var ExpressKitErrorCode_1 = __importDefault(require("../../types/ExpressKitErrorCode"));
|
|
51
|
+
/*------------------------------------------------------------------------*/
|
|
52
|
+
/* ----------------------------- Credentials ---------------------------- */
|
|
53
|
+
/*------------------------------------------------------------------------*/
|
|
54
|
+
/*
|
|
55
|
+
DCEKIT_CROSS_SERVER_CREDENTIALS format:
|
|
56
|
+
|host:key:secret||host:key:secret|...
|
|
57
|
+
*/
|
|
58
|
+
var credentials = (((_a = process.env.DCEKIT_CROSS_SERVER_CREDENTIALS) !== null && _a !== void 0 ? _a : '')
|
|
59
|
+
// Replace multiple | with a single one
|
|
60
|
+
.replace(/\|+/g, '|')
|
|
61
|
+
// Split by |
|
|
62
|
+
.split('|')
|
|
63
|
+
// Remove empty strings
|
|
64
|
+
.filter(function (str) {
|
|
65
|
+
return str.trim().length > 0;
|
|
66
|
+
})
|
|
67
|
+
// Process each credential
|
|
68
|
+
.map(function (str) {
|
|
69
|
+
// Split by :
|
|
70
|
+
var parts = str.split(':');
|
|
71
|
+
// Check for errors
|
|
72
|
+
if (parts.length !== 3) {
|
|
73
|
+
throw new dce_reactkit_1.ErrorWithCode('Invalid DCEKIT_CROSS_SERVER_CREDENTIALS format. Each credential must be in the format |host:key:secret|', ExpressKitErrorCode_1.default.InvalidCrossServerCredentialsFormat);
|
|
74
|
+
}
|
|
75
|
+
// Return the credential
|
|
76
|
+
return {
|
|
77
|
+
host: parts[0].trim(),
|
|
78
|
+
key: parts[1].trim(),
|
|
79
|
+
secret: parts[2].trim(),
|
|
80
|
+
};
|
|
81
|
+
}));
|
|
82
|
+
/*------------------------------------------------------------------------*/
|
|
83
|
+
/* ------------------------------- Helpers ------------------------------ */
|
|
84
|
+
/*------------------------------------------------------------------------*/
|
|
85
|
+
/**
|
|
86
|
+
* Get the credential to use for the request to another server
|
|
87
|
+
* @author Gabe Abrams
|
|
88
|
+
* @param host the host of the other server
|
|
89
|
+
* @return the credential to use
|
|
90
|
+
*/
|
|
91
|
+
var getCrossServerCredential = function (host) {
|
|
92
|
+
// Find the credential
|
|
93
|
+
var credential = credentials.find(function (cred) {
|
|
94
|
+
return cred.host.toLowerCase() === host.toLowerCase();
|
|
95
|
+
});
|
|
96
|
+
if (!credential) {
|
|
97
|
+
throw new dce_reactkit_1.ErrorWithCode('Cannot send cross-server signed request there was no credential that matched the host that the request is being sent to.', ExpressKitErrorCode_1.default.CrossServerNoCredentialsToSignWith);
|
|
98
|
+
}
|
|
99
|
+
// Return credential
|
|
100
|
+
return credential;
|
|
101
|
+
};
|
|
102
|
+
/*------------------------------------------------------------------------*/
|
|
103
|
+
/* -------------------------------- Main -------------------------------- */
|
|
104
|
+
/*------------------------------------------------------------------------*/
|
|
48
105
|
/**
|
|
49
106
|
* Sends and retries an http request
|
|
50
107
|
* @author Gabriel Abrams
|
|
@@ -57,10 +114,10 @@ var ExpressKitErrorCode_1 = __importDefault(require("../../types/ExpressKitError
|
|
|
57
114
|
* @returns { body, status, headers } on success
|
|
58
115
|
*/
|
|
59
116
|
var sendServerToServerRequest = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
|
|
60
|
-
var method, params, stringifiedParams, query, url, headers, data, encodedData, response, responseHeaders_1, responseBody, err_1, err_2;
|
|
61
|
-
var _a;
|
|
62
|
-
return __generator(this, function (
|
|
63
|
-
switch (
|
|
117
|
+
var method, params, credential, augmentedParams, stringifiedParams, query, url, headers, data, encodedData, response, responseHeaders_1, responseBody, err_1, err_2;
|
|
118
|
+
var _a, _b;
|
|
119
|
+
return __generator(this, function (_c) {
|
|
120
|
+
switch (_c.label) {
|
|
64
121
|
case 0:
|
|
65
122
|
method = (opts.method || 'GET');
|
|
66
123
|
if (opts.params) {
|
|
@@ -75,7 +132,17 @@ var sendServerToServerRequest = function (opts) { return __awaiter(void 0, void
|
|
|
75
132
|
}
|
|
76
133
|
});
|
|
77
134
|
}
|
|
78
|
-
|
|
135
|
+
credential = getCrossServerCredential(opts.host);
|
|
136
|
+
return [4 /*yield*/, (0, dataSigner_1.signRequest)({
|
|
137
|
+
method: opts.method,
|
|
138
|
+
path: opts.path,
|
|
139
|
+
params: (_a = opts.params) !== null && _a !== void 0 ? _a : {},
|
|
140
|
+
key: credential.key,
|
|
141
|
+
secret: credential.secret,
|
|
142
|
+
})];
|
|
143
|
+
case 1:
|
|
144
|
+
augmentedParams = _c.sent();
|
|
145
|
+
stringifiedParams = qs_1.default.stringify(augmentedParams || {}, {
|
|
79
146
|
encodeValuesOnly: true,
|
|
80
147
|
arrayFormat: 'brackets',
|
|
81
148
|
});
|
|
@@ -97,19 +164,19 @@ var sendServerToServerRequest = function (opts) { return __awaiter(void 0, void
|
|
|
97
164
|
}
|
|
98
165
|
else {
|
|
99
166
|
// JSON encode
|
|
100
|
-
data =
|
|
167
|
+
data = augmentedParams;
|
|
101
168
|
}
|
|
102
169
|
if (data) {
|
|
103
170
|
if (headers['Content-Type'] === 'application/x-www-form-urlencoded') {
|
|
104
|
-
encodedData = new URLSearchParams(
|
|
171
|
+
encodedData = new URLSearchParams(augmentedParams);
|
|
105
172
|
}
|
|
106
173
|
else {
|
|
107
174
|
encodedData = JSON.stringify(data);
|
|
108
175
|
}
|
|
109
176
|
}
|
|
110
|
-
|
|
111
|
-
case
|
|
112
|
-
|
|
177
|
+
_c.label = 2;
|
|
178
|
+
case 2:
|
|
179
|
+
_c.trys.push([2, 11, , 12]);
|
|
113
180
|
return [4 /*yield*/, fetch(url, {
|
|
114
181
|
method: method,
|
|
115
182
|
mode: 'cors',
|
|
@@ -119,48 +186,48 @@ var sendServerToServerRequest = function (opts) { return __awaiter(void 0, void
|
|
|
119
186
|
: undefined),
|
|
120
187
|
redirect: 'follow',
|
|
121
188
|
})];
|
|
122
|
-
case
|
|
123
|
-
response =
|
|
189
|
+
case 3:
|
|
190
|
+
response = _c.sent();
|
|
124
191
|
responseHeaders_1 = {};
|
|
125
192
|
response.headers.forEach(function (value, key) {
|
|
126
193
|
responseHeaders_1[key] = value;
|
|
127
194
|
});
|
|
128
|
-
|
|
129
|
-
case
|
|
130
|
-
|
|
195
|
+
_c.label = 4;
|
|
196
|
+
case 4:
|
|
197
|
+
_c.trys.push([4, 9, , 10]);
|
|
131
198
|
responseBody = void 0;
|
|
132
199
|
if (!(opts.responseType
|
|
133
|
-
&& opts.responseType === 'Text')) return [3 /*break*/,
|
|
200
|
+
&& opts.responseType === 'Text')) return [3 /*break*/, 6];
|
|
134
201
|
return [4 /*yield*/, response.text()];
|
|
135
|
-
case
|
|
202
|
+
case 5:
|
|
136
203
|
// Response type is text
|
|
137
|
-
responseBody =
|
|
138
|
-
return [3 /*break*/,
|
|
139
|
-
case
|
|
140
|
-
case
|
|
204
|
+
responseBody = _c.sent();
|
|
205
|
+
return [3 /*break*/, 8];
|
|
206
|
+
case 6: return [4 /*yield*/, response.json()];
|
|
207
|
+
case 7:
|
|
141
208
|
// Response type is JSON
|
|
142
|
-
responseBody =
|
|
143
|
-
|
|
144
|
-
case
|
|
209
|
+
responseBody = _c.sent();
|
|
210
|
+
_c.label = 8;
|
|
211
|
+
case 8:
|
|
145
212
|
// Return response
|
|
146
213
|
return [2 /*return*/, {
|
|
147
214
|
body: responseBody,
|
|
148
215
|
status: response.status,
|
|
149
216
|
headers: responseHeaders_1,
|
|
150
217
|
}];
|
|
151
|
-
case
|
|
152
|
-
err_1 =
|
|
218
|
+
case 9:
|
|
219
|
+
err_1 = _c.sent();
|
|
153
220
|
throw new dce_reactkit_1.ErrorWithCode("Failed to parse response as ".concat(opts.responseType, ": ").concat(err_1 === null || err_1 === void 0 ? void 0 : err_1.message), ExpressKitErrorCode_1.default.ResponseParseError);
|
|
154
|
-
case
|
|
155
|
-
case
|
|
156
|
-
err_2 =
|
|
221
|
+
case 10: return [3 /*break*/, 12];
|
|
222
|
+
case 11:
|
|
223
|
+
err_2 = _c.sent();
|
|
157
224
|
// Self-signed certificate error:
|
|
158
|
-
if ((
|
|
225
|
+
if ((_b = err_2 === null || err_2 === void 0 ? void 0 : err_2.message) === null || _b === void 0 ? void 0 : _b.includes('self signed certificate')) {
|
|
159
226
|
throw new dce_reactkit_1.ErrorWithCode('We refused to send a request because the receiver has self-signed certificates.', ExpressKitErrorCode_1.default.SelfSigned);
|
|
160
227
|
}
|
|
161
228
|
// No tries left
|
|
162
229
|
throw new dce_reactkit_1.ErrorWithCode("We encountered an error when trying to send a network request. If this issue persists, contact an admin. Error: ".concat(err_2 === null || err_2 === void 0 ? void 0 : err_2.message), ExpressKitErrorCode_1.default.NotConnected);
|
|
163
|
-
case
|
|
230
|
+
case 12: return [2 /*return*/];
|
|
164
231
|
}
|
|
165
232
|
});
|
|
166
233
|
}); };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sendServerToServerRequest.js","sourceRoot":"","sources":["../../../src/helpers/visitEndpointOnAnotherServer/sendServerToServerRequest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sendServerToServerRequest.js","sourceRoot":"","sources":["../../../src/helpers/visitEndpointOnAnotherServer/sendServerToServerRequest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,cAAc;AACd,0CAAoB;AAEpB,sBAAsB;AACtB,6CAEsB;AAEtB,qBAAqB;AACrB,4CAA4C;AAE5C,sBAAsB;AACtB,wFAAkE;AAElE,4EAA4E;AAC5E,4EAA4E;AAC5E,4EAA4E;AAE5E;;;EAGE;AAEF,IAAM,WAAW,GAIX,CACF,CAAC,MAAA,OAAO,CAAC,GAA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CAAC,YAAY,CACjC,CAAC;;;;KAEL,CAAC;AAEF,kBAAe,yBAAyB,CAAC"}
|
package/package.json
CHANGED
|
@@ -4,84 +4,9 @@ import {
|
|
|
4
4
|
ReactKitErrorCode,
|
|
5
5
|
} from 'dce-reactkit';
|
|
6
6
|
|
|
7
|
-
// Import data signer
|
|
8
|
-
import { signRequest } from '../dataSigner';
|
|
9
|
-
|
|
10
7
|
// Import shared types
|
|
11
|
-
import ExpressKitErrorCode from '../../types/ExpressKitErrorCode';
|
|
12
8
|
import sendServerToServerRequest from './sendServerToServerRequest';
|
|
13
9
|
|
|
14
|
-
/*------------------------------------------------------------------------*/
|
|
15
|
-
/* ----------------------------- Credentials ---------------------------- */
|
|
16
|
-
/*------------------------------------------------------------------------*/
|
|
17
|
-
|
|
18
|
-
/*
|
|
19
|
-
DCEKIT_CROSS_SERVER_CREDENTIALS format:
|
|
20
|
-
|host:key:secret||host:key:secret|...
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
const credentials: {
|
|
24
|
-
host: string,
|
|
25
|
-
key: string,
|
|
26
|
-
secret: string,
|
|
27
|
-
}[] = (
|
|
28
|
-
(process.env.DCEKIT_CROSS_SERVER_CREDENTIALS ?? '')
|
|
29
|
-
// Replace multiple | with a single one
|
|
30
|
-
.replace(/\|+/g, '|')
|
|
31
|
-
// Split by |
|
|
32
|
-
.split('|')
|
|
33
|
-
// Remove empty strings
|
|
34
|
-
.filter((str) => {
|
|
35
|
-
return str.trim().length > 0;
|
|
36
|
-
})
|
|
37
|
-
// Process each credential
|
|
38
|
-
.map((str) => {
|
|
39
|
-
// Split by :
|
|
40
|
-
const parts = str.split(':');
|
|
41
|
-
|
|
42
|
-
// Check for errors
|
|
43
|
-
if (parts.length !== 3) {
|
|
44
|
-
throw new ErrorWithCode(
|
|
45
|
-
'Invalid DCEKIT_CROSS_SERVER_CREDENTIALS format. Each credential must be in the format |host:key:secret|',
|
|
46
|
-
ExpressKitErrorCode.InvalidCrossServerCredentialsFormat,
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Return the credential
|
|
51
|
-
return {
|
|
52
|
-
host: parts[0].trim(),
|
|
53
|
-
key: parts[1].trim(),
|
|
54
|
-
secret: parts[2].trim(),
|
|
55
|
-
};
|
|
56
|
-
})
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
/*------------------------------------------------------------------------*/
|
|
60
|
-
/* ------------------------------- Helpers ------------------------------ */
|
|
61
|
-
/*------------------------------------------------------------------------*/
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Get the credential to use for the request to another server
|
|
65
|
-
* @author Gabe Abrams
|
|
66
|
-
* @param host the host of the other server
|
|
67
|
-
* @return the credential to use
|
|
68
|
-
*/
|
|
69
|
-
const getCrossServerCredential = (host: string) => {
|
|
70
|
-
// Find the credential
|
|
71
|
-
const credential = credentials.find((cred) => {
|
|
72
|
-
return cred.host.toLowerCase() === host.toLowerCase();
|
|
73
|
-
});
|
|
74
|
-
if (!credential) {
|
|
75
|
-
throw new ErrorWithCode(
|
|
76
|
-
'Cannot send cross-server signed request there was no credential that matched the host that the request is being sent to.',
|
|
77
|
-
ExpressKitErrorCode.CrossServerNoCredentialsToSignWith,
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Return credential
|
|
82
|
-
return credential;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
10
|
/*------------------------------------------------------------------------*/
|
|
86
11
|
/* -------------------------------- Main -------------------------------- */
|
|
87
12
|
/*------------------------------------------------------------------------*/
|
|
@@ -105,24 +30,12 @@ const visitEndpointOnAnotherServer = async (
|
|
|
105
30
|
responseType?: 'JSON' | 'Text',
|
|
106
31
|
},
|
|
107
32
|
): Promise<any> => {
|
|
108
|
-
// Get cross-server credential
|
|
109
|
-
const credential = getCrossServerCredential(opts.host);
|
|
110
|
-
|
|
111
|
-
// Sign the request, get new params
|
|
112
|
-
const augmentedParams = await signRequest({
|
|
113
|
-
method: opts.method,
|
|
114
|
-
path: opts.path,
|
|
115
|
-
params: opts.params ?? {},
|
|
116
|
-
key: credential.key,
|
|
117
|
-
secret: credential.secret,
|
|
118
|
-
});
|
|
119
|
-
|
|
120
33
|
// Send the request
|
|
121
34
|
const response = await sendServerToServerRequest({
|
|
122
35
|
path: opts.path,
|
|
123
36
|
host: opts.host,
|
|
124
37
|
method: opts.method,
|
|
125
|
-
params:
|
|
38
|
+
params: opts.params,
|
|
126
39
|
responseType: opts.responseType,
|
|
127
40
|
});
|
|
128
41
|
|
|
@@ -6,9 +6,87 @@ import {
|
|
|
6
6
|
ErrorWithCode,
|
|
7
7
|
} from 'dce-reactkit';
|
|
8
8
|
|
|
9
|
+
// Import data signer
|
|
10
|
+
import { signRequest } from '../dataSigner';
|
|
11
|
+
|
|
9
12
|
// Import shared types
|
|
10
13
|
import ExpressKitErrorCode from '../../types/ExpressKitErrorCode';
|
|
11
14
|
|
|
15
|
+
/*------------------------------------------------------------------------*/
|
|
16
|
+
/* ----------------------------- Credentials ---------------------------- */
|
|
17
|
+
/*------------------------------------------------------------------------*/
|
|
18
|
+
|
|
19
|
+
/*
|
|
20
|
+
DCEKIT_CROSS_SERVER_CREDENTIALS format:
|
|
21
|
+
|host:key:secret||host:key:secret|...
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const credentials: {
|
|
25
|
+
host: string,
|
|
26
|
+
key: string,
|
|
27
|
+
secret: string,
|
|
28
|
+
}[] = (
|
|
29
|
+
(process.env.DCEKIT_CROSS_SERVER_CREDENTIALS ?? '')
|
|
30
|
+
// Replace multiple | with a single one
|
|
31
|
+
.replace(/\|+/g, '|')
|
|
32
|
+
// Split by |
|
|
33
|
+
.split('|')
|
|
34
|
+
// Remove empty strings
|
|
35
|
+
.filter((str) => {
|
|
36
|
+
return str.trim().length > 0;
|
|
37
|
+
})
|
|
38
|
+
// Process each credential
|
|
39
|
+
.map((str) => {
|
|
40
|
+
// Split by :
|
|
41
|
+
const parts = str.split(':');
|
|
42
|
+
|
|
43
|
+
// Check for errors
|
|
44
|
+
if (parts.length !== 3) {
|
|
45
|
+
throw new ErrorWithCode(
|
|
46
|
+
'Invalid DCEKIT_CROSS_SERVER_CREDENTIALS format. Each credential must be in the format |host:key:secret|',
|
|
47
|
+
ExpressKitErrorCode.InvalidCrossServerCredentialsFormat,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Return the credential
|
|
52
|
+
return {
|
|
53
|
+
host: parts[0].trim(),
|
|
54
|
+
key: parts[1].trim(),
|
|
55
|
+
secret: parts[2].trim(),
|
|
56
|
+
};
|
|
57
|
+
})
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
/*------------------------------------------------------------------------*/
|
|
61
|
+
/* ------------------------------- Helpers ------------------------------ */
|
|
62
|
+
/*------------------------------------------------------------------------*/
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Get the credential to use for the request to another server
|
|
66
|
+
* @author Gabe Abrams
|
|
67
|
+
* @param host the host of the other server
|
|
68
|
+
* @return the credential to use
|
|
69
|
+
*/
|
|
70
|
+
const getCrossServerCredential = (host: string) => {
|
|
71
|
+
// Find the credential
|
|
72
|
+
const credential = credentials.find((cred) => {
|
|
73
|
+
return cred.host.toLowerCase() === host.toLowerCase();
|
|
74
|
+
});
|
|
75
|
+
if (!credential) {
|
|
76
|
+
throw new ErrorWithCode(
|
|
77
|
+
'Cannot send cross-server signed request there was no credential that matched the host that the request is being sent to.',
|
|
78
|
+
ExpressKitErrorCode.CrossServerNoCredentialsToSignWith,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Return credential
|
|
83
|
+
return credential;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/*------------------------------------------------------------------------*/
|
|
87
|
+
/* -------------------------------- Main -------------------------------- */
|
|
88
|
+
/*------------------------------------------------------------------------*/
|
|
89
|
+
|
|
12
90
|
/**
|
|
13
91
|
* Sends and retries an http request
|
|
14
92
|
* @author Gabriel Abrams
|
|
@@ -51,12 +129,27 @@ const sendServerToServerRequest = async (
|
|
|
51
129
|
});
|
|
52
130
|
}
|
|
53
131
|
|
|
54
|
-
//
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
132
|
+
// Get cross-server credential
|
|
133
|
+
const credential = getCrossServerCredential(opts.host);
|
|
134
|
+
|
|
135
|
+
// Sign the request, get new params
|
|
136
|
+
const augmentedParams = await signRequest({
|
|
137
|
+
method: opts.method,
|
|
138
|
+
path: opts.path,
|
|
139
|
+
params: opts.params ?? {},
|
|
140
|
+
key: credential.key,
|
|
141
|
+
secret: credential.secret,
|
|
58
142
|
});
|
|
59
143
|
|
|
144
|
+
// Stringify parameters
|
|
145
|
+
const stringifiedParams = qs.stringify(
|
|
146
|
+
augmentedParams || {},
|
|
147
|
+
{
|
|
148
|
+
encodeValuesOnly: true,
|
|
149
|
+
arrayFormat: 'brackets',
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
|
|
60
153
|
// Create url (include query if GET)
|
|
61
154
|
const query = (method === 'GET' ? `?${stringifiedParams}` : '');
|
|
62
155
|
let url;
|
|
@@ -79,14 +172,14 @@ const sendServerToServerRequest = async (
|
|
|
79
172
|
data = (method !== 'GET' ? stringifiedParams : null);
|
|
80
173
|
} else {
|
|
81
174
|
// JSON encode
|
|
82
|
-
data =
|
|
175
|
+
data = augmentedParams;
|
|
83
176
|
}
|
|
84
177
|
|
|
85
178
|
// Encode data
|
|
86
179
|
let encodedData: URLSearchParams | string | undefined;
|
|
87
180
|
if (data) {
|
|
88
181
|
if (headers['Content-Type'] === 'application/x-www-form-urlencoded') {
|
|
89
|
-
encodedData = new URLSearchParams(
|
|
182
|
+
encodedData = new URLSearchParams(augmentedParams);
|
|
90
183
|
} else {
|
|
91
184
|
encodedData = JSON.stringify(data);
|
|
92
185
|
}
|