@cubist-labs/cubesigner-sdk 0.1.50 → 0.2.2

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 (47) hide show
  1. package/README.md +66 -13
  2. package/dist/src/client.d.ts +434 -7
  3. package/dist/src/client.js +1022 -18
  4. package/dist/src/ethers/index.d.ts +2 -4
  5. package/dist/src/ethers/index.js +11 -9
  6. package/dist/src/fido.d.ts +76 -0
  7. package/dist/src/fido.js +148 -0
  8. package/dist/src/index.d.ts +102 -30
  9. package/dist/src/index.js +126 -72
  10. package/dist/src/key.d.ts +15 -45
  11. package/dist/src/key.js +31 -93
  12. package/dist/src/mfa.d.ts +85 -14
  13. package/dist/src/mfa.js +158 -40
  14. package/dist/src/org.d.ts +237 -123
  15. package/dist/src/org.js +108 -213
  16. package/dist/src/paginator.d.ts +76 -0
  17. package/dist/src/paginator.js +99 -0
  18. package/dist/src/role.d.ts +76 -74
  19. package/dist/src/role.js +79 -136
  20. package/dist/src/schema.d.ts +1672 -520
  21. package/dist/src/schema.js +1 -1
  22. package/dist/src/schema_types.d.ts +103 -0
  23. package/dist/src/schema_types.js +3 -0
  24. package/dist/src/session/session_manager.js +2 -2
  25. package/dist/src/session/session_storage.js +1 -1
  26. package/dist/src/session/signer_session_manager.d.ts +16 -29
  27. package/dist/src/session/signer_session_manager.js +27 -78
  28. package/dist/src/signer_session.d.ts +232 -125
  29. package/dist/src/signer_session.js +149 -250
  30. package/dist/src/util.d.ts +20 -0
  31. package/dist/src/util.js +31 -2
  32. package/package.json +13 -11
  33. package/src/client.ts +1217 -7
  34. package/src/ethers/index.ts +11 -18
  35. package/src/index.ts +149 -101
  36. package/src/key.ts +28 -121
  37. package/src/mfa.ts +202 -0
  38. package/src/org.ts +126 -275
  39. package/src/paginator.ts +122 -0
  40. package/src/role.ts +108 -181
  41. package/src/schema.ts +1673 -520
  42. package/src/schema_types.ts +103 -0
  43. package/src/session/session_manager.ts +2 -2
  44. package/src/session/session_storage.ts +1 -1
  45. package/src/session/signer_session_manager.ts +38 -108
  46. package/src/signer_session.ts +164 -323
  47. package/src/util.ts +41 -0
@@ -0,0 +1,103 @@
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
+ /** Options for a new OIDC user */
82
+ export interface CreateOidcUserOptions {
83
+ /** The role of an OIDC user, default is "Alien" */
84
+ memberRole?: MemberRole;
85
+ /** Optional MFA policy to associate with the user account */
86
+ mfaPolicy?: MfaPolicy;
87
+ }
88
+
89
+ /** Ava P- or X-chain transaction */
90
+ export type AvaTx = { P: AvaPChainTx } | { X: AvaXChainTx };
91
+
92
+ /** Ava P-chain transaction */
93
+ export type AvaPChainTx =
94
+ | { AddPermissionlessValidator: JsonMap }
95
+ | { AddSubnetValidator: JsonMap }
96
+ | { AddValidator: JsonMap }
97
+ | { CreateChain: JsonMap }
98
+ | { CreateSubnet: JsonMap }
99
+ | { Export: JsonMap }
100
+ | { Import: JsonMap };
101
+
102
+ /** Ava X-chain transaction */
103
+ export type AvaXChainTx = { Base: JsonMap } | { Export: JsonMap } | { Import: JsonMap };
@@ -1,6 +1,6 @@
1
1
  import { SessionStorage } from "..";
2
2
  import { EnvInterface } from "../env";
3
- import { paths, Client } from "../client";
3
+ import { Client, paths } from "../client";
4
4
  import createClient from "openapi-fetch";
5
5
 
6
6
  const DEFAULT_EXPIRATION_BUFFER_SECS = 30;
@@ -98,7 +98,7 @@ export abstract class SessionManager<U> {
98
98
  * @return {boolean} True if the timestamp has expired
99
99
  */
100
100
  protected hasExpired(exp: number, buffer?: number): boolean {
101
- return exp < new Date().getTime() / 1000 + (buffer || DEFAULT_EXPIRATION_BUFFER_SECS);
101
+ return exp < new Date().getTime() + (buffer || DEFAULT_EXPIRATION_BUFFER_SECS) * 1000;
102
102
  }
103
103
 
104
104
  /**
@@ -44,7 +44,7 @@ export class MemorySessionStorage<U> implements SessionStorage<U> {
44
44
 
45
45
  /** Stores session information in a JSON file */
46
46
  export class JsonFileSessionStorage<U> implements SessionStorage<U> {
47
- #filePath: string;
47
+ readonly #filePath: string;
48
48
 
49
49
  /**
50
50
  * Store session information.
@@ -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 "../client";
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 {
@@ -38,17 +35,12 @@ export interface SignerSessionLifetime {
38
35
  auth: number;
39
36
  /** Refresh token lifetime (in seconds). Defaults to one day (86400). */
40
37
  refresh?: number;
38
+ /** Grace lifetime (in seconds). Defaults to 30 seconds (30). */
39
+ grace?: number;
41
40
  }
42
41
 
43
- const defaultSignerSessionLifetime: SignerSessionLifetime = {
44
- session: 604800,
45
- auth: 300,
46
- refresh: 86400,
47
- };
48
-
49
42
  /** Manager for signer sessions. */
50
43
  export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
51
- readonly cs?: CubeSigner;
52
44
  #client: Client;
53
45
 
54
46
  /**
@@ -61,29 +53,27 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
61
53
  }
62
54
 
63
55
  /**
64
- * Returns a client with the current session and refreshes the current
65
- * 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.
66
59
  */
67
60
  async client(): Promise<Client> {
68
61
  await this.refreshIfNeeded();
69
62
  return this.#client;
70
63
  }
71
64
 
65
+ /**
66
+ * @return {Client} A client using the current session (without attempting to refresh it).
67
+ */
68
+ clientNoRefresh(): Client {
69
+ return this.#client;
70
+ }
71
+
72
72
  /** Revokes the session. */
73
73
  async revoke(): Promise<void> {
74
- if (!this.cs) {
75
- throw new Error("No management session available");
76
- }
77
- const session = await this.storage.retrieve();
78
- const resp = await (
79
- await this.cs.management()
80
- ).del("/v0/org/{org_id}/session/{session_id}", {
81
- params: {
82
- path: {
83
- org_id: session.org_id,
84
- session_id: session.session_info.session_id,
85
- },
86
- },
74
+ const client = await this.client();
75
+ const resp = await client.del("/v0/org/{org_id}/session/self", {
76
+ params: { path: { org_id: this.orgId } },
87
77
  parseAs: "json",
88
78
  });
89
79
  assertOk(resp);
@@ -96,17 +86,18 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
96
86
  */
97
87
  async isStale(): Promise<boolean> {
98
88
  const session = await this.storage.retrieve();
99
- return this.hasExpired(session.session_info.auth_token_exp);
89
+ return this.hasExpired(session.session_info.auth_token_exp * 1000);
100
90
  }
101
91
 
102
92
  /**
103
93
  * Refreshes the session and **UPDATES/MUTATES** self.
104
94
  */
105
95
  async refresh(): Promise<void> {
106
- const session = await this.storage.retrieve();
107
- const csi = session.session_info;
96
+ const currSession = await this.storage.retrieve();
97
+
98
+ const csi = currSession.session_info;
108
99
  const resp = await this.#client.patch("/v1/org/{org_id}/token/refresh", {
109
- params: { path: { org_id: session.org_id } },
100
+ params: { path: { org_id: this.orgId } },
110
101
  body: <RefreshSignerSessionRequest>{
111
102
  epoch_num: csi.epoch,
112
103
  epoch_token: csi.epoch_token,
@@ -115,62 +106,14 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
115
106
  parseAs: "json",
116
107
  });
117
108
  const data = assertOk(resp);
118
- await this.storage.save(<SignerSessionData>{
119
- ...session,
109
+ const newSession = <SignerSessionData>{
110
+ ...currSession,
120
111
  session_info: data.session_info,
121
112
  token: data.token,
122
- });
123
- this.#client = this.createClient(data.token);
124
- }
125
-
126
- /**
127
- * Create a new signer session.
128
- * @param {CubeSigner} cs The CubeSigner instance
129
- * @param {SignerSessionStorage} storage The session storage to use
130
- * @param {string} orgId Org ID
131
- * @param {string} roleId Role ID
132
- * @param {string} purpose The purpose of the session
133
- * @param {SignerSessionLifetime} ttl Lifetime settings
134
- * @return {Promise<SignerSessionManager>} New signer session
135
- */
136
- static async create(
137
- cs: CubeSigner,
138
- storage: SignerSessionStorage,
139
- orgId: string,
140
- roleId: string,
141
- purpose: string,
142
- ttl?: SignerSessionLifetime,
143
- ): Promise<SignerSessionManager> {
144
- const resp = await (
145
- await cs.management()
146
- ).post("/v0/org/{org_id}/roles/{role_id}/tokens", {
147
- params: { path: { org_id: orgId, role_id: roleId } },
148
- body: {
149
- purpose,
150
- auth_lifetime: ttl?.auth || defaultSignerSessionLifetime.auth,
151
- refresh_lifetime: ttl?.refresh || defaultSignerSessionLifetime.refresh,
152
- session_lifetime: ttl?.session || defaultSignerSessionLifetime.session,
153
- },
154
- parseAs: "json",
155
- });
156
- const data = assertOk(resp);
157
- const session_info = data.session_info;
158
- if (!session_info) {
159
- throw new Error("Signer session info missing");
160
- }
161
- const sessionData = {
162
- org_id: orgId,
163
- role_id: roleId,
164
- purpose,
165
- token: data.token,
166
- session_info,
167
- // Keep compatibility with tokens produced by CLI
168
- env: {
169
- ["Dev-CubeSignerStack"]: cs.env,
170
- },
171
113
  };
172
- await storage.save(sessionData);
173
- return new SignerSessionManager(sessionData, storage, cs);
114
+
115
+ await this.storage.save(newSession);
116
+ this.#client = this.createClient(newSession.token);
174
117
  }
175
118
 
176
119
  /**
@@ -202,36 +145,23 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
202
145
 
203
146
  /**
204
147
  * Uses an existing session to create a new signer session manager.
148
+ *
205
149
  * @param {SignerSessionStorage} storage The session storage to use
206
- * @param {CubeSigner} cs Optional CubeSigner instance.
207
- * Currently used for token revocation; will be completely removed
208
- * since token revocation should not require management session.
209
150
  * @return {Promise<SingerSession>} New signer session manager
210
151
  */
211
- static async loadFromStorage(
212
- storage: SignerSessionStorage,
213
- cs?: CubeSigner,
214
- ): Promise<SignerSessionManager> {
152
+ static async loadFromStorage(storage: SignerSessionStorage): Promise<SignerSessionManager> {
215
153
  const session = await storage.retrieve();
216
- return new SignerSessionManager(session, storage, cs);
154
+ return new SignerSessionManager(session, storage);
217
155
  }
218
156
 
219
157
  /**
220
158
  * Constructor.
159
+ *
221
160
  * @param {SignerSessionData} sessionData Session data
222
161
  * @param {SignerSessionStorage} storage The session storage to use
223
- * @param {CubeSigner} cs Optional CubeSigner instance.
224
- * Currently used for token revocation; will be completely removed
225
- * since token revocation should not require management session.
226
- * @internal
227
162
  */
228
- private constructor(
229
- sessionData: SignerSessionData,
230
- storage: SignerSessionStorage,
231
- cs?: CubeSigner,
232
- ) {
163
+ constructor(sessionData: SignerSessionData, storage: SignerSessionStorage) {
233
164
  super(sessionData.env["Dev-CubeSignerStack"], sessionData.org_id, storage);
234
- this.cs = cs;
235
165
  this.#client = this.createClient(sessionData.token);
236
166
  }
237
167
  }