dce-reactkit 3.12.1-beta-cross-server.2 → 3.12.1-beta-cross-server.3
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/dist/cjs/index.js +8028 -190
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/helpers/dataSigner.d.ts +13 -11
- package/dist/cjs/types/helpers/visitEndpointOnAnotherServer/index.d.ts +0 -6
- package/dist/cjs/types/types/ReactKitErrorCode.d.ts +13 -11
- package/dist/esm/index.js +8027 -189
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/helpers/dataSigner.d.ts +13 -11
- package/dist/esm/types/helpers/visitEndpointOnAnotherServer/index.d.ts +0 -6
- package/dist/esm/types/types/ReactKitErrorCode.d.ts +13 -11
- package/dist/index.d.ts +13 -17
- package/genEncodedSecret.ts +67 -11
- package/package.json +3 -2
- package/rollup.config.js +2 -0
- package/src/helpers/dataSigner.ts +221 -113
- package/src/helpers/genRouteHandler.ts +27 -24
- package/src/helpers/visitEndpointOnAnotherServer/index.ts +84 -26
- package/src/types/ReactKitErrorCode.tsx +13 -11
|
@@ -1,11 +1,86 @@
|
|
|
1
1
|
// Import data signer
|
|
2
|
-
import {
|
|
2
|
+
import { signRequest } from '../dataSigner';
|
|
3
3
|
|
|
4
4
|
// Import shared types
|
|
5
5
|
import ErrorWithCode from '../../errors/ErrorWithCode';
|
|
6
6
|
import ReactKitErrorCode from '../../types/ReactKitErrorCode';
|
|
7
7
|
import sendServerToServerRequest from './sendServerToServerRequest';
|
|
8
8
|
|
|
9
|
+
/*------------------------------------------------------------------------*/
|
|
10
|
+
/* ----------------------------- Credentials ---------------------------- */
|
|
11
|
+
/*------------------------------------------------------------------------*/
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
REACTKIT_CROSS_SERVER_CREDENTIALS format:
|
|
15
|
+
|host:key:secret||host:key:secret|...
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const credentials: {
|
|
19
|
+
host: string,
|
|
20
|
+
key: string,
|
|
21
|
+
secret: string,
|
|
22
|
+
}[] = (
|
|
23
|
+
(process.env.REACTKIT_CROSS_SERVER_CREDENTIALS ?? '')
|
|
24
|
+
// Replace multiple | with a single one
|
|
25
|
+
.replace(/\|+/g, '|')
|
|
26
|
+
// Split by |
|
|
27
|
+
.split('|')
|
|
28
|
+
// Remove empty strings
|
|
29
|
+
.filter((str) => {
|
|
30
|
+
return str.trim().length > 0;
|
|
31
|
+
})
|
|
32
|
+
// Process each credential
|
|
33
|
+
.map((str) => {
|
|
34
|
+
// Split by :
|
|
35
|
+
const parts = str.split(':');
|
|
36
|
+
|
|
37
|
+
// Check for errors
|
|
38
|
+
if (parts.length !== 3) {
|
|
39
|
+
throw new ErrorWithCode(
|
|
40
|
+
'Invalid REACTKIT_CROSS_SERVER_CREDENTIALS format. Each credential must be in the format |host:key:secret|',
|
|
41
|
+
ReactKitErrorCode.InvalidCrossServerCredentialsFormat,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Return the credential
|
|
46
|
+
return {
|
|
47
|
+
host: parts[0].trim(),
|
|
48
|
+
key: parts[1].trim(),
|
|
49
|
+
secret: parts[2].trim(),
|
|
50
|
+
};
|
|
51
|
+
})
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
/*------------------------------------------------------------------------*/
|
|
55
|
+
/* ------------------------------- Helpers ------------------------------ */
|
|
56
|
+
/*------------------------------------------------------------------------*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Get the credential to use for the request to another server
|
|
60
|
+
* @author Gabe Abrams
|
|
61
|
+
* @param host the host of the other server
|
|
62
|
+
* @return the credential to use
|
|
63
|
+
*/
|
|
64
|
+
const getCrossServerCredential = (host: string) => {
|
|
65
|
+
// Find the credential
|
|
66
|
+
const credential = credentials.find((cred) => {
|
|
67
|
+
return cred.host.toLowerCase() === host.toLowerCase();
|
|
68
|
+
});
|
|
69
|
+
if (!credential) {
|
|
70
|
+
throw new ErrorWithCode(
|
|
71
|
+
'Cannot send cross-server signed request there was no credential that matched the host that the request is being sent to.',
|
|
72
|
+
ReactKitErrorCode.CrossServerNoCredentialsToSignWith,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Return credential
|
|
77
|
+
return credential;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/*------------------------------------------------------------------------*/
|
|
81
|
+
/* -------------------------------- Main -------------------------------- */
|
|
82
|
+
/*------------------------------------------------------------------------*/
|
|
83
|
+
|
|
9
84
|
/**
|
|
10
85
|
* Visit an endpoint on another server
|
|
11
86
|
* @author Gabe Abrams
|
|
@@ -13,10 +88,6 @@ import sendServerToServerRequest from './sendServerToServerRequest';
|
|
|
13
88
|
* @param opts.method the method of the endpoint
|
|
14
89
|
* @param opts.path the path of the other server's endpoint
|
|
15
90
|
* @param opts.host the host of the other server
|
|
16
|
-
* @param [opts.key=process.env.REACTKIT_CROSS_SERVER_CREDENTIAL_KEY] reactkit cross-server
|
|
17
|
-
* credential key
|
|
18
|
-
* @param [opts.secret=process.env.REACTKIT_CROSS_SERVER_CREDENTIAL_SECRET reactkit cross-server
|
|
19
|
-
* credential secret
|
|
20
91
|
* @param [opts.params={}] query/body parameters to include
|
|
21
92
|
* @param [opts.responseType=JSON] the response type from the other server
|
|
22
93
|
*/
|
|
@@ -25,31 +96,20 @@ const visitEndpointOnAnotherServer = async (
|
|
|
25
96
|
method: 'GET' | 'POST' | 'DELETE' | 'PUT',
|
|
26
97
|
path: string,
|
|
27
98
|
host: string,
|
|
28
|
-
key?: string,
|
|
29
|
-
secret?: string,
|
|
30
99
|
params?: { [key in string]: any },
|
|
31
100
|
responseType?: 'JSON' | 'Text',
|
|
32
101
|
},
|
|
33
102
|
): Promise<any> => {
|
|
34
|
-
// Get cross-server
|
|
35
|
-
const
|
|
36
|
-
const secret = opts.secret ?? process.env.REACTKIT_CROSS_SERVER_SECRET;
|
|
37
|
-
|
|
38
|
-
// Throw error if no credentials
|
|
39
|
-
if (!key || !secret) {
|
|
40
|
-
throw new ErrorWithCode(
|
|
41
|
-
'Cannot send cross-server signed request because either or both the key and secret were not included or found in env.',
|
|
42
|
-
ReactKitErrorCode.CrossServerNoCredentialsToSignWith,
|
|
43
|
-
);
|
|
44
|
-
}
|
|
103
|
+
// Get cross-server credential
|
|
104
|
+
const credential = getCrossServerCredential(opts.host);
|
|
45
105
|
|
|
46
|
-
//
|
|
47
|
-
const
|
|
106
|
+
// Sign the request, get new params
|
|
107
|
+
const augmentedParams = await signRequest({
|
|
48
108
|
method: opts.method,
|
|
49
109
|
path: opts.path,
|
|
50
110
|
params: opts.params ?? {},
|
|
51
|
-
key,
|
|
52
|
-
secret,
|
|
111
|
+
key: credential.key,
|
|
112
|
+
secret: credential.secret,
|
|
53
113
|
});
|
|
54
114
|
|
|
55
115
|
// Send the request
|
|
@@ -57,16 +117,14 @@ const visitEndpointOnAnotherServer = async (
|
|
|
57
117
|
path: opts.path,
|
|
58
118
|
host: opts.host,
|
|
59
119
|
method: opts.method,
|
|
60
|
-
params:
|
|
61
|
-
signedPack,
|
|
62
|
-
},
|
|
120
|
+
params: augmentedParams,
|
|
63
121
|
responseType: opts.responseType,
|
|
64
122
|
});
|
|
65
123
|
|
|
66
124
|
// Check for failure
|
|
67
125
|
if (!response || !response.body) {
|
|
68
126
|
throw new ErrorWithCode(
|
|
69
|
-
'We didn\'t get a response from the other server. Please check
|
|
127
|
+
'We didn\'t get a response from the other server. Please check the network between the two connection.',
|
|
70
128
|
ReactKitErrorCode.NoResponse,
|
|
71
129
|
);
|
|
72
130
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Highest error code =
|
|
1
|
+
// Highest error code = DRK34
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* List of error codes built into the react kit
|
|
@@ -25,18 +25,20 @@ enum ReactKitErrorCode {
|
|
|
25
25
|
NotConnected = 'DRK14',
|
|
26
26
|
SelfSigned = 'DRK15',
|
|
27
27
|
ResponseParseError = 'DRK16',
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
PackInvalidSignature = 'DRK25',
|
|
36
|
-
PackInvalidBody = 'DRK26',
|
|
28
|
+
SignedRequestUnparseable = 'DRK28',
|
|
29
|
+
SignedRequestInvalidCollection = 'DRK21',
|
|
30
|
+
SignedRequestInvalidCredential = 'DRK23',
|
|
31
|
+
SignedRequestInvalidScope = 'DRK22',
|
|
32
|
+
SignedRequestInvalidTimestamp = 'DRK24',
|
|
33
|
+
SignedRequestInvalidSignature = 'DRK25',
|
|
34
|
+
SignedRequestInvalidBody = 'DRK26',
|
|
37
35
|
CrossServerNoCredentialsToSignWith = 'DRK27',
|
|
38
|
-
|
|
36
|
+
CrossServerMissingSignedRequestInfo = 'DRK29',
|
|
39
37
|
CrossServerNoCredentialEncodingSalt = 'DRK30',
|
|
38
|
+
NoOauthLib = 'DRK31',
|
|
39
|
+
NoCryptoLib = 'DRK32',
|
|
40
|
+
InvalidCrossServerCredentialsFormat = 'DRK33',
|
|
41
|
+
UnknownCrossServerError = 'DRK34',
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
export default ReactKitErrorCode;
|