@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,196 @@
1
+ import { CubeSignerClient, SignerSession } from ".";
2
+ import { MfaReceipt } from "./mfa";
3
+ import { AcceptedResponse, NewSessionResponse } from "./schema_types";
4
+
5
+ /**
6
+ * Response type, which can be either a value of type {@link U}
7
+ * or {@link AcceptedResponse} (status code 202) which requires MFA.
8
+ */
9
+ export type Response<U> = U | AcceptedResponse;
10
+
11
+ /**
12
+ * Request function which optionally takes additional headers
13
+ * (which, for example, can be used to attach an MFA receipt).
14
+ */
15
+ export type RequestFn<U> = (headers?: HeadersInit) => Promise<Response<U>>;
16
+
17
+ /**
18
+ * Map function occasionally used to map a response from the API into a higher-level type.
19
+ */
20
+ export type MapFn<U, V> = (u: U) => V;
21
+
22
+ /**
23
+ * Take a {@link Response<U>} and a {@link MapFn<U, V>} function and return
24
+ * a {@link Response<V>} that maps the value of the original response when its status code is 200.
25
+ *
26
+ * @param {Response<U>} resp Original response
27
+ * @param {Map<U, V>} mapFn Map to apply to the response value when its status code is 200.
28
+ * @return {Response<V>} Response whose value for status code 200 is mapped from U to V
29
+ */
30
+ export function mapResponse<U, V>(resp: Response<U>, mapFn: MapFn<U, V>): Response<V> {
31
+ if ((resp as AcceptedResponse).accepted?.MfaRequired) {
32
+ return resp as AcceptedResponse;
33
+ } else {
34
+ return mapFn(resp as U);
35
+ }
36
+ }
37
+
38
+ export interface MfaRequired {
39
+ /** Org id */
40
+ org_id: string;
41
+ /** MFA request id */
42
+ id: string;
43
+ /** Optional MFA session */
44
+ session?: NewSessionResponse | null;
45
+ }
46
+
47
+ /**
48
+ * A response of a CubeSigner request.
49
+ */
50
+ export class CubeSignerResponse<U> {
51
+ readonly #requestFn: RequestFn<U>;
52
+ readonly #resp: U | AcceptedResponse;
53
+ /**
54
+ * Optional MFA id. Only set if there is an MFA request associated with the
55
+ * signing request
56
+ */
57
+ readonly #mfaRequired?: MfaRequired;
58
+
59
+ /** @return {string} The MFA id associated with this request (if any) */
60
+ mfaId(): string {
61
+ return this.#mfaRequired!.id;
62
+ }
63
+
64
+ /** @return {boolean} True if this request requires an MFA approval */
65
+ requiresMfa(): boolean {
66
+ return this.#mfaRequired !== undefined;
67
+ }
68
+
69
+ /**
70
+ * Return session information to use for any MFA approval requests (if any was included in the response).
71
+ * @return {ClientSessionInfo | undefined}
72
+ */
73
+ mfaSessionInfo(): NewSessionResponse | undefined {
74
+ return (this.#resp as AcceptedResponse).accepted?.MfaRequired?.session ?? undefined;
75
+ }
76
+
77
+ /** @return {U} The response data, if no MFA is required */
78
+ data(): U {
79
+ if (this.requiresMfa()) {
80
+ throw new Error("Cannot call `data()` while MFA is required");
81
+ }
82
+ return this.#resp as U;
83
+ }
84
+
85
+ /**
86
+ * Approve the MFA request using a given session and a TOTP code.
87
+ *
88
+ * @param {SignerSession} session Signer session to use
89
+ * @param {string} code 6-digit TOTP code
90
+ * @return {CubeSignerResponse<U>} The result of signing with the approval
91
+ */
92
+ async approveTotp(session: SignerSession, code: string): Promise<CubeSignerResponse<U>> {
93
+ if (!this.requiresMfa()) {
94
+ return this;
95
+ }
96
+
97
+ const mfaId = this.mfaId();
98
+ const mfaOrgId = this.#mfaRequired!.org_id;
99
+ const mfaApproval = await session.mfaApproveTotp(mfaId, code);
100
+ const mfaConf = mfaApproval.receipt?.confirmation;
101
+
102
+ if (!mfaConf) {
103
+ return this;
104
+ }
105
+
106
+ return await this.signWithMfaApproval({ mfaId, mfaOrgId, mfaConf });
107
+ }
108
+
109
+ /**
110
+ * Approve the MFA request using a given `CubeSignerClient` instance (i.e., its session).
111
+ *
112
+ * @param {CubeSignerClient} cs CubeSigner whose session to use
113
+ * @return {CubeSignerResponse<U>} The result of signing with the approval
114
+ */
115
+ async approve(cs: CubeSignerClient): Promise<CubeSignerResponse<U>> {
116
+ if (!this.requiresMfa()) {
117
+ return this;
118
+ }
119
+
120
+ const mfaId = this.#mfaRequired!.id;
121
+ const mfaOrgId = this.#mfaRequired!.org_id;
122
+
123
+ const mfaApproval = await cs.mfaApprove(mfaId);
124
+ const mfaConf = mfaApproval.receipt?.confirmation;
125
+
126
+ if (!mfaConf) {
127
+ return this;
128
+ }
129
+
130
+ return await this.signWithMfaApproval({ mfaId, mfaOrgId, mfaConf });
131
+ }
132
+
133
+ /**
134
+ * Resubmits the request with a given MFA receipt attached.
135
+ *
136
+ * @param {MfaReceipt} mfaReceipt The MFA receipt
137
+ * @return {Promise<CubeSignerResponse<U>>} The result of signing after MFA approval
138
+ */
139
+ async signWithMfaApproval(mfaReceipt: MfaReceipt): Promise<CubeSignerResponse<U>> {
140
+ const headers = CubeSignerResponse.getMfaHeaders(mfaReceipt);
141
+ return new CubeSignerResponse(this.#requestFn, await this.#requestFn(headers));
142
+ }
143
+
144
+ // --------------------------------------------------------------------------
145
+ // -- INTERNAL --------------------------------------------------------------
146
+ // --------------------------------------------------------------------------
147
+
148
+ /**
149
+ * Constructor.
150
+ *
151
+ * @param {RequestFn} requestFn
152
+ * The signing function that this response is from.
153
+ * This argument is used to resend requests with different headers if needed.
154
+ * @param {U | AcceptedResponse} resp The response as returned by the OpenAPI client.
155
+ * @internal
156
+ */
157
+ constructor(requestFn: RequestFn<U>, resp: U | AcceptedResponse) {
158
+ this.#requestFn = requestFn;
159
+ this.#resp = resp;
160
+ this.#mfaRequired = (this.#resp as AcceptedResponse).accepted?.MfaRequired;
161
+ }
162
+
163
+ /**
164
+ * Static constructor.
165
+ * @param {RequestFn} requestFn
166
+ * The request function that this response is from.
167
+ * This argument is used to resend requests with different headers if needed.
168
+ * @param {MfaReceipt} mfaReceipt Optional MFA receipt
169
+ * @return {Promise<CubeSignerResponse<U>>} New instance of this class.
170
+ * @internal
171
+ */
172
+ static async create<U>(
173
+ requestFn: RequestFn<U>,
174
+ mfaReceipt?: MfaReceipt,
175
+ ): Promise<CubeSignerResponse<U>> {
176
+ const seed = await requestFn(this.getMfaHeaders(mfaReceipt));
177
+ return new CubeSignerResponse(requestFn, seed);
178
+ }
179
+
180
+ /**
181
+ * Return HTTP headers containing a given MFA receipt.
182
+ *
183
+ * @param {MfaReceipt} mfaReceipt MFA receipt
184
+ * @return {HeadersInit} Headers including that receipt
185
+ * @internal
186
+ */
187
+ static getMfaHeaders(mfaReceipt?: MfaReceipt): HeadersInit | undefined {
188
+ return mfaReceipt
189
+ ? {
190
+ "x-cubist-mfa-id": mfaReceipt.mfaId,
191
+ "x-cubist-mfa-org-id": mfaReceipt.mfaOrgId,
192
+ "x-cubist-mfa-confirmation": mfaReceipt.mfaConf,
193
+ }
194
+ : undefined;
195
+ }
196
+ }
package/src/role.ts CHANGED
@@ -1,24 +1,23 @@
1
1
  import {
2
- CubeSigner,
3
2
  Key,
3
+ KeyWithPoliciesInfo,
4
4
  MfaType,
5
+ PageOpts,
6
+ RoleInfo,
5
7
  SignerSession,
6
8
  SignerSessionInfo,
7
9
  SignerSessionLifetime,
8
10
  SignerSessionManager,
9
11
  SignerSessionStorage,
12
+ UpdateRoleRequest,
10
13
  } from ".";
11
- import { components, paths } from "./client";
12
- import { assertOk } from "./util";
14
+ import { CubeSignerClient } from "./client";
13
15
 
14
- type UpdateRoleRequest =
15
- paths["/v0/org/{org_id}/keys/{key_id}"]["patch"]["requestBody"]["content"]["application/json"];
16
- type KeyWithPoliciesInfo = components["schemas"]["KeyInRoleInfo"];
17
- export type RoleInfo = components["schemas"]["RoleInfo"];
18
-
19
- /** Restrict transaction receiver.
16
+ /**
17
+ * Restrict transaction receiver.
18
+ *
20
19
  * @example { TxReceiver: "0x8c594691c0e592ffa21f153a16ae41db5befcaaa" }
21
- * */
20
+ */
22
21
  export type TxReceiver = { TxReceiver: string };
23
22
 
24
23
  /** The kind of deposit contracts. */
@@ -35,14 +34,18 @@ export type TxDeposit = TxDepositBase | TxDepositPubkey | TxDepositRole;
35
34
  /** Restrict transactions to calls to deposit contract*/
36
35
  export type TxDepositBase = { TxDeposit: { kind: DepositContract } };
37
36
 
38
- /** Restrict transactions to calls to deposit contract with fixed validator (pubkey):
39
- * @example { TxDeposit: { kind: DespositContract.Canonical, validator: { pubkey: "8879...8"} }}
40
- * */
37
+ /**
38
+ * Restrict transactions to calls to deposit contract with fixed validator (pubkey):
39
+ *
40
+ * @example { TxDeposit: { kind: DespositContract.Canonical, validator: { pubkey: "8879...8"} }}
41
+ */
41
42
  export type TxDepositPubkey = { TxDeposit: { kind: DepositContract; pubkey: string } };
42
43
 
43
- /** Restrict transactions to calls to deposit contract with any validator key in a role:
44
+ /**
45
+ * Restrict transactions to calls to deposit contract with any validator key in a role:
46
+ *
44
47
  * @example { TxDeposit: { kind: DespositContract.Canonical, validator: { role_id: "Role#c63...af"} }}
45
- * */
48
+ */
46
49
  export type TxDepositRole = { TxDeposit: { kind: DepositContract; role_id: string } };
47
50
 
48
51
  /** All different kinds of sensitive operations. */
@@ -55,7 +58,9 @@ export enum OperationKind {
55
58
  SolanaSign = "SolanaSign", // eslint-disable-line no-unused-vars
56
59
  }
57
60
 
58
- /** MFA policy
61
+ /**
62
+ * MFA policy
63
+ *
59
64
  * @example {
60
65
  * {
61
66
  * count: 1,
@@ -63,7 +68,7 @@ export enum OperationKind {
63
68
  * allowed_mfa_types: [ "Totp" ],
64
69
  * allowed_approvers: [ "User#123" ],
65
70
  * }
66
- * */
71
+ */
67
72
  export type MfaPolicy = {
68
73
  count?: number;
69
74
  num_auth_factors?: number;
@@ -72,7 +77,9 @@ export type MfaPolicy = {
72
77
  restricted_operations?: OperationKind[];
73
78
  };
74
79
 
75
- /** Require MFA for transactions.
80
+ /**
81
+ * Require MFA for transactions.
82
+ *
76
83
  * @example {
77
84
  * RequireMfa: {
78
85
  * count: 1,
@@ -84,7 +91,7 @@ export type MfaPolicy = {
84
91
  * ]
85
92
  * }
86
93
  * }
87
- * */
94
+ */
88
95
  export type RequireMfa = {
89
96
  RequireMfa: MfaPolicy;
90
97
  };
@@ -93,7 +100,9 @@ export type RequireMfa = {
93
100
  export const AllowRawBlobSigning = "AllowRawBlobSigning" as const;
94
101
  export type AllowRawBlobSigning = typeof AllowRawBlobSigning;
95
102
 
96
- /** Key policy
103
+ /**
104
+ * Key policy
105
+ *
97
106
  * @example [
98
107
  * {
99
108
  * "TxReceiver": "0x8c594691c0e592ffa21f153a16ae41db5befcaaa"
@@ -114,30 +123,29 @@ export type AllowRawBlobSigning = typeof AllowRawBlobSigning;
114
123
  * }
115
124
  * }
116
125
  * ]
117
- * */
126
+ */
118
127
  export type KeyPolicy = (TxReceiver | TxDeposit | RequireMfa | AllowRawBlobSigning)[];
119
128
 
120
129
  /** A key guarded by a policy. */
121
130
  export class KeyWithPolicies {
122
- readonly #cs: CubeSigner;
123
- readonly #orgId: string;
131
+ readonly #csc: CubeSignerClient;
124
132
  readonly keyId: string;
125
133
  readonly policy?: KeyPolicy;
126
134
 
127
135
  /** @return {Promise<Key>} The key */
128
136
  async getKey(): Promise<Key> {
129
- return await Key.getKey(this.#cs, this.#orgId, this.keyId);
137
+ const keyInfo = await this.#csc.keyGet(this.keyId);
138
+ return new Key(this.#csc, keyInfo);
130
139
  }
131
140
 
132
- /** Constructor.
133
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
134
- * @param {string} orgId The id of the organization to which the key belongs.
141
+ /**
142
+ * Constructor.
143
+ * @param {CubeSignerClient} csc The CubeSigner instance to use for signing.
135
144
  * @param {KeyWithPoliciesInfo} keyWithPolicies The key and its policies
136
145
  * @internal
137
- * */
138
- constructor(cs: CubeSigner, orgId: string, keyWithPolicies: KeyWithPoliciesInfo) {
139
- this.#cs = cs;
140
- this.#orgId = orgId;
146
+ */
147
+ constructor(csc: CubeSignerClient, keyWithPolicies: KeyWithPoliciesInfo) {
148
+ this.#csc = csc;
141
149
  this.keyId = keyWithPolicies.key_id;
142
150
  this.policy = keyWithPolicies.policy as unknown as KeyPolicy;
143
151
  }
@@ -145,20 +153,20 @@ export class KeyWithPolicies {
145
153
 
146
154
  /** Roles. */
147
155
  export class Role {
148
- readonly #cs: CubeSigner;
149
- readonly #orgId: string;
156
+ readonly #csc: CubeSignerClient;
157
+
150
158
  /** Human-readable name for the role */
151
159
  public readonly name?: string;
152
160
 
153
161
  /**
154
162
  * The ID of the role.
155
163
  * @example Role#bfe3eccb-731e-430d-b1e5-ac1363e6b06b
156
- * */
164
+ */
157
165
  readonly id: string;
158
166
 
159
167
  /** Delete the role. */
160
168
  async delete(): Promise<void> {
161
- await Role.deleteRole(this.#cs, this.#orgId, this.id);
169
+ await this.#csc.roleDelete(this.id);
162
170
  }
163
171
 
164
172
  /** Is the role enabled? */
@@ -177,222 +185,143 @@ export class Role {
177
185
  await this.update({ enabled: false });
178
186
  }
179
187
 
180
- /** The list of users with access to the role.
188
+ /**
189
+ * The list of all users with access to the role.
181
190
  * @example [
182
191
  * "User#c3b9379c-4e8c-4216-bd0a-65ace53cf98f",
183
192
  * "User#5593c25b-52e2-4fb5-b39b-96d41d681d82"
184
193
  * ]
185
- * */
186
- async users(): Promise<string[]> {
187
- const data = await this.fetch();
188
- return data.users || [];
194
+ *
195
+ * @param {PageOpts} page Optional pagination options; by default, retrieves all users.
196
+ */
197
+ async users(page?: PageOpts): Promise<string[]> {
198
+ const users = await this.#csc.roleUsersList(this.id, page).fetch();
199
+ return (users || []).map((u) => u.user_id);
189
200
  }
190
201
 
191
- /** Add a user to the role.
192
- * Adds an existing user to an existing role.
202
+ /**
203
+ * Add an existing user to an existing role.
204
+ *
193
205
  * @param {string} userId The user-id of the user to add to the role.
194
- * */
206
+ */
195
207
  async addUser(userId: string) {
196
- const resp = await (
197
- await this.#cs.management()
198
- ).put("/v0/org/{org_id}/roles/{role_id}/add_user/{user_id}", {
199
- params: { path: { org_id: this.#orgId, role_id: this.id, user_id: userId } },
200
- parseAs: "json",
201
- });
202
- assertOk(resp, "Failed to add user to role");
208
+ await this.#csc.roleUserAdd(this.id, userId);
203
209
  }
204
210
 
205
- /** The list of keys in the role.
211
+ /**
212
+ * The list of keys in the role.
206
213
  * @example [
207
214
  * {
208
215
  * id: "Key#bfe3eccb-731e-430d-b1e5-ac1363e6b06b",
209
216
  * policy: { TxReceiver: "0x8c594691c0e592ffa21f153a16ae41db5befcaaa" }
210
217
  * },
211
218
  * ]
212
- * */
213
- async keys(): Promise<KeyWithPolicies[]> {
214
- const data = await this.fetch();
215
- return (data.keys || []).map((k) => new KeyWithPolicies(this.#cs, this.#orgId, k));
219
+ *
220
+ * @param {PageOpts} page Optional pagination options; by default, retrieves all keys in this role.
221
+ */
222
+ async keys(page?: PageOpts): Promise<KeyWithPolicies[]> {
223
+ const keysInRole = await this.#csc.roleKeysList(this.id, page).fetch();
224
+ return keysInRole.map((k) => new KeyWithPolicies(this.#csc, k));
216
225
  }
217
226
 
218
- /** Add keys to the role.
219
- * Adds a list of existing keys to an existing role.
227
+ /**
228
+ * Add a list of existing keys to an existing role.
229
+ *
220
230
  * @param {Key[]} keys The list of keys to add to the role.
221
231
  * @param {KeyPolicy?} policy The optional policy to apply to each key.
222
- * */
232
+ */
223
233
  async addKeys(keys: Key[], policy?: KeyPolicy) {
224
- const resp = await (
225
- await this.#cs.management()
226
- ).put("/v0/org/{org_id}/roles/{role_id}/add_keys", {
227
- params: { path: { org_id: this.#orgId, role_id: this.id } },
228
- body: {
229
- key_ids: keys.map((k) => k.id),
230
- policy: (policy ?? null) as Record<string, never>[] | null,
231
- },
232
- parseAs: "json",
233
- });
234
- assertOk(resp, "Failed to add keys to role");
234
+ await this.#csc.roleKeysAdd(
235
+ this.id,
236
+ keys.map((k) => k.id),
237
+ policy,
238
+ );
235
239
  }
236
240
 
237
- /** Add a key to the role.
238
- * Adds an existing key to an existing role.
241
+ /**
242
+ * Add an existing key to an existing role.
243
+ *
239
244
  * @param {Key} key The key to add to the role.
240
245
  * @param {KeyPolicy?} policy The optional policy to apply to the key.
241
- * */
246
+ */
242
247
  async addKey(key: Key, policy?: KeyPolicy) {
243
- return await this.addKeys([key], policy);
248
+ await this.addKeys([key], policy);
244
249
  }
245
250
 
246
- /** Remove key from the role.
247
- * Removes an existing key from an existing role.
251
+ /**
252
+ * Remove an existing key from an existing role.
253
+ *
248
254
  * @param {Key} key The key to remove from the role.
249
- * */
255
+ */
250
256
  async removeKey(key: Key) {
251
- const resp = await (
252
- await this.#cs.management()
253
- ).del("/v0/org/{org_id}/roles/{role_id}/keys/{key_id}", {
254
- params: { path: { org_id: this.#orgId, role_id: this.id, key_id: key.id } },
255
- parseAs: "json",
256
- });
257
- assertOk(resp, "Failed to remove key from role");
257
+ await this.#csc.roleKeysRemove(this.id, key.id);
258
258
  }
259
259
 
260
260
  /**
261
261
  * Create a new session for this role.
262
262
  * @param {SignerSessionStorage} storage The session storage to use
263
263
  * @param {string} purpose Descriptive purpose.
264
- * @param {SignerSessionLifetime} ttl Optional session lifetimes.
264
+ * @param {SignerSessionLifetime} lifetimes Optional session lifetimes.
265
+ * @param {string[]} scopes Session scopes. Only `sign:*` scopes are allowed.
265
266
  * @return {Promise<SignerSession>} New signer session.
266
267
  */
267
268
  async createSession(
268
269
  storage: SignerSessionStorage,
269
270
  purpose: string,
270
- ttl?: SignerSessionLifetime,
271
+ lifetimes?: SignerSessionLifetime,
272
+ scopes?: string[],
271
273
  ): Promise<SignerSession> {
272
- const manager = await SignerSessionManager.create(
273
- this.#cs,
274
- storage,
275
- this.#orgId,
276
- this.id,
277
- purpose,
278
- ttl,
279
- );
274
+ const sessionData = await this.#csc.sessionCreateForRole(this.id, purpose, scopes, lifetimes);
275
+ await storage.save(sessionData);
276
+ const manager = await SignerSessionManager.loadFromStorage(storage);
280
277
  return new SignerSession(manager);
281
278
  }
282
279
 
283
280
  /**
284
281
  * List all signer sessions for this role. Returned objects can be used to
285
282
  * revoke individual sessions, but they cannot be used for authentication.
283
+ *
284
+ * @param {PageOpts} page Optional pagination options; by default, retrieves all sessions.
286
285
  * @return {Promise<SignerSessionInfo[]>} Signer sessions for this role.
287
286
  */
288
- async sessions(): Promise<SignerSessionInfo[]> {
289
- const resp = await (
290
- await this.#cs.management()
291
- ).get("/v0/org/{org_id}/roles/{role_id}/tokens", {
292
- params: { path: { org_id: this.#orgId, role_id: this.id } },
293
- });
294
- const data = assertOk(resp);
295
- return data.tokens.map(
296
- (t) => new SignerSessionInfo(this.#cs, this.#orgId, this.id, t.hash, t.purpose),
297
- );
287
+ async sessions(page?: PageOpts): Promise<SignerSessionInfo[]> {
288
+ const sessions = await this.#csc.sessionsList(this.id, page).fetch();
289
+ return sessions.map((t) => new SignerSessionInfo(this.#csc, t.session_id, t.purpose));
298
290
  }
299
291
 
300
292
  // --------------------------------------------------------------------------
301
293
  // -- INTERNAL --------------------------------------------------------------
302
294
  // --------------------------------------------------------------------------
303
295
 
304
- /** Create a new role.
305
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
306
- * @param {string} orgId The id of the organization to which the role belongs.
296
+ /**
297
+ * Constructor.
298
+ * @param {CubeSignerClient} csc The CubeSigner instance to use for signing.
307
299
  * @param {RoleInfo} data The JSON response from the API server.
308
300
  * @internal
309
- * */
310
- constructor(cs: CubeSigner, orgId: string, data: RoleInfo) {
311
- this.#cs = cs;
312
- this.#orgId = orgId;
301
+ */
302
+ constructor(csc: CubeSignerClient, data: RoleInfo) {
303
+ this.#csc = csc;
313
304
  this.id = data.role_id;
314
305
  this.name = data.name ?? undefined;
315
306
  }
316
307
 
317
- /** Update the role.
308
+ /**
309
+ * Update the role.
310
+ *
318
311
  * @param {UpdateRoleRequest} request The JSON request to send to the API server.
319
- * */
320
- private async update(request: UpdateRoleRequest): Promise<void> {
321
- const resp = await (
322
- await this.#cs.management()
323
- ).patch("/v0/org/{org_id}/roles/{role_id}", {
324
- params: { path: { org_id: this.#orgId, role_id: this.id } },
325
- body: request,
326
- parseAs: "json",
327
- });
328
- assertOk(resp);
329
- }
330
-
331
- /** Create new role.
332
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
333
- * @param {string} orgId The id of the organization to which the role belongs.
334
- * @param {string?} name The optional name of the role.
335
- * @return {Role} The new role.
336
- * @internal
337
- * */
338
- static async createRole(cs: CubeSigner, orgId: string, name?: string): Promise<Role> {
339
- const resp = await (
340
- await cs.management()
341
- ).post("/v0/org/{org_id}/roles", {
342
- params: { path: { org_id: orgId } },
343
- body: name ? { name } : undefined,
344
- parseAs: "json",
345
- });
346
- const data = assertOk(resp);
347
- return await Role.getRole(cs, orgId, data.role_id);
348
- }
349
-
350
- /** Get a role by id.
351
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
352
- * @param {string} orgId The id of the organization to which the role belongs.
353
- * @param {string} roleId The id of the role to get.
354
- * @return {Role} The role.
355
- * @internal
356
- * */
357
- static async getRole(cs: CubeSigner, orgId: string, roleId: string): Promise<Role> {
358
- const resp = await (
359
- await cs.management()
360
- ).get("/v0/org/{org_id}/roles/{role_id}", {
361
- params: { path: { org_id: orgId, role_id: roleId } },
362
- parseAs: "json",
363
- });
364
- const data = assertOk(resp);
365
- return new Role(cs, orgId, data);
312
+ * @return {Promise<RoleInfo>} The updated role information.
313
+ */
314
+ private async update(request: UpdateRoleRequest): Promise<RoleInfo> {
315
+ return await this.#csc.roleUpdate(this.id, request);
366
316
  }
367
317
 
368
- /** Fetches the role information.
318
+ /**
319
+ * Fetches the role information.
320
+ *
369
321
  * @return {RoleInfo} The role information.
370
322
  * @internal
371
- * */
323
+ */
372
324
  private async fetch(): Promise<RoleInfo> {
373
- const resp = await (
374
- await this.#cs.management()
375
- ).get("/v0/org/{org_id}/roles/{role_id}", {
376
- params: { path: { org_id: this.#orgId, role_id: this.id } },
377
- parseAs: "json",
378
- });
379
- const data = assertOk(resp);
380
- return data;
381
- }
382
-
383
- /** Delete role.
384
- * @param {CubeSigner} cs The CubeSigner instance to use for signing.
385
- * @param {string} orgId The id of the organization to which the role belongs.
386
- * @param {string} roleId The id of the role to delete.
387
- * @internal
388
- * */
389
- private static async deleteRole(cs: CubeSigner, orgId: string, roleId: string): Promise<void> {
390
- const resp = await (
391
- await cs.management()
392
- ).del("/v0/org/{org_id}/roles/{role_id}", {
393
- params: { path: { org_id: orgId, role_id: roleId } },
394
- parseAs: "json",
395
- });
396
- assertOk(resp);
325
+ return await this.#csc.roleGet(this.id);
397
326
  }
398
327
  }