@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
@@ -1,193 +0,0 @@
1
- import { paths, Client } from "../client";
2
- import { EnvInterface } from "..";
3
- import { assertOk } from "../util";
4
- import { OrgSessionManager } from "./session_manager";
5
- import { SessionStorage } from "./session_storage";
6
- import createClient from "openapi-fetch";
7
-
8
- // An token obtained from an OIDC token is valid for 5 minutes
9
- const OIDC_TOKEN_EXP_SECS = 300;
10
-
11
- type OidcAuthResponse =
12
- paths["/v0/org/{org_id}/oidc"]["post"]["responses"]["200"]["content"]["application/json"];
13
-
14
- /** JSON representation of the OIDC token */
15
- export interface OidcSessionData {
16
- /** The environment that this token is for */
17
- env: EnvInterface;
18
- /** The organization ID */
19
- org_id: string;
20
- /** The OIDC token that this session was created from */
21
- oidc_token: string;
22
- /** The token to include in Authorization header */
23
- token: string;
24
- /** Token expiration timestamp */
25
- token_exp: number;
26
- /** The scopes of the token */
27
- scopes: Array<string>;
28
- }
29
-
30
- /** Type of storage required for OIDC sessions */
31
- export type OidcSessionStorage = SessionStorage<OidcSessionData>;
32
-
33
- /** Manager for OIDC sessions. */
34
- export class OidcSessionManager extends OrgSessionManager<OidcSessionData> {
35
- #client: Client;
36
-
37
- /**
38
- * @return {string} The current auth token.
39
- * @internal
40
- */
41
- async token(): Promise<string> {
42
- const session = await this.storage.retrieve();
43
- return session.token;
44
- }
45
-
46
- /**
47
- * Returns a client with the current session and refreshes the current
48
- * session. May **UPDATE/MUTATE** self.
49
- */
50
- async client(): Promise<Client> {
51
- await this.refreshIfNeeded();
52
- return this.#client;
53
- }
54
-
55
- /** Revokes the session. */
56
- async revoke(): Promise<void> {
57
- this.unsupported("revoke");
58
- }
59
-
60
- /**
61
- * Refreshes the session and **UPDATES/MUTATES** self.
62
- */
63
- async refresh(): Promise<void> {
64
- const session = await this.storage.retrieve();
65
- const [token, tokenExp] = await OidcSessionManager.#exchangeToken(
66
- session.env,
67
- session.oidc_token,
68
- session.org_id,
69
- session.scopes,
70
- );
71
- await this.storage.save(<OidcSessionData>{
72
- ...session,
73
- token: token,
74
- token_exp: tokenExp,
75
- });
76
- this.#client = this.createClient(token);
77
- }
78
-
79
- /**
80
- * Returns whether it's time to refresh this token.
81
- * @return {boolean} Whether it's time to refresh this token.
82
- * @internal
83
- */
84
- async isStale(): Promise<boolean> {
85
- const session = await this.storage.retrieve();
86
- return this.hasExpired(session.token_exp);
87
- }
88
-
89
- /**
90
- * Refreshes the session if it is about to expire.
91
- * @return {boolean} Whether the session token was refreshed.
92
- * @internal
93
- */
94
- async refreshIfNeeded(): Promise<boolean> {
95
- if (await this.isStale()) {
96
- await this.refresh();
97
- return true;
98
- }
99
- return false;
100
- }
101
-
102
- /**
103
- * Authenticate an OIDC user and create a new session for them.
104
- * @param {EnvInterface} env The environment of the session
105
- * @param {SessionStorage<SignerSessionObject>} storage The signer session storage
106
- * @param {string} oidcToken The OIDC token
107
- * @param {string} orgId The id of the organization that the user is in
108
- * @param {List<string>} scopes The scopes of the resulting session
109
- * @return {Promise<OidcSessionManager>} The signer session
110
- */
111
- static async create(
112
- env: EnvInterface,
113
- storage: SessionStorage<OidcSessionData>,
114
- oidcToken: string,
115
- orgId: string,
116
- scopes: Array<string>,
117
- ): Promise<OidcSessionManager> {
118
- const [token, tokenExp] = await OidcSessionManager.#exchangeToken(
119
- env,
120
- oidcToken,
121
- orgId,
122
- scopes,
123
- );
124
- await storage.save(<OidcSessionData>{
125
- env,
126
- org_id: orgId,
127
- oidc_token: oidcToken,
128
- token,
129
- token_exp: tokenExp,
130
- scopes,
131
- });
132
- return new OidcSessionManager(env, orgId, token, storage);
133
- }
134
-
135
- /**
136
- * Load from storage
137
- * @param {OidcSessionStorage} storage The storage to load from
138
- * @return {Promise<OidcSessionManager>} New OIDC session manager
139
- */
140
- static async loadFromStorage(storage: OidcSessionStorage): Promise<OidcSessionManager> {
141
- const info = await storage.retrieve();
142
- return new OidcSessionManager(info.env, info.org_id, info.token, storage);
143
- }
144
-
145
- /**
146
- * Constructor.
147
- * @param {EnvInterface} env The environment of the session
148
- * @param {string} orgId The id of the org associated with this session
149
- * @param {string} token The authorization token to use
150
- * @param {SessionStorage<U>} storage The storage back end to use for storing
151
- * session information
152
- */
153
- private constructor(
154
- env: EnvInterface,
155
- orgId: string,
156
- token: string,
157
- storage: SessionStorage<OidcSessionData>,
158
- ) {
159
- super(env, orgId, storage);
160
- this.#client = this.createClient(token);
161
- }
162
-
163
- /**
164
- * Exchange an OIDC token for a CubeSigner session token.
165
- * @param {EnvInterface} env The CubeSigner environment
166
- * @param {string} oidcToken The OIDC token
167
- * @param {string} orgId The id of the organization that the user is in
168
- * @param {List<string>} scopes The scopes of the resulting session
169
- * @return {Promise<[string, number]>} The session token and its expiration time
170
- */
171
- static async #exchangeToken(
172
- env: EnvInterface,
173
- oidcToken: string,
174
- orgId: string,
175
- scopes: Array<string>,
176
- ): Promise<[string, number]> {
177
- const client = createClient<paths>({
178
- baseUrl: env.SignerApiRoot,
179
- headers: {
180
- Authorization: oidcToken,
181
- },
182
- });
183
- const resp = await client.post("/v0/org/{org_id}/oidc", {
184
- params: { path: { org_id: orgId } },
185
- body: {
186
- scopes,
187
- },
188
- parseAs: "json",
189
- });
190
- const data = assertOk(resp) as OidcAuthResponse;
191
- return [data.token, new Date().getTime() / 1000 + OIDC_TOKEN_EXP_SECS];
192
- }
193
- }