@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
package/src/org.ts CHANGED
@@ -1,16 +1,5 @@
1
- import {
2
- CubeSigner,
3
- MfaRequestInfo,
4
- IdentityProof,
5
- PageOpts,
6
- Page,
7
- PageQueryArgs,
8
- Paginator,
9
- } from ".";
10
- import { components, paths } from "./client";
11
- import { assertOk } from "./util";
12
- import { KeyType, Key } from "./key";
13
- import { MfaPolicy, Role, RoleInfo } from "./role";
1
+ import { CubeSignerClient } from "./client";
2
+ import { OrgInfo, SignerSessionManager, SignerSessionStorage } from ".";
14
3
 
15
4
  /** Organization id */
16
5
  export type OrgId = string;
@@ -54,443 +43,103 @@ export interface MaxDailyUnstakePolicy {
54
43
  MaxDailyUnstake: number;
55
44
  }
56
45
 
57
- type OrgInfo = components["schemas"]["OrgInfo"];
58
- type UserIdInfo = components["schemas"]["UserIdInfo"];
59
- type UpdateOrgRequest =
60
- paths["/v0/org/{org_id}"]["patch"]["requestBody"]["content"]["application/json"];
61
- type UpdateOrgResponse =
62
- paths["/v0/org/{org_id}"]["patch"]["responses"]["200"]["content"]["application/json"];
63
-
64
- export type OidcIdentity = components["schemas"]["OIDCIdentity"];
65
- export type MemberRole = components["schemas"]["MemberRole"];
66
-
67
- /** Options for a new OIDC user */
68
- export interface CreateOidcUserOptions {
69
- /** The role of an OIDC user, default is "Alien" */
70
- memberRole?: MemberRole;
71
- /** Optional MFA policy to associate with the user account */
72
- mfaPolicy?: MfaPolicy;
73
- }
74
-
75
- /** An organization. */
76
- export class Org {
77
- readonly #cs: CubeSigner;
78
- /**
79
- * The ID of the organization.
80
- * @example Org#124dfe3e-3bbd-487d-80c0-53c55e8ab87a
81
- */
82
- readonly #id: string;
83
-
46
+ /**
47
+ * An organization.
48
+ *
49
+ * Extends {@link CubeSignerClient} and provides a few org-specific methods on top.
50
+ */
51
+ export class Org extends CubeSignerClient {
84
52
  /**
85
53
  * @description The org id
86
54
  * @example Org#c3b9379c-4e8c-4216-bd0a-65ace53cf98f
87
- * */
55
+ */
88
56
  get id(): OrgId {
89
- return this.#id;
57
+ return this.orgId;
58
+ }
59
+
60
+ /**
61
+ * Obtain information about the current organization.
62
+ *
63
+ * Same as {@link orgGet}.
64
+ */
65
+ get info() {
66
+ return this.orgGet.bind(this);
90
67
  }
91
68
 
92
69
  /** Human-readable name for the org */
93
70
  async name(): Promise<string | undefined> {
94
- const data = await this.fetch();
95
- return data.name ?? undefined;
71
+ const org = await this.orgGet();
72
+ return org.name ?? undefined;
73
+ }
74
+
75
+ /** Get all keys in the org. */
76
+ get keys() {
77
+ return this.orgKeys.bind(this);
96
78
  }
97
79
 
98
- /** Set the human-readable name for the org.
80
+ /**
81
+ * Set the human-readable name for the org.
99
82
  * @param {string} name The new human-readable name for the org (must be alphanumeric).
100
83
  * @example my_org_name
101
- * */
84
+ */
102
85
  async setName(name: string) {
103
86
  if (!/^[a-zA-Z0-9_]{3,30}$/.test(name)) {
104
87
  throw new Error("Org name must be alphanumeric and between 3 and 30 characters");
105
88
  }
106
- await this.update({ name });
89
+ await this.orgUpdate({ name });
107
90
  }
108
91
 
109
92
  /** Is the org enabled? */
110
93
  async enabled(): Promise<boolean> {
111
- const data = await this.fetch();
112
- return data.enabled;
94
+ const org = await this.orgGet();
95
+ return org.enabled;
113
96
  }
114
97
 
115
98
  /** Enable the org. */
116
99
  async enable() {
117
- await this.update({ enabled: true });
100
+ await this.orgUpdate({ enabled: true });
118
101
  }
119
102
 
120
103
  /** Disable the org. */
121
104
  async disable() {
122
- await this.update({ enabled: false });
105
+ await this.orgUpdate({ enabled: false });
123
106
  }
124
107
 
125
108
  /** Get the policy for the org. */
126
109
  async policy(): Promise<OrgPolicy[]> {
127
- const data = await this.fetch();
128
- return (data.policy ?? []) as unknown as OrgPolicy[];
110
+ const org = await this.orgGet();
111
+ return (org.policy ?? []) as unknown as OrgPolicy[];
129
112
  }
130
113
 
131
- /** Set the policy for the org.
114
+ /**
115
+ * Set the policy for the org.
132
116
  * @param {OrgPolicy[]} policy The new policy for the org.
133
- * */
117
+ */
134
118
  async setPolicy(policy: OrgPolicy[]) {
135
119
  const p = policy as unknown as Record<string, never>[];
136
- await this.update({ policy: p });
137
- }
138
-
139
- /** Create a new signing key.
140
- * @param {KeyType} type The type of key to create.
141
- * @param {string?} ownerId The owner of the key. Defaults to the session's user.
142
- * @return {Key[]} The new keys.
143
- * */
144
- async createKey(type: KeyType, ownerId?: string): Promise<Key> {
145
- return (await Key.createKeys(this.#cs, this.id, type, 1, ownerId))[0];
146
- }
147
-
148
- /** Create new signing keys.
149
- * @param {KeyType} type The type of key to create.
150
- * @param {nummber} count The number of keys to create.
151
- * @param {string?} ownerId The owner of the keys. Defaults to the session's user.
152
- * @return {Key[]} The new keys.
153
- * */
154
- async createKeys(type: KeyType, count: number, ownerId?: string): Promise<Key[]> {
155
- return Key.createKeys(this.#cs, this.id, type, count, ownerId);
156
- }
157
-
158
- /**
159
- * Derives a key of the given type using the given derivation path and mnemonic.
160
- * The owner of the derived key will be the owner of the mnemonic.
161
- *
162
- * @param {KeyType} type Type of key to derive from the mnemonic.
163
- * @param {string} derivationPath Mnemonic derivation path used to generate new key.
164
- * @param {string} mnemonicId materialId of mnemonic key used to derive the new key.
165
- *
166
- * @return {Key} newly derived key.
167
- */
168
- async deriveKey(type: KeyType, derivationPath: string, mnemonicId: string): Promise<Key> {
169
- return (await Key.deriveKeys(this.#cs, this.id, type, [derivationPath], mnemonicId))[0];
170
- }
171
-
172
- /**
173
- * Derives a set of keys of the given type using the given derivation paths and mnemonic.
174
- *
175
- * The owner of the derived keys will be the owner of the mnemonic.
176
- *
177
- * @param {KeyType} type Type of key to derive from the mnemonic.
178
- * @param {string[]} derivationPaths Mnemonic derivation paths used to generate new key.
179
- * @param {string} mnemonicId materialId of mnemonic key used to derive the new key.
180
- *
181
- * @return {Key[]} newly derived keys.
182
- */
183
- async deriveKeys(type: KeyType, derivationPaths: string[], mnemonicId: string): Promise<Key[]> {
184
- return await Key.deriveKeys(this.#cs, this.#id, type, derivationPaths, mnemonicId);
185
- }
186
-
187
- /**
188
- * Create a new user in the organization and sends an invitation to that user
189
- * @param {string} email Email of the user
190
- * @param {string} name The full name of the user
191
- */
192
- async createUser(email: string, name: string): Promise<void> {
193
- const resp = await (
194
- await this.#cs.management()
195
- ).post("/v0/org/{org_id}/invite", {
196
- params: { path: { org_id: this.id } },
197
- body: {
198
- email,
199
- name,
200
- skip_email: false,
201
- },
202
- parseAs: "json",
203
- });
204
- assertOk(resp);
205
- }
206
-
207
- /**
208
- * Create a new OIDC user
209
- * @param {OidcIdentity} identity The identity of the OIDC user
210
- * @param {string} email Email of the OIDC user
211
- * @param {CreateOidcUserOptions} opts Additional options for new OIDC users
212
- * @return {string} User id of the new user
213
- */
214
- async createOidcUser(
215
- identity: OidcIdentity,
216
- email: string,
217
- opts: CreateOidcUserOptions = {},
218
- ): Promise<string> {
219
- const resp = await (
220
- await this.#cs.management()
221
- ).post("/v0/org/{org_id}/users", {
222
- params: { path: { org_id: this.id } },
223
- body: {
224
- identity,
225
- role: opts.memberRole ?? "Alien",
226
- email: email,
227
- mfa_policy: opts.mfaPolicy ?? null,
228
- },
229
- parseAs: "json",
230
- });
231
- return assertOk(resp).user_id;
232
- }
233
-
234
- /**
235
- * Delete an existing OIDC user
236
- * @param {OidcIdentity} identity The identity of the OIDC user
237
- */
238
- async deleteOidcUser(identity: OidcIdentity) {
239
- const resp = await (
240
- await this.#cs.management()
241
- ).del("/v0/org/{org_id}/users/oidc", {
242
- params: { path: { org_id: this.id } },
243
- body: identity,
244
- parseAs: "json",
245
- });
246
- return assertOk(resp);
247
- }
248
-
249
- /**
250
- * Checks if a given proof of OIDC authentication is valid.
251
- *
252
- * @param {IdentityProof} proof The proof of authentication.
253
- */
254
- async verifyIdentity(proof: IdentityProof) {
255
- await this.#cs.verifyIdentity(this.id, proof);
256
- }
257
-
258
- /**
259
- * List users in the organization
260
- * @return {UserIdInfo[]} List of users
261
- */
262
- async users(): Promise<UserIdInfo[]> {
263
- const resp = await (
264
- await this.#cs.management()
265
- ).get("/v0/org/{org_id}/users", {
266
- params: { path: { org_id: this.id } },
267
- parseAs: "json",
268
- });
269
- return assertOk(resp).users;
270
- }
271
-
272
- /** Get a key by id.
273
- * @param {string} keyId The id of the key to get.
274
- * @return {Key} The key.
275
- * */
276
- async getKey(keyId: string): Promise<Key> {
277
- return await Key.getKey(this.#cs, this.id, keyId);
278
- }
279
-
280
- /** Get all keys in the org.
281
- * @param {KeyType?} type Optional key type to filter list for.
282
- * @param {PageOpts} page Pagination options. Defaults to fetching the entire result set.
283
- * @return {Key} The key.
284
- * */
285
- async keys(type?: KeyType, page?: PageOpts): Promise<Key[]> {
286
- page ??= Page.default();
287
- const listFn = async (query: PageQueryArgs) => {
288
- const client = await this.#cs.management();
289
- const resp = await client.get("/v0/org/{org_id}/keys", {
290
- params: {
291
- path: { org_id: this.id },
292
- query: {
293
- key_type: type,
294
- ...query,
295
- },
296
- },
297
- parseAs: "json",
298
- });
299
- return assertOk(resp);
300
- };
301
- const p = new Paginator(
302
- page,
303
- listFn,
304
- (r) => r.keys,
305
- (r) => r.last_evaluated_key,
306
- );
307
- const keys = await p.fetch();
308
- return keys.map((k) => new Key(this.#cs, this.id, k));
309
- }
310
-
311
- /** Create a new role.
312
- * @param {string?} name The name of the role.
313
- * @return {Role} The new role.
314
- * */
315
- async createRole(name?: string): Promise<Role> {
316
- return Role.createRole(this.#cs, this.id, name);
317
- }
318
-
319
- /** Get a role by id or name.
320
- * @param {string} roleId The id or name of the role to get.
321
- * @return {Role} The role.
322
- * */
323
- async getRole(roleId: string): Promise<Role> {
324
- return Role.getRole(this.#cs, this.id, roleId);
325
- }
326
-
327
- /**
328
- * List all roles in the org.
329
- *
330
- * @param {PageOpts} page Pagination options. Defaults to fetching the entire result set.
331
- * @return {Role[]} The roles.
332
- * */
333
- async listRoles(page?: PageOpts): Promise<Role[]> {
334
- return Org.roles(this.#cs, this.id, page);
335
- }
336
-
337
- /** List all users in the org.
338
- * @return {User[]} The users.
339
- * */
340
- async listUsers(): Promise<UserIdInfo[]> {
341
- return Org.users(this.#cs, this.id);
342
- }
343
-
344
- /**
345
- * Get a pending MFA request by its id.
346
- * @param {string} mfaId The id of the MFA request.
347
- * @return {Promise<MfaRequestInfo>} The MFA request.
348
- *
349
- * @deprecated Use {@link getMfaInfo()} instead.
350
- */
351
- async mfaGet(mfaId: string): Promise<MfaRequestInfo> {
352
- return await this.getMfaInfo(mfaId);
120
+ await this.orgUpdate({ policy: p });
353
121
  }
354
122
 
355
123
  /**
356
- * Approve a pending MFA request.
357
- *
358
- * @param {string} mfaId The id of the MFA request.
359
- * @return {Promise<MfaRequestInfo>} The MFA request.
360
- *
361
- * @deprecated Use {@link approveMfaRequest()} instead.
362
- */
363
- async mfaApprove(mfaId: string): Promise<MfaRequestInfo> {
364
- return await this.approveMfaRequest(mfaId);
365
- }
366
-
367
- /**
368
- * Get a pending MFA request by its id.
369
- * @param {string} mfaId The id of the MFA request.
370
- * @return {Promise<MfaRequestInfo>} The MFA request.
371
- */
372
- async getMfaInfo(mfaId: string): Promise<MfaRequestInfo> {
373
- return await this.#cs.mfaGet(this.id, mfaId);
374
- }
375
-
376
- /**
377
- * List pending MFA requests accessible to the current user.
378
- * @return {Promise<MfaRequestInfo[]>} The MFA requests.
379
- */
380
- async listMfaInfos(): Promise<MfaRequestInfo[]> {
381
- return await this.#cs.mfaList(this.id);
382
- }
383
-
384
- /**
385
- * Approve a pending MFA request.
386
- *
387
- * @param {string} mfaId The id of the MFA request.
388
- * @return {Promise<MfaRequestInfo>} The MFA request.
124
+ * Retrieve the org associated with a session.
125
+ * @param {SessionStorage} storage The session
126
+ * @return {Org} An {@link Org} instance for the org associated with this session.
389
127
  */
390
- async approveMfaRequest(mfaId: string): Promise<MfaRequestInfo> {
391
- return Org.mfaApprove(this.#cs, this.#id, mfaId);
392
- }
393
-
394
- // --------------------------------------------------------------------------
395
- // -- INTERNAL --------------------------------------------------------------
396
- // --------------------------------------------------------------------------
397
-
398
- /** Create a new org.
399
- * @param {CubeSigner} cs The CubeSigner instance.
400
- * @param {OrgInfo} data The JSON response from the API server.
401
- * @internal
402
- * */
403
- constructor(cs: CubeSigner, data: OrgInfo) {
404
- this.#cs = cs;
405
- this.#id = data.org_id;
128
+ static async retrieveFromStorage(storage: SignerSessionStorage): Promise<Org> {
129
+ const sessionMgr = await SignerSessionManager.loadFromStorage(storage);
130
+ return new Org(new CubeSignerClient(sessionMgr), sessionMgr.orgId);
406
131
  }
407
132
 
408
133
  /**
409
- * Approve a pending MFA request.
410
- *
411
- * @param {CubeSigner} cs The CubeSigner instance to use for requests
412
- * @param {string} orgId The org id of the MFA request
413
- * @param {string} mfaId The id of the MFA request
414
- * @return {Promise<MfaRequestInfo>} The result of the MFA request
134
+ * Constructor.
135
+ * @param {CubeSignerClient | SignerSessionManager} csc The CubeSigner instance.
136
+ * @param {OrgInfo| string} data Either org id or name or {@link OrgInfo}.
415
137
  */
416
- static async mfaApprove(cs: CubeSigner, orgId: string, mfaId: string): Promise<MfaRequestInfo> {
417
- return await cs.mfaApprove(orgId, mfaId);
418
- }
419
-
420
- /** Fetch org info.
421
- * @return {OrgInfo} The org info.
422
- * */
423
- private async fetch(): Promise<OrgInfo> {
424
- const resp = await (
425
- await this.#cs.management()
426
- ).get("/v0/org/{org_id}", {
427
- params: { path: { org_id: this.id } },
428
- parseAs: "json",
429
- });
430
- const data = assertOk(resp);
431
- return data;
432
- }
433
-
434
- /** Update the org.
435
- * @param {UpdateOrgRequest} request The JSON request to send to the API server.
436
- * @return {UpdateOrgResponse} The JSON response from the API server.
437
- * */
438
- private async update(request: UpdateOrgRequest): Promise<UpdateOrgResponse> {
439
- const resp = await (
440
- await this.#cs.management()
441
- ).patch("/v0/org/{org_id}", {
442
- params: { path: { org_id: this.id } },
443
- body: request,
444
- parseAs: "json",
445
- });
446
- return assertOk(resp);
447
- }
448
-
449
- /** List roles.
450
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
451
- * @param {string} orgId The id of the organization to which the role belongs.
452
- * @param {PageOpts} page Pagination options. Defaults to fetching the entire result set.
453
- * @return {Role[]} Org roles.
454
- * @internal
455
- * */
456
- private static async roles(cs: CubeSigner, orgId: string, page?: PageOpts): Promise<Role[]> {
457
- page ??= Page.default();
458
- const listFn = async (query: PageQueryArgs) => {
459
- const resp = await (
460
- await cs.management()
461
- ).get("/v0/org/{org_id}/roles", {
462
- params: {
463
- path: { org_id: orgId },
464
- query,
465
- },
466
- parseAs: "json",
467
- });
468
- return assertOk(resp);
469
- };
470
- const p = new Paginator(
471
- page,
472
- listFn,
473
- (u) => u.roles,
474
- (u) => u.last_evaluated_key,
475
- );
476
- const roles = await p.fetch();
477
- return roles.map((r: RoleInfo) => new Role(cs, orgId, r));
478
- }
138
+ constructor(csc: CubeSignerClient | SignerSessionManager, data?: OrgInfo | string) {
139
+ const mgr = csc instanceof CubeSignerClient ? csc.sessionMgr : (csc as SignerSessionManager);
479
140
 
480
- /** List users.
481
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
482
- * @param {string} orgId The id of the organization to which the role belongs.
483
- * @return {User[]} Org users.
484
- * @internal
485
- * */
486
- private static async users(cs: CubeSigner, orgId: string): Promise<UserIdInfo[]> {
487
- const resp = await (
488
- await cs.management()
489
- ).get("/v0/org/{org_id}/users", {
490
- params: { path: { org_id: orgId } },
491
- parseAs: "json",
492
- });
493
- const data = assertOk(resp);
494
- return data.users;
141
+ // NOTE: data can be OrgInfo for backward compatibility reasons
142
+ const orgId = typeof data === "string" ? data : data?.org_id;
143
+ super(mgr, orgId);
495
144
  }
496
145
  }