Skip to content

Commit 49eeea9

Browse files
feat: add async versions of the encrypt and decrypt functions
1 parent 7cb80eb commit 49eeea9

File tree

7 files changed

+1945
-1224
lines changed

7 files changed

+1945
-1224
lines changed

packages/node-embed-sdk/README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,26 @@ Sigma expects you to encrypt OAuth tokens using the same embed secret used to si
3636

3737
#### Example Usage
3838

39+
##### Asynchronous
40+
41+
```typescript
42+
import { encrypt, decrypt } from "@sigmacomputing/node-embed-sdk/promises";
43+
44+
// Encrypt an OAuth token
45+
const encryptedToken = await encrypt("your-embed-secret", "your-oauth-token");
46+
47+
// Decrypt an encrypted token
48+
const decryptedToken = await decrypt("your-embed-secret", encryptedToken);
49+
```
50+
51+
##### Synchronous
52+
3953
```typescript
40-
import { encrypt, decrypt } from '@sigmacomputing/node-embed-sdk';
54+
import { encrypt, decrypt } from "@sigmacomputing/node-embed-sdk";
4155

4256
// Encrypt an OAuth token
43-
const encryptedToken = encrypt(
44-
'your-embed-secret',
45-
'your-oauth-token'
46-
);
57+
const encryptedToken = encrypt("your-embed-secret", "your-oauth-token");
4758

4859
// Decrypt an encrypted token
49-
const decryptedToken = decrypt(
50-
'your-embed-secret',
51-
encryptedToken
52-
);
60+
const decryptedToken = decrypt("your-embed-secret", encryptedToken);
5361
```

packages/node-embed-sdk/package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
"module": "./dist/index.mjs",
3232
"types": "./dist/index.d.ts",
3333
"exports": {
34-
"import": {
34+
".": {
35+
"types": "./dist/index.d.ts",
3536
"import": "./dist/index.mjs",
36-
"types": "./dist/index.d.mts"
37+
"require": "./dist/index.js"
3738
},
38-
"require": {
39-
"require": "./dist/index.js",
40-
"types": "./dist/index.d.ts"
39+
"./promises": {
40+
"types": "./dist/promises.d.ts",
41+
"import": "./dist/promises.mjs",
42+
"require": "./dist/promises.js"
4143
}
4244
},
4345
"files": [
@@ -48,9 +50,9 @@
4850
"@sigmacomputing/typescript-config": "workspace:*",
4951
"@swc/core": "^1.11.29",
5052
"@swc/jest": "^0.2.38",
51-
"@types/jest": "^29.5.14",
53+
"@types/jest": "^30.0.0",
5254
"@types/node": "^20.17.16",
53-
"jest": "^29.7.0"
55+
"jest": "^30.2.0"
5456
},
5557
"engines": {
5658
"node": ">=18"
Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { decrypt, encrypt } from "../index";
2-
import { _testExports } from "../encryption";
1+
import { decrypt, encrypt } from "../encryption";
32

43
const EMBED_SECRET = "my fake embed secret";
54

@@ -12,55 +11,4 @@ describe("oauth token encryption", () => {
1211
const decryptedToken = decrypt(EMBED_SECRET, encryptedToken);
1312
expect(decryptedToken).toBe(plaintext);
1413
});
15-
16-
function toEncodedString(
17-
salt: string,
18-
iv: string,
19-
tag: string,
20-
ciphertext: string,
21-
): string {
22-
return `${salt}.${iv}.${tag}.${ciphertext}`;
23-
}
24-
25-
it("only throws error when reading an incorrectly encoded string", () => {
26-
// Throws for invalid format.
27-
expect(() => {
28-
_testExports.asEncodedPassphraseEncryptionOutput("hello, world!");
29-
}).toThrow();
30-
31-
// Throws for valid format, but with non-base64 components.
32-
expect(() => {
33-
_testExports.asEncodedPassphraseEncryptionOutput(
34-
toEncodedString("(salt)", "(iv)", "(tag)", "(ciphertext)"),
35-
);
36-
}).toThrow();
37-
38-
// Throws for valid format with base64 components of invalid length.
39-
expect(() => {
40-
_testExports.asEncodedPassphraseEncryptionOutput(
41-
toEncodedString("YQ==", "Yg==", "Yw==", "ZA=="),
42-
);
43-
}).toThrow();
44-
45-
// Does not throw for valid format with base64 components of valid length.
46-
const salt = Buffer.from(
47-
"s".repeat(
48-
_testExports.PBKDF2_HMAC_SHA256_KEY_DERIVATION.SALT_LENGTH_BYTES,
49-
),
50-
).toString("base64");
51-
const iv = Buffer.from(
52-
"i".repeat(_testExports.AES_256_GCM_ENCRYPTION.IV_LENGTH_BYTES),
53-
).toString("base64");
54-
const tag = Buffer.from(
55-
"t".repeat(_testExports.AES_256_GCM_ENCRYPTION.TAG_LENGTH_BYTES),
56-
).toString("base64");
57-
const ciphertext = Buffer.from(
58-
"c".repeat(10 /* arbitrary length */),
59-
).toString("base64");
60-
expect(() => {
61-
_testExports.asEncodedPassphraseEncryptionOutput(
62-
toEncodedString(salt, iv, tag, ciphertext),
63-
);
64-
}).not.toThrow();
65-
});
6614
});

0 commit comments

Comments
 (0)