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.
@@ -1,11 +1,86 @@
1
1
  // Import data signer
2
- import { createSignedPack } from '../dataSigner';
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 credentials
35
- const key = opts.key ?? process.env.REACTKIT_CROSS_SERVER_KEY;
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
- // Create signed pack
47
- const signedPack = createSignedPack({
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 your internet connection.',
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 = DRK30
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
- PackUnparseable = 'DRK28',
29
- PackInvalidMethod = 'DRK19',
30
- PackInvalidPath = 'DRK20',
31
- PackInvalidCollection = 'DRK21',
32
- PackInvalidCredential = 'DRK23',
33
- PackInvalidScope = 'DRK22',
34
- PackInvalidTimestamp = 'DRK24',
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
- CrossServerNoPack = 'DRK29',
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;