@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,110 @@
1
+ import { MfaPolicy } from "./role";
2
+ import { components } from "./schema";
3
+ import { JsonMap } from "./util";
4
+
5
+ type schemas = components["schemas"];
6
+
7
+ export type UserInfo = schemas["UserInfo"];
8
+ export type ConfiguredMfa = schemas["ConfiguredMfa"];
9
+ export type RatchetConfig = schemas["RatchetConfig"];
10
+ export type IdentityProof = schemas["IdentityProof"];
11
+ export type TotpInfo = schemas["TotpInfo"];
12
+
13
+ export type OidcAuthResponse = schemas["NewSessionResponse"];
14
+ export type ApiAddFidoChallenge = schemas["FidoCreateChallengeResponse"];
15
+ export type ApiMfaFidoChallenge = schemas["FidoAssertChallenge"];
16
+
17
+ export type PublicKeyCredentialCreationOptions = schemas["PublicKeyCredentialCreationOptions"];
18
+ export type PublicKeyCredentialRequestOptions = schemas["PublicKeyCredentialRequestOptions"];
19
+ export type PublicKeyCredentialParameters = schemas["PublicKeyCredentialParameters"];
20
+ export type PublicKeyCredentialDescriptor = schemas["PublicKeyCredentialDescriptor"];
21
+ export type AuthenticatorSelectionCriteria = schemas["AuthenticatorSelectionCriteria"];
22
+ export type PublicKeyCredentialUserEntity = schemas["PublicKeyCredentialUserEntity"];
23
+ export type PublicKeyCredential = schemas["PublicKeyCredential"];
24
+
25
+ export type OrgInfo = schemas["OrgInfo"];
26
+ export type UserIdInfo = schemas["UserIdInfo"];
27
+ export type UpdateOrgRequest = schemas["UpdateOrgRequest"];
28
+ export type UpdateOrgResponse = schemas["UpdateOrgResponse"];
29
+
30
+ export type OidcIdentity = schemas["OIDCIdentity"];
31
+ export type MemberRole = schemas["MemberRole"];
32
+
33
+ export type SchemaKeyType = schemas["KeyType"];
34
+
35
+ export type ListKeysResponse = schemas["PaginatedListKeysResponse"];
36
+ export type UpdateKeyRequest = schemas["UpdateKeyRequest"];
37
+ export type KeyInfoApi = schemas["KeyInfo"];
38
+ export type KeyInRoleInfo = schemas["KeyInRoleInfo"];
39
+ export type UserInRoleInfo = schemas["UserInRoleInfo"];
40
+ export type KeyTypeApi = schemas["KeyType"];
41
+
42
+ export type ListRolesResponse = schemas["PaginatedListRolesResponse"];
43
+ export type ListRoleKeysResponse = schemas["PaginatedListRoleKeysResponse"];
44
+ export type ListRoleUsersResponse = schemas["PaginatedListRoleUsersResponse"];
45
+ export type UpdateRoleRequest = schemas["UpdateRoleRequest"];
46
+ export type KeyWithPoliciesInfo = schemas["KeyInRoleInfo"];
47
+ export type RoleInfo = schemas["RoleInfo"];
48
+
49
+ export type SessionInfo = schemas["SessionInfo"];
50
+ export type ClientSessionInfo = schemas["ClientSessionInfo"];
51
+ export type NewSessionResponse = schemas["NewSessionResponse"];
52
+ export type SessionsResponse = schemas["PaginatedSessionsResponse"];
53
+
54
+ export type CreateSignerSessionRequest = schemas["CreateTokenRequest"];
55
+ export type RefreshSignerSessionRequest = schemas["AuthData"];
56
+
57
+ export type EvmSignRequest = schemas["Eth1SignRequest"];
58
+ export type EvmSignResponse = schemas["Eth1SignResponse"];
59
+ export type Eth2SignRequest = schemas["Eth2SignRequest"];
60
+ export type Eth2SignResponse = schemas["Eth2SignResponse"];
61
+ export type Eth2StakeRequest = schemas["StakeRequest"];
62
+ export type Eth2StakeResponse = schemas["StakeResponse"];
63
+ export type Eth2UnstakeRequest = schemas["UnstakeRequest"];
64
+ export type Eth2UnstakeResponse = schemas["UnstakeResponse"];
65
+ export type BlobSignRequest = schemas["BlobSignRequest"];
66
+ export type BlobSignResponse = schemas["BlobSignResponse"];
67
+ export type BtcSignRequest = schemas["BtcSignRequest"];
68
+ export type BtcSignResponse = schemas["BtcSignResponse"];
69
+ export type SolanaSignRequest = schemas["SolanaSignRequest"];
70
+ export type SolanaSignResponse = schemas["SolanaSignResponse"];
71
+ export type AvaSignRequest = schemas["AvaSignRequest"];
72
+ export type AvaSignResponse = schemas["AvaSignResponse"];
73
+
74
+ export type AcceptedResponse = schemas["AcceptedResponse"];
75
+ export type ErrorResponse = schemas["ErrorResponse"];
76
+ export type BtcSignatureKind = schemas["BtcSignatureKind"];
77
+
78
+ export type MfaType = schemas["MfaType"];
79
+ export type MfaRequestInfo = schemas["MfaRequestInfo"];
80
+
81
+ export type UserExportInitRequest = schemas["UserExportInitRequest"];
82
+ export type UserExportInitResponse = schemas["UserExportInitResponse"];
83
+ export type UserExportCompleteRequest = schemas["UserExportCompleteRequest"];
84
+ export type UserExportCompleteResponse = schemas["UserExportCompleteResponse"];
85
+ export type UserExportListResponse = schemas["PaginatedUserExportListResponse"];
86
+ export type UserExportKeyMaterial = schemas["JsonKeyPackage"];
87
+
88
+ /** Options for a new OIDC user */
89
+ export interface CreateOidcUserOptions {
90
+ /** The role of an OIDC user, default is "Alien" */
91
+ memberRole?: MemberRole;
92
+ /** Optional MFA policy to associate with the user account */
93
+ mfaPolicy?: MfaPolicy;
94
+ }
95
+
96
+ /** Ava P- or X-chain transaction */
97
+ export type AvaTx = { P: AvaPChainTx } | { X: AvaXChainTx };
98
+
99
+ /** Ava P-chain transaction */
100
+ export type AvaPChainTx =
101
+ | { AddPermissionlessValidator: JsonMap }
102
+ | { AddSubnetValidator: JsonMap }
103
+ | { AddValidator: JsonMap }
104
+ | { CreateChain: JsonMap }
105
+ | { CreateSubnet: JsonMap }
106
+ | { Export: JsonMap }
107
+ | { Import: JsonMap };
108
+
109
+ /** Ava X-chain transaction */
110
+ export type AvaXChainTx = { Base: JsonMap } | { Export: JsonMap } | { Import: JsonMap };
@@ -1,10 +1,14 @@
1
- import { Client } from "../client";
1
+ import path from "path";
2
+ import { Client } from "../api";
2
3
  import { EnvInterface } from "../env";
3
- import { HasEnv, SessionManager } from "./session_manager";
4
- import { SessionStorage } from "./session_storage";
4
+ import { HasEnv, OrgSessionManager } from "./session_manager";
5
+ import { JsonFileSessionStorage, SessionStorage } from "./session_storage";
6
+ import { configDir } from "../util";
5
7
 
6
8
  /** JSON representation of our "management session" file format */
7
9
  export interface CognitoSessionObject {
10
+ /** The organization ID */
11
+ org_id: string;
8
12
  /** The email address of the user */
9
13
  email: string;
10
14
  /** The ID token */
@@ -23,7 +27,7 @@ export interface CognitoSessionInfo extends CognitoSessionObject, HasEnv {}
23
27
  export type CognitoSessionStorage = SessionStorage<CognitoSessionInfo>;
24
28
 
25
29
  /** The session manager for cognito (management) sessions */
26
- export class CognitoSessionManager extends SessionManager<CognitoSessionInfo> {
30
+ export class CognitoSessionManager extends OrgSessionManager<CognitoSessionInfo> {
27
31
  #client: Client;
28
32
 
29
33
  /**
@@ -116,19 +120,42 @@ export class CognitoSessionManager extends SessionManager<CognitoSessionInfo> {
116
120
  const sessionInfo = await storage.retrieve();
117
121
  return new CognitoSessionManager(
118
122
  sessionInfo.env["Dev-CubeSignerStack"],
123
+ sessionInfo.org_id,
119
124
  sessionInfo.id_token,
120
125
  storage,
121
126
  );
122
127
  }
123
128
 
129
+ /**
130
+ * Loads an existing management session and creates a Cognito session manager for it.
131
+ *
132
+ * @param {CognitoSessionStorage} storage Optional session storage to load
133
+ * the session from. If not specified, the management session from the config
134
+ * directory will be loaded.
135
+ * @return {Promise<CognitoSessionManager>} Cognito session manager
136
+ */
137
+ static async loadManagementSession(
138
+ storage?: CognitoSessionStorage,
139
+ ): Promise<CognitoSessionManager> {
140
+ return await CognitoSessionManager.loadFromStorage(
141
+ storage ?? new JsonFileSessionStorage(path.join(configDir(), "management-session.json")),
142
+ );
143
+ }
144
+
124
145
  /**
125
146
  * Constructor.
126
147
  * @param {EnvInterface} env The environment of the session
148
+ * @param {string} orgId The id of the org associated with this session
127
149
  * @param {string} token The current token of the session
128
150
  * @param {CognitoSessionStorage} storage The storage back end to use
129
151
  */
130
- private constructor(env: EnvInterface, token: string, storage: CognitoSessionStorage) {
131
- super(env, storage);
152
+ private constructor(
153
+ env: EnvInterface,
154
+ orgId: string,
155
+ token: string,
156
+ storage: CognitoSessionStorage,
157
+ ) {
158
+ super(env, orgId, storage);
132
159
  this.#client = this.createClient(token);
133
160
  }
134
161
  }
@@ -1,7 +1,6 @@
1
1
  import { SessionStorage } from "..";
2
2
  import { EnvInterface } from "../env";
3
- import { paths, Client } from "../client";
4
- import createClient from "openapi-fetch";
3
+ import { Client, createHttpClient } from "../api";
5
4
 
6
5
  const DEFAULT_EXPIRATION_BUFFER_SECS = 30;
7
6
 
@@ -83,12 +82,7 @@ export abstract class SessionManager<U> {
83
82
  * @return {Client} The new REST client
84
83
  */
85
84
  protected createClient(token: string): Client {
86
- return createClient<paths>({
87
- baseUrl: this.env.SignerApiRoot,
88
- headers: {
89
- Authorization: token,
90
- },
91
- });
85
+ return createHttpClient(this.env.SignerApiRoot, token);
92
86
  }
93
87
 
94
88
  /**
@@ -1,16 +1,13 @@
1
- import { CubeSigner, EnvInterface } from "..";
2
- import { assertOk } from "../util";
3
- import { components, paths, Client } from "../client";
1
+ import { EnvInterface } from "..";
2
+ import {
3
+ ClientSessionInfo,
4
+ NewSessionResponse,
5
+ RefreshSignerSessionRequest,
6
+ } from "../schema_types";
7
+ import { Client } from "../api";
4
8
  import { HasEnv, OrgSessionManager } from "./session_manager";
5
9
  import { MemorySessionStorage, SessionStorage } from "./session_storage";
6
-
7
- export type ClientSessionInfo = components["schemas"]["ClientSessionInfo"];
8
- export type NewSessionResponse = components["schemas"]["NewSessionResponse"];
9
-
10
- export type CreateSignerSessionRequest =
11
- paths["/v0/org/{org_id}/roles/{role_id}/tokens"]["post"]["requestBody"]["content"]["application/json"];
12
- export type RefreshSignerSessionRequest =
13
- paths["/v1/org/{org_id}/token/refresh"]["patch"]["requestBody"]["content"]["application/json"];
10
+ import { assertOk } from "../util";
14
11
 
15
12
  /** JSON representation of our "signer session" file format */
16
13
  export interface SignerSessionObject {
@@ -42,16 +39,8 @@ export interface SignerSessionLifetime {
42
39
  grace?: number;
43
40
  }
44
41
 
45
- const defaultSignerSessionLifetime: SignerSessionLifetime = {
46
- session: 604800,
47
- auth: 300,
48
- refresh: 86400,
49
- grace: 30,
50
- };
51
-
52
42
  /** Manager for signer sessions. */
53
43
  export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
54
- readonly cs?: CubeSigner;
55
44
  #client: Client;
56
45
 
57
46
  /**
@@ -64,8 +53,9 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
64
53
  }
65
54
 
66
55
  /**
67
- * Returns a client with the current session and refreshes the current
68
- * session. May **UPDATE/MUTATE** self.
56
+ * Refreshes the current session if needed, then returns a client using the current session.
57
+ *
58
+ * May **UPDATE/MUTATE** self.
69
59
  */
70
60
  async client(): Promise<Client> {
71
61
  await this.refreshIfNeeded();
@@ -74,19 +64,9 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
74
64
 
75
65
  /** Revokes the session. */
76
66
  async revoke(): Promise<void> {
77
- if (!this.cs) {
78
- throw new Error("No management session available");
79
- }
80
- const session = await this.storage.retrieve();
81
- const resp = await (
82
- await this.cs.management()
83
- ).del("/v0/org/{org_id}/session/{session_id}", {
84
- params: {
85
- path: {
86
- org_id: session.org_id,
87
- session_id: session.session_info.session_id,
88
- },
89
- },
67
+ const client = await this.client();
68
+ const resp = await client.del("/v0/org/{org_id}/session/self", {
69
+ params: { path: { org_id: this.orgId } },
90
70
  parseAs: "json",
91
71
  });
92
72
  assertOk(resp);
@@ -106,10 +86,11 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
106
86
  * Refreshes the session and **UPDATES/MUTATES** self.
107
87
  */
108
88
  async refresh(): Promise<void> {
109
- const session = await this.storage.retrieve();
110
- const csi = session.session_info;
89
+ const currSession = await this.storage.retrieve();
90
+
91
+ const csi = currSession.session_info;
111
92
  const resp = await this.#client.patch("/v1/org/{org_id}/token/refresh", {
112
- params: { path: { org_id: session.org_id } },
93
+ params: { path: { org_id: this.orgId } },
113
94
  body: <RefreshSignerSessionRequest>{
114
95
  epoch_num: csi.epoch,
115
96
  epoch_token: csi.epoch_token,
@@ -118,63 +99,14 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
118
99
  parseAs: "json",
119
100
  });
120
101
  const data = assertOk(resp);
121
- await this.storage.save(<SignerSessionData>{
122
- ...session,
102
+ const newSession = <SignerSessionData>{
103
+ ...currSession,
123
104
  session_info: data.session_info,
124
105
  token: data.token,
125
- });
126
- this.#client = this.createClient(data.token);
127
- }
128
-
129
- /**
130
- * Create a new signer session.
131
- * @param {CubeSigner} cs The CubeSigner instance
132
- * @param {SignerSessionStorage} storage The session storage to use
133
- * @param {string} orgId Org ID
134
- * @param {string} roleId Role ID
135
- * @param {string} purpose The purpose of the session
136
- * @param {SignerSessionLifetime} ttl Lifetime settings
137
- * @return {Promise<SignerSessionManager>} New signer session
138
- */
139
- static async create(
140
- cs: CubeSigner,
141
- storage: SignerSessionStorage,
142
- orgId: string,
143
- roleId: string,
144
- purpose: string,
145
- ttl?: SignerSessionLifetime,
146
- ): Promise<SignerSessionManager> {
147
- const resp = await (
148
- await cs.management()
149
- ).post("/v0/org/{org_id}/roles/{role_id}/tokens", {
150
- params: { path: { org_id: orgId, role_id: roleId } },
151
- body: {
152
- purpose,
153
- auth_lifetime: ttl?.auth || defaultSignerSessionLifetime.auth,
154
- refresh_lifetime: ttl?.refresh || defaultSignerSessionLifetime.refresh,
155
- session_lifetime: ttl?.session || defaultSignerSessionLifetime.session,
156
- grace_lifetime: ttl?.grace || defaultSignerSessionLifetime.grace,
157
- },
158
- parseAs: "json",
159
- });
160
- const data = assertOk(resp);
161
- const session_info = data.session_info;
162
- if (!session_info) {
163
- throw new Error("Signer session info missing");
164
- }
165
- const sessionData = {
166
- org_id: orgId,
167
- role_id: roleId,
168
- purpose,
169
- token: data.token,
170
- session_info,
171
- // Keep compatibility with tokens produced by CLI
172
- env: {
173
- ["Dev-CubeSignerStack"]: cs.env,
174
- },
175
106
  };
176
- await storage.save(sessionData);
177
- return new SignerSessionManager(sessionData, storage, cs);
107
+
108
+ await this.storage.save(newSession);
109
+ this.#client = this.createClient(newSession.token);
178
110
  }
179
111
 
180
112
  /**
@@ -206,36 +138,23 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
206
138
 
207
139
  /**
208
140
  * Uses an existing session to create a new signer session manager.
141
+ *
209
142
  * @param {SignerSessionStorage} storage The session storage to use
210
- * @param {CubeSigner} cs Optional CubeSigner instance.
211
- * Currently used for token revocation; will be completely removed
212
- * since token revocation should not require management session.
213
143
  * @return {Promise<SingerSession>} New signer session manager
214
144
  */
215
- static async loadFromStorage(
216
- storage: SignerSessionStorage,
217
- cs?: CubeSigner,
218
- ): Promise<SignerSessionManager> {
145
+ static async loadFromStorage(storage: SignerSessionStorage): Promise<SignerSessionManager> {
219
146
  const session = await storage.retrieve();
220
- return new SignerSessionManager(session, storage, cs);
147
+ return new SignerSessionManager(session, storage);
221
148
  }
222
149
 
223
150
  /**
224
151
  * Constructor.
152
+ *
225
153
  * @param {SignerSessionData} sessionData Session data
226
- * @param {SignerSessionStorage} storage The session storage to use
227
- * @param {CubeSigner} cs Optional CubeSigner instance.
228
- * Currently used for token revocation; will be completely removed
229
- * since token revocation should not require management session.
230
- * @internal
154
+ * @param {SignerSessionStorage} storage The session storage to use.
231
155
  */
232
- private constructor(
233
- sessionData: SignerSessionData,
234
- storage: SignerSessionStorage,
235
- cs?: CubeSigner,
236
- ) {
156
+ private constructor(sessionData: SignerSessionData, storage: SignerSessionStorage) {
237
157
  super(sessionData.env["Dev-CubeSignerStack"], sessionData.org_id, storage);
238
- this.cs = cs;
239
158
  this.#client = this.createClient(sessionData.token);
240
159
  }
241
160
  }