@cubist-labs/cubesigner-sdk 0.1.77 → 0.2.15

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.
Files changed (55) hide show
  1. package/dist/package.json +68 -0
  2. package/dist/src/api.d.ts +493 -0
  3. package/dist/src/api.js +1166 -0
  4. package/dist/src/client.d.ts +534 -10
  5. package/dist/src/client.js +355 -19
  6. package/dist/src/ethers/index.d.ts +34 -9
  7. package/dist/src/ethers/index.js +63 -19
  8. package/dist/src/index.d.ts +51 -70
  9. package/dist/src/index.js +83 -237
  10. package/dist/src/key.d.ts +35 -64
  11. package/dist/src/key.js +32 -96
  12. package/dist/src/mfa.d.ts +85 -14
  13. package/dist/src/mfa.js +146 -40
  14. package/dist/src/org.d.ts +42 -194
  15. package/dist/src/org.js +52 -336
  16. package/dist/src/paginator.js +1 -1
  17. package/dist/src/response.d.ts +101 -0
  18. package/dist/src/response.js +164 -0
  19. package/dist/src/role.d.ts +87 -83
  20. package/dist/src/role.js +79 -136
  21. package/dist/src/schema.d.ts +936 -28
  22. package/dist/src/schema.js +1 -1
  23. package/dist/src/schema_types.d.ts +109 -0
  24. package/dist/src/schema_types.js +3 -0
  25. package/dist/src/session/cognito_manager.d.ts +15 -3
  26. package/dist/src/session/cognito_manager.js +23 -5
  27. package/dist/src/session/session_manager.d.ts +1 -1
  28. package/dist/src/session/session_manager.js +3 -11
  29. package/dist/src/session/session_storage.js +1 -1
  30. package/dist/src/session/signer_session_manager.d.ts +10 -29
  31. package/dist/src/session/signer_session_manager.js +21 -80
  32. package/dist/src/signer_session.d.ts +15 -252
  33. package/dist/src/signer_session.js +25 -424
  34. package/dist/src/user_export.d.ts +52 -0
  35. package/dist/src/user_export.js +129 -0
  36. package/dist/src/util.d.ts +15 -0
  37. package/dist/src/util.js +33 -11
  38. package/package.json +13 -11
  39. package/src/api.ts +1395 -0
  40. package/src/client.ts +413 -12
  41. package/src/ethers/index.ts +74 -28
  42. package/src/index.ts +96 -273
  43. package/src/key.ts +36 -131
  44. package/src/{fido.ts → mfa.ts} +62 -38
  45. package/src/org.ts +54 -405
  46. package/src/response.ts +196 -0
  47. package/src/role.ts +113 -184
  48. package/src/schema.ts +936 -28
  49. package/src/schema_types.ts +110 -0
  50. package/src/session/cognito_manager.ts +33 -6
  51. package/src/session/session_manager.ts +2 -8
  52. package/src/session/signer_session_manager.ts +29 -110
  53. package/src/signer_session.ts +22 -597
  54. package/src/user_export.ts +116 -0
  55. package/src/util.ts +29 -10
@@ -0,0 +1,116 @@
1
+ import { UserExportCompleteResponse, UserExportKeyMaterial } from "./schema_types";
2
+ import { decodeBase64 } from "./util";
3
+ import type { CipherSuite } from "@hpke/core";
4
+
5
+ /** Get the HPKE ciphersuite for user-export decryption.
6
+ *
7
+ * @return {any} The HPKE ciphersuite for user export.
8
+ */
9
+ export async function userExportCipherSuite(): Promise<CipherSuite> {
10
+ const hpke = await import("@hpke/core"); // eslint-disable-line @typescript-eslint/no-var-requires
11
+ const suite = new hpke.CipherSuite({
12
+ kem: new hpke.DhkemP256HkdfSha256(),
13
+ kdf: new hpke.HkdfSha256(),
14
+ aead: new hpke.Aes256Gcm(),
15
+ });
16
+ return suite;
17
+ }
18
+
19
+ /**
20
+ * Generate a key pair for user export.
21
+ *
22
+ * @return {Promise<CryptoKeyPair>} The newly generated key pair.
23
+ */
24
+ export async function userExportKeygen(): Promise<CryptoKeyPair> {
25
+ return (await userExportCipherSuite()).kem.generateKeyPair();
26
+ }
27
+
28
+ /**
29
+ * Get the ArrayBuffer slice represented by a Buffer
30
+ *
31
+ * @param {Uint8Array} b The buffer to convert
32
+ * @return {ArrayBuffer} The resulting ArrayBuffer
33
+ */
34
+ function toArrayBuffer(b: Uint8Array): ArrayBuffer {
35
+ return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
36
+ }
37
+
38
+ /**
39
+ * Decrypt a user export.
40
+ *
41
+ * @param {CryptoKey} recipientKey The NIST P-256 secret key corresponding to the `publicKey` argument to the `userExportComplete` invocation that returned `response`.
42
+ * @param {UserExportCompleteResponse} response The response from a successful `userExportComplete` request.
43
+ * @return {Promise<UserExportKeyMaterial>} The decrypted key material.
44
+ */
45
+ export async function userExportDecrypt(
46
+ recipientKey: CryptoKey,
47
+ response: UserExportCompleteResponse,
48
+ ): Promise<UserExportKeyMaterial> {
49
+ // The ciphersuite we use for decryption
50
+ const suite = await userExportCipherSuite();
51
+
52
+ // decrypt the export ciphertext using the HPKE one-shot API
53
+ const tenc = new TextEncoder();
54
+ const tdec = new TextDecoder();
55
+ const info = toArrayBuffer(tenc.encode(`cubist-signer::UserExportOwner::${response.user_id}`));
56
+ const public_key = toArrayBuffer(decodeBase64(response.ephemeral_public_key));
57
+ const ctxt = toArrayBuffer(decodeBase64(response.encrypted_key_material));
58
+ const decrypted: UserExportKeyMaterial = JSON.parse(
59
+ tdec.decode(
60
+ await suite.open(
61
+ {
62
+ recipientKey,
63
+ enc: public_key,
64
+ info: info,
65
+ },
66
+ ctxt,
67
+ ),
68
+ ),
69
+ );
70
+
71
+ return decrypted;
72
+ }
73
+
74
+ /**
75
+ * Figure out how to load SubtleCrypto in the current environment.
76
+ *
77
+ * This functionality is reproduced from the hpke-js package,
78
+ * https://github.com/dajiaji/hpke-js/
79
+ * which is Copyright (C) 2022 Ajitomi Daisuke and licensed
80
+ * under the MIT License, which follows:
81
+ *
82
+ * MIT License
83
+ *
84
+ * Copyright (c) 2022 Ajitomi Daisuke
85
+ *
86
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
87
+ * of this software and associated documentation files (the "Software"), to deal
88
+ * in the Software without restriction, including without limitation the rights
89
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
90
+ * copies of the Software, and to permit persons to whom the Software is
91
+ * furnished to do so, subject to the following conditions:
92
+ *
93
+ * The above copyright notice and this permission notice shall be included in all
94
+ * copies or substantial portions of the Software.
95
+ *
96
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
97
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
98
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
99
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
100
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
101
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
102
+ * SOFTWARE.
103
+ */
104
+ export async function loadSubtleCrypto() {
105
+ if (globalThis !== undefined && globalThis.crypto !== undefined) {
106
+ // Browsers, Node.js >= v19, Cloudflare Workers, Bun, etc.
107
+ return globalThis.crypto.subtle;
108
+ }
109
+ // Node.js <= v18
110
+ try {
111
+ const { webcrypto } = await import("crypto"); // node:crypto
112
+ return (webcrypto as unknown as Crypto).subtle;
113
+ } catch (e: unknown) {
114
+ throw new Error("subtle crypto not supported");
115
+ }
116
+ }
package/src/util.ts CHANGED
@@ -65,6 +65,18 @@ export function assertOk<D, T>(resp: ResponseType<D, T>, description?: string):
65
65
  return resp.data;
66
66
  }
67
67
 
68
+ /**
69
+ * Browser-friendly helper for decoding a 'base64'-encoded string into a byte array.
70
+ *
71
+ * @param {string} b64 The 'base64'-encoded string to decode
72
+ * @return {Uint8Array} Decoded byte array
73
+ */
74
+ export function decodeBase64(b64: string): Uint8Array {
75
+ return typeof Buffer === "function"
76
+ ? Buffer.from(b64, "base64")
77
+ : Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
78
+ }
79
+
68
80
  /**
69
81
  * Browser-friendly helper for decoding a 'base64url'-encoded string into a byte array.
70
82
  *
@@ -72,28 +84,35 @@ export function assertOk<D, T>(resp: ResponseType<D, T>, description?: string):
72
84
  * @return {Uint8Array} Decoded byte array
73
85
  */
74
86
  export function decodeBase64Url(b64url: string): Uint8Array {
75
- const b64 = b64url.replace(/-/g, "+").replace(/_/g, "/").replace(/=*$/g, "");
76
-
77
87
  // NOTE: there is no "base64url" encoding in the "buffer" module for the browser (unlike in node.js)
78
- return typeof Buffer === "function"
79
- ? Buffer.from(b64, "base64")
80
- : Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
88
+ const b64 = b64url.replace(/-/g, "+").replace(/_/g, "/").replace(/=*$/g, "");
89
+ return decodeBase64(b64);
81
90
  }
82
91
 
83
92
  /**
84
- * Browser-friendly helper for encoding a byte array into a 'base64url`-encoded string.
93
+ *
94
+ * Browser-friendly helper for encoding a byte array into a padded `base64`-encoded string.
85
95
  *
86
96
  * @param {Iterable<number>} buffer The byte array to encode
87
- * @return {string} The 'base64url' encoding of the byte array.
97
+ * @return {string} The 'base64' encoding of the byte array.
88
98
  */
89
- export function encodeToBase64Url(buffer: Iterable<number>): string {
99
+ export function encodeToBase64(buffer: Iterable<number>): string {
90
100
  const bytes = new Uint8Array(buffer);
91
-
92
- // NOTE: there is no "base64url" encoding in the "buffer" module for the browser (unlike in node.js)
93
101
  const b64 =
94
102
  typeof Buffer === "function"
95
103
  ? Buffer.from(bytes).toString("base64")
96
104
  : btoa(bytes.reduce((s, b) => s + String.fromCharCode(b), ""));
105
+ return b64;
106
+ }
97
107
 
108
+ /**
109
+ * Browser-friendly helper for encoding a byte array into a 'base64url`-encoded string.
110
+ *
111
+ * @param {Iterable<number>} buffer The byte array to encode
112
+ * @return {string} The 'base64url' encoding of the byte array.
113
+ */
114
+ export function encodeToBase64Url(buffer: Iterable<number>): string {
115
+ const b64 = encodeToBase64(buffer);
116
+ // NOTE: there is no "base64url" encoding in the "buffer" module for the browser (unlike in node.js)
98
117
  return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=*$/g, "");
99
118
  }