dce-reactkit 3.12.1-beta-cross-server.3 → 3.12.1-beta-cross-server.4

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,5 +1,5 @@
1
- // Import crypto
2
- import crypto from 'crypto';
1
+ // Import crypto lib
2
+ import Cryptr from 'cryptr';
3
3
 
4
4
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
5
5
 
@@ -42,34 +42,9 @@ if (!host) {
42
42
  process.exit(1);
43
43
  }
44
44
 
45
- // Encryption process based on:
46
- // https://medium.com/@tony.infisical/guide-to-nodes-crypto-module-for-encryption-decryption-65c077176980
47
-
48
- // Create a random initialization vector
49
- const iv = crypto.randomBytes(12).toString('base64');
50
-
51
- // Create a cipher
52
- const cipher = crypto.createCipheriv(
53
- 'aes-256-gcm',
54
- Buffer.from(secret, 'base64'),
55
- Buffer.from(iv, 'base64'),
56
- );
57
-
58
- // Encrypt the string
59
- let ciphertext = cipher.update(secret, 'utf8', 'base64');
60
-
61
- // Finalize the encryption
62
- ciphertext += cipher.final('base64');
63
-
64
- // Get the authentication tag
65
- const tag = cipher.getAuthTag();
66
-
67
- // JSONify the encrypted data
68
- const encryptionPack = encodeURIComponent(JSON.stringify({
69
- ciphertext,
70
- iv,
71
- tag,
72
- }));
45
+ // Encrypt the secret
46
+ const cryptr = new Cryptr(REACTKIT_CRED_ENCODING_SALT);
47
+ const encodedSecret = cryptr.encrypt(secret);
73
48
 
74
49
  // Show the encrypted data
75
50
  console.log('\n\n');
@@ -79,6 +54,6 @@ console.log('On the server *sending* the requests, append the following to the R
79
54
  console.log(`|${host}:${key}:${secret}|`);
80
55
  console.log('');
81
56
  console.log('On the server *receiving* the requests, add an entry to the "CrossServerCredential" collection:');
82
- console.log(`{ "description": "${description}", "key": "${key}", "encodedeSecret": "${encryptionPack}", "scopes": [] }`);
57
+ console.log(`{ "description": "${description}", "key": "${key}", "encodedeSecret": "${encodedSecret}", "scopes": [] }`);
83
58
  console.log('');
84
59
  console.log('For all scopes that the server should have access to, add them to the "scopes" array.');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dce-reactkit",
3
- "version": "3.12.1-beta-cross-server.3",
3
+ "version": "3.12.1-beta-cross-server.4",
4
4
  "description": "Shared components for Harvard DCE apps",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "homepage": "https://github.com/harvard-edtech/dce-reactkit#readme",
27
27
  "dependencies": {
28
+ "cryptr": "^6.3.0",
28
29
  "qs": "^6.x.x",
29
30
  "react-select": "^5.7.3"
30
31
  },
@@ -1,3 +1,6 @@
1
+ // Import cryptography lib
2
+ import Cryptr from 'cryptr';
3
+
1
4
  // Import shared helpers
2
5
  // eslint-disable-next-line import/no-cycle
3
6
  import { internalGetCrossServerCredentialCollection } from '../server/initServer';
@@ -35,26 +38,6 @@ const getOauthLib = async () => {
35
38
  }
36
39
  };
37
40
 
38
- /**
39
- * Get a copy of the crypto lib
40
- * @author Gabe Abrams
41
- * @return the crypto lib
42
- */
43
- const getCryptoLib = async () => {
44
- // Get the crypto library (included on the server)
45
- // Ignore because this is assumed to be included with typescript
46
- // @ts-ignore
47
- try {
48
- const crypto = await import('crypto');
49
- return crypto;
50
- } catch (err) {
51
- throw new ErrorWithCode(
52
- 'Could not sign a cross-server request because we could not load the crypto library. Please make sure this operation is running on the server.',
53
- ReactKitErrorCode.NoCryptoLib,
54
- );
55
- }
56
- };
57
-
58
41
  /*------------------------------------------------------------------------*/
59
42
  /* ------------------------------- Helpers ------------------------------ */
60
43
  /*------------------------------------------------------------------------*/
@@ -122,9 +105,6 @@ const genSignature = async (
122
105
  const decrypt = async (
123
106
  encryptedPack: string,
124
107
  ): Promise<string> => {
125
- // Decryption process based on:
126
- // https://medium.com/@tony.infisical/guide-to-nodes-crypto-module-for-encryption-decryption-65c077176980
127
-
128
108
  // Get the encryption secret
129
109
  const { REACTKIT_CRED_ENCODING_SALT } = process.env;
130
110
  if (!REACTKIT_CRED_ENCODING_SALT) {
@@ -134,29 +114,9 @@ const decrypt = async (
134
114
  );
135
115
  }
136
116
 
137
- // Get the crypto library
138
- const crypto = await getCryptoLib();
139
-
140
- // Separate encrypted pack
141
- const {
142
- ciphertext,
143
- iv,
144
- tag,
145
- } = JSON.parse(decodeURIComponent(encryptedPack));
146
-
147
- // Parse the encrypted data
148
- const decipher = crypto.createDecipheriv(
149
- 'aes-256-gcm',
150
- Buffer.from(REACTKIT_CRED_ENCODING_SALT, 'base64'),
151
- Buffer.from(iv, 'base64'),
152
- );
153
-
154
- // Set the authentication tag
155
- decipher.setAuthTag(Buffer.from(tag, 'base64'));
156
-
157
117
  // Decrypt the string
158
- let str = decipher.update(ciphertext, 'base64', 'utf8');
159
- str += decipher.final('utf8');
118
+ const cryptr = new Cryptr(REACTKIT_CRED_ENCODING_SALT);
119
+ const str = cryptr.decrypt(encryptedPack);
160
120
 
161
121
  // Return the decrypted string
162
122
  return str;