@cubist-labs/cubesigner-sdk 0.1.26 → 0.1.77

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 (44) hide show
  1. package/README.md +94 -33
  2. package/dist/src/ethers/index.d.ts +25 -5
  3. package/dist/src/ethers/index.js +58 -16
  4. package/dist/src/fido.d.ts +76 -0
  5. package/dist/src/fido.js +148 -0
  6. package/dist/src/index.d.ts +148 -35
  7. package/dist/src/index.js +320 -53
  8. package/dist/src/key.d.ts +64 -8
  9. package/dist/src/key.js +91 -19
  10. package/dist/src/org.d.ts +98 -9
  11. package/dist/src/org.js +144 -29
  12. package/dist/src/paginator.d.ts +76 -0
  13. package/dist/src/paginator.js +99 -0
  14. package/dist/src/role.d.ts +20 -8
  15. package/dist/src/role.js +7 -5
  16. package/dist/src/schema.d.ts +2395 -393
  17. package/dist/src/schema.js +1 -1
  18. package/dist/src/session/cognito_manager.d.ts +59 -0
  19. package/dist/src/session/cognito_manager.js +111 -0
  20. package/dist/src/session/session_manager.d.ts +15 -0
  21. package/dist/src/session/session_manager.js +21 -2
  22. package/dist/src/session/session_storage.js +1 -1
  23. package/dist/src/session/signer_session_manager.d.ts +24 -12
  24. package/dist/src/session/signer_session_manager.js +45 -20
  25. package/dist/src/signer_session.d.ts +136 -38
  26. package/dist/src/signer_session.js +187 -80
  27. package/dist/src/util.d.ts +20 -0
  28. package/dist/src/util.js +31 -2
  29. package/package.json +12 -7
  30. package/src/ethers/index.ts +88 -16
  31. package/src/fido.ts +166 -0
  32. package/src/index.ts +366 -77
  33. package/src/key.ts +112 -16
  34. package/src/org.ts +200 -37
  35. package/src/paginator.ts +122 -0
  36. package/src/role.ts +24 -11
  37. package/src/schema.ts +2458 -449
  38. package/src/session/{management_session_manager.ts → cognito_manager.ts} +13 -15
  39. package/src/session/session_manager.ts +25 -1
  40. package/src/session/session_storage.ts +1 -1
  41. package/src/session/signer_session_manager.ts +57 -27
  42. package/src/signer_session.ts +266 -89
  43. package/src/util.ts +41 -0
  44. package/src/session/oidc_session_manager.ts +0 -193
@@ -4,7 +4,7 @@ import { HasEnv, SessionManager } from "./session_manager";
4
4
  import { SessionStorage } from "./session_storage";
5
5
 
6
6
  /** JSON representation of our "management session" file format */
7
- export interface ManagementSessionObject {
7
+ export interface CognitoSessionObject {
8
8
  /** The email address of the user */
9
9
  email: string;
10
10
  /** The ID token */
@@ -17,13 +17,13 @@ export interface ManagementSessionObject {
17
17
  expiration: string;
18
18
  }
19
19
 
20
- export interface ManagementSessionInfo extends ManagementSessionObject, HasEnv {}
20
+ export interface CognitoSessionInfo extends CognitoSessionObject, HasEnv {}
21
21
 
22
- /** Type of storage required for management sessions */
23
- export type ManagementSessionStorage = SessionStorage<ManagementSessionInfo>;
22
+ /** Type of storage required for cognito (management) sessions */
23
+ export type CognitoSessionStorage = SessionStorage<CognitoSessionInfo>;
24
24
 
25
- /** The session manager for management sessions */
26
- export class ManagementSessionManager extends SessionManager<ManagementSessionInfo> {
25
+ /** The session manager for cognito (management) sessions */
26
+ export class CognitoSessionManager extends SessionManager<CognitoSessionInfo> {
27
27
  #client: Client;
28
28
 
29
29
  /**
@@ -98,7 +98,7 @@ export class ManagementSessionManager extends SessionManager<ManagementSessionIn
98
98
  const expiration = new Date(new Date().getTime() + expiresInMs).toISOString();
99
99
  const idToken = resp.AuthenticationResult.IdToken;
100
100
 
101
- await this.storage.save(<ManagementSessionInfo>{
101
+ await this.storage.save(<CognitoSessionInfo>{
102
102
  ...session,
103
103
  id_token: idToken,
104
104
  access_token: resp.AuthenticationResult.AccessToken,
@@ -108,15 +108,13 @@ export class ManagementSessionManager extends SessionManager<ManagementSessionIn
108
108
  }
109
109
 
110
110
  /**
111
- * Loads an existing management session from storage.
112
- * @param {ManagementSessionStorage} storage The storage back end to use
111
+ * Loads an existing cognito (management) session from storage.
112
+ * @param {CognitoSessionStorage} storage The storage back end to use
113
113
  * @return {Promise<SingerSession>} New token
114
114
  */
115
- static async loadFromStorage(
116
- storage: ManagementSessionStorage,
117
- ): Promise<ManagementSessionManager> {
115
+ static async loadFromStorage(storage: CognitoSessionStorage): Promise<CognitoSessionManager> {
118
116
  const sessionInfo = await storage.retrieve();
119
- return new ManagementSessionManager(
117
+ return new CognitoSessionManager(
120
118
  sessionInfo.env["Dev-CubeSignerStack"],
121
119
  sessionInfo.id_token,
122
120
  storage,
@@ -127,9 +125,9 @@ export class ManagementSessionManager extends SessionManager<ManagementSessionIn
127
125
  * Constructor.
128
126
  * @param {EnvInterface} env The environment of the session
129
127
  * @param {string} token The current token of the session
130
- * @param {ManagementSessionStorage} storage The storage back end to use
128
+ * @param {CognitoSessionStorage} storage The storage back end to use
131
129
  */
132
- private constructor(env: EnvInterface, token: string, storage: ManagementSessionStorage) {
130
+ private constructor(env: EnvInterface, token: string, storage: CognitoSessionStorage) {
133
131
  super(env, storage);
134
132
  this.#client = this.createClient(token);
135
133
  }
@@ -45,6 +45,27 @@ export abstract class SessionManager<U> {
45
45
  return false;
46
46
  }
47
47
 
48
+ /**
49
+ * Automatically refreshes the session in the background.
50
+ * The default implementation refreshes (if needed) every minute.
51
+ * Base implementations can, instead use the token expirations timestamps
52
+ * to refresh less often. This is a simple wrapper around `setInterval`.
53
+ * @return {number} The interval ID of the refresh timer.
54
+ */
55
+ autoRefresh(): RefreshId {
56
+ return setInterval(async () => {
57
+ await this.refreshIfNeeded();
58
+ }, 60 * 1000);
59
+ }
60
+
61
+ /**
62
+ * Clears the auto refresh timer.
63
+ * @param {number} timer The timer ID to clear.
64
+ */
65
+ clearAutoRefresh(timer: RefreshId): void {
66
+ clearInterval(timer);
67
+ }
68
+
48
69
  /**
49
70
  * Constructor.
50
71
  * @param {EnvInterface} env The environment of the session
@@ -77,7 +98,7 @@ export abstract class SessionManager<U> {
77
98
  * @return {boolean} True if the timestamp has expired
78
99
  */
79
100
  protected hasExpired(exp: number, buffer?: number): boolean {
80
- return exp < new Date().getTime() / 1000 + (buffer || DEFAULT_EXPIRATION_BUFFER_SECS);
101
+ return exp < new Date().getTime() + (buffer || DEFAULT_EXPIRATION_BUFFER_SECS) * 1000;
81
102
  }
82
103
 
83
104
  /**
@@ -112,3 +133,6 @@ export interface HasEnv {
112
133
  ["Dev-CubeSignerStack"]: EnvInterface;
113
134
  };
114
135
  }
136
+
137
+ /** Type of the refresh timer ID. */
138
+ export type RefreshId = ReturnType<typeof setInterval>;
@@ -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,10 +1,11 @@
1
- import { CubeSigner } from "..";
1
+ import { CubeSigner, EnvInterface } from "..";
2
2
  import { assertOk } from "../util";
3
3
  import { components, paths, Client } from "../client";
4
4
  import { HasEnv, OrgSessionManager } from "./session_manager";
5
- import { SessionStorage } from "./session_storage";
5
+ import { MemorySessionStorage, SessionStorage } from "./session_storage";
6
6
 
7
7
  export type ClientSessionInfo = components["schemas"]["ClientSessionInfo"];
8
+ export type NewSessionResponse = components["schemas"]["NewSessionResponse"];
8
9
 
9
10
  export type CreateSignerSessionRequest =
10
11
  paths["/v0/org/{org_id}/roles/{role_id}/tokens"]["post"]["requestBody"]["content"]["application/json"];
@@ -16,9 +17,9 @@ export interface SignerSessionObject {
16
17
  /** The organization ID */
17
18
  org_id: string;
18
19
  /** The role ID */
19
- role_id: string;
20
+ role_id?: string;
20
21
  /** The purpose of the session token */
21
- purpose: string;
22
+ purpose?: string;
22
23
  /** The token to include in Authorization header */
23
24
  token: string;
24
25
  /** Session info */
@@ -37,18 +38,20 @@ export interface SignerSessionLifetime {
37
38
  auth: number;
38
39
  /** Refresh token lifetime (in seconds). Defaults to one day (86400). */
39
40
  refresh?: number;
41
+ /** Grace lifetime (in seconds). Defaults to 30 seconds (30). */
42
+ grace?: number;
40
43
  }
41
44
 
42
45
  const defaultSignerSessionLifetime: SignerSessionLifetime = {
43
46
  session: 604800,
44
47
  auth: 300,
45
48
  refresh: 86400,
49
+ grace: 30,
46
50
  };
47
51
 
48
52
  /** Manager for signer sessions. */
49
53
  export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
50
54
  readonly cs?: CubeSigner;
51
- readonly roleId: string;
52
55
  #client: Client;
53
56
 
54
57
  /**
@@ -77,11 +80,10 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
77
80
  const session = await this.storage.retrieve();
78
81
  const resp = await (
79
82
  await this.cs.management()
80
- ).del("/v0/org/{org_id}/roles/{role_id}/tokens/{session_id}", {
83
+ ).del("/v0/org/{org_id}/session/{session_id}", {
81
84
  params: {
82
85
  path: {
83
86
  org_id: session.org_id,
84
- role_id: session.role_id,
85
87
  session_id: session.session_info.session_id,
86
88
  },
87
89
  },
@@ -97,7 +99,7 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
97
99
  */
98
100
  async isStale(): Promise<boolean> {
99
101
  const session = await this.storage.retrieve();
100
- return this.hasExpired(session.session_info.auth_token_exp);
102
+ return this.hasExpired(session.session_info.auth_token_exp * 1000);
101
103
  }
102
104
 
103
105
  /**
@@ -127,7 +129,7 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
127
129
  /**
128
130
  * Create a new signer session.
129
131
  * @param {CubeSigner} cs The CubeSigner instance
130
- * @param {SessionStorage<SignerSessionObject>} storage The session storage to use
132
+ * @param {SignerSessionStorage} storage The session storage to use
131
133
  * @param {string} orgId Org ID
132
134
  * @param {string} roleId Role ID
133
135
  * @param {string} purpose The purpose of the session
@@ -151,6 +153,7 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
151
153
  auth_lifetime: ttl?.auth || defaultSignerSessionLifetime.auth,
152
154
  refresh_lifetime: ttl?.refresh || defaultSignerSessionLifetime.refresh,
153
155
  session_lifetime: ttl?.session || defaultSignerSessionLifetime.session,
156
+ grace_lifetime: ttl?.grace || defaultSignerSessionLifetime.grace,
154
157
  },
155
158
  parseAs: "json",
156
159
  });
@@ -159,7 +162,7 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
159
162
  if (!session_info) {
160
163
  throw new Error("Signer session info missing");
161
164
  }
162
- await storage.save({
165
+ const sessionData = {
163
166
  org_id: orgId,
164
167
  role_id: roleId,
165
168
  purpose,
@@ -169,43 +172,70 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
169
172
  env: {
170
173
  ["Dev-CubeSignerStack"]: cs.env,
171
174
  },
172
- });
173
- return new SignerSessionManager(cs, orgId, roleId, data.token, storage);
175
+ };
176
+ await storage.save(sessionData);
177
+ return new SignerSessionManager(sessionData, storage, cs);
178
+ }
179
+
180
+ /**
181
+ * @param {EnvInterface} env The CubeSigner environment
182
+ * @param {string} orgId The organization ID
183
+ * @param {NewSessionResponse} session The session information.
184
+ * @param {SignerSessionStorage} storage The storage to use for saving the session.
185
+ * @return {Promise<SignerSessionManager>} New signer session manager.
186
+ */
187
+ static async createFromSessionInfo(
188
+ env: EnvInterface,
189
+ orgId: string,
190
+ session: NewSessionResponse,
191
+ storage?: SignerSessionStorage,
192
+ ): Promise<SignerSessionManager> {
193
+ const sessionData = {
194
+ env: {
195
+ ["Dev-CubeSignerStack"]: env,
196
+ },
197
+ org_id: orgId,
198
+ token: session.token,
199
+ purpose: "sign via oidc",
200
+ session_info: session.session_info,
201
+ };
202
+ storage ??= new MemorySessionStorage();
203
+ await storage.save(sessionData);
204
+ return await SignerSessionManager.loadFromStorage(storage);
174
205
  }
175
206
 
176
207
  /**
177
208
  * Uses an existing session to create a new signer session manager.
178
- * @param {CubeSigner} cs The CubeSigner instance
179
- * @param {SessionStorage<SignerSessionObject>} storage The session storage to use
209
+ * @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.
180
213
  * @return {Promise<SingerSession>} New signer session manager
181
214
  */
182
215
  static async loadFromStorage(
183
- cs: CubeSigner,
184
216
  storage: SignerSessionStorage,
217
+ cs?: CubeSigner,
185
218
  ): Promise<SignerSessionManager> {
186
219
  const session = await storage.retrieve();
187
- return new SignerSessionManager(cs, session.org_id, session.role_id, session.token, storage);
220
+ return new SignerSessionManager(session, storage, cs);
188
221
  }
189
222
 
190
223
  /**
191
224
  * Constructor.
192
- * @param {CubeSigner} cs CubeSigner
193
- * @param {string} orgId The id of the org associated with this session
194
- * @param {string} roleId The id of the role that this session assumes
195
- * @param {string} token The authorization token to use
225
+ * @param {SignerSessionData} sessionData Session data
196
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.
197
230
  * @internal
198
231
  */
199
232
  private constructor(
200
- cs: CubeSigner,
201
- orgId: string,
202
- roleId: string,
203
- token: string,
233
+ sessionData: SignerSessionData,
204
234
  storage: SignerSessionStorage,
235
+ cs?: CubeSigner,
205
236
  ) {
206
- super(cs.env, orgId, storage);
237
+ super(sessionData.env["Dev-CubeSignerStack"], sessionData.org_id, storage);
207
238
  this.cs = cs;
208
- this.roleId = roleId;
209
- this.#client = this.createClient(token);
239
+ this.#client = this.createClient(sessionData.token);
210
240
  }
211
241
  }