@cubist-labs/cubesigner-sdk 0.1.77 → 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.
- package/dist/src/client.d.ts +434 -7
- package/dist/src/client.js +1022 -18
- package/dist/src/ethers/index.d.ts +2 -4
- package/dist/src/ethers/index.js +5 -8
- package/dist/src/index.d.ts +41 -65
- package/dist/src/index.js +58 -231
- package/dist/src/key.d.ts +9 -45
- package/dist/src/key.js +18 -89
- package/dist/src/mfa.d.ts +85 -14
- package/dist/src/mfa.js +158 -40
- package/dist/src/org.d.ts +217 -127
- package/dist/src/org.js +94 -232
- package/dist/src/role.d.ts +76 -74
- package/dist/src/role.js +79 -136
- package/dist/src/schema.d.ts +345 -13
- package/dist/src/schema.js +1 -1
- package/dist/src/schema_types.d.ts +103 -0
- package/dist/src/schema_types.js +3 -0
- package/dist/src/session/session_manager.js +1 -1
- package/dist/src/session/signer_session_manager.d.ts +14 -29
- package/dist/src/session/signer_session_manager.js +26 -79
- package/dist/src/signer_session.d.ts +212 -191
- package/dist/src/signer_session.js +120 -313
- package/package.json +6 -6
- package/src/client.ts +1217 -7
- package/src/ethers/index.ts +5 -17
- package/src/index.ts +69 -262
- package/src/key.ts +19 -121
- package/src/{fido.ts → mfa.ts} +62 -26
- package/src/org.ts +96 -292
- package/src/role.ts +108 -181
- package/src/schema.ts +345 -13
- package/src/schema_types.ts +103 -0
- package/src/session/session_manager.ts +1 -1
- package/src/session/signer_session_manager.ts +35 -109
- package/src/signer_session.ts +115 -442
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
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 {
|
|
@@ -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,29 +53,27 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
|
|
|
64
53
|
}
|
|
65
54
|
|
|
66
55
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
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();
|
|
72
62
|
return this.#client;
|
|
73
63
|
}
|
|
74
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
|
+
|
|
75
72
|
/** Revokes the session. */
|
|
76
73
|
async revoke(): Promise<void> {
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
},
|
|
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 } },
|
|
90
77
|
parseAs: "json",
|
|
91
78
|
});
|
|
92
79
|
assertOk(resp);
|
|
@@ -106,10 +93,11 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
|
|
|
106
93
|
* Refreshes the session and **UPDATES/MUTATES** self.
|
|
107
94
|
*/
|
|
108
95
|
async refresh(): Promise<void> {
|
|
109
|
-
const
|
|
110
|
-
|
|
96
|
+
const currSession = await this.storage.retrieve();
|
|
97
|
+
|
|
98
|
+
const csi = currSession.session_info;
|
|
111
99
|
const resp = await this.#client.patch("/v1/org/{org_id}/token/refresh", {
|
|
112
|
-
params: { path: { org_id:
|
|
100
|
+
params: { path: { org_id: this.orgId } },
|
|
113
101
|
body: <RefreshSignerSessionRequest>{
|
|
114
102
|
epoch_num: csi.epoch,
|
|
115
103
|
epoch_token: csi.epoch_token,
|
|
@@ -118,63 +106,14 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
|
|
|
118
106
|
parseAs: "json",
|
|
119
107
|
});
|
|
120
108
|
const data = assertOk(resp);
|
|
121
|
-
|
|
122
|
-
...
|
|
109
|
+
const newSession = <SignerSessionData>{
|
|
110
|
+
...currSession,
|
|
123
111
|
session_info: data.session_info,
|
|
124
112
|
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
113
|
};
|
|
176
|
-
|
|
177
|
-
|
|
114
|
+
|
|
115
|
+
await this.storage.save(newSession);
|
|
116
|
+
this.#client = this.createClient(newSession.token);
|
|
178
117
|
}
|
|
179
118
|
|
|
180
119
|
/**
|
|
@@ -206,36 +145,23 @@ export class SignerSessionManager extends OrgSessionManager<SignerSessionData> {
|
|
|
206
145
|
|
|
207
146
|
/**
|
|
208
147
|
* Uses an existing session to create a new signer session manager.
|
|
148
|
+
*
|
|
209
149
|
* @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
150
|
* @return {Promise<SingerSession>} New signer session manager
|
|
214
151
|
*/
|
|
215
|
-
static async loadFromStorage(
|
|
216
|
-
storage: SignerSessionStorage,
|
|
217
|
-
cs?: CubeSigner,
|
|
218
|
-
): Promise<SignerSessionManager> {
|
|
152
|
+
static async loadFromStorage(storage: SignerSessionStorage): Promise<SignerSessionManager> {
|
|
219
153
|
const session = await storage.retrieve();
|
|
220
|
-
return new SignerSessionManager(session, storage
|
|
154
|
+
return new SignerSessionManager(session, storage);
|
|
221
155
|
}
|
|
222
156
|
|
|
223
157
|
/**
|
|
224
158
|
* Constructor.
|
|
159
|
+
*
|
|
225
160
|
* @param {SignerSessionData} sessionData Session data
|
|
226
161
|
* @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
|
|
231
162
|
*/
|
|
232
|
-
|
|
233
|
-
sessionData: SignerSessionData,
|
|
234
|
-
storage: SignerSessionStorage,
|
|
235
|
-
cs?: CubeSigner,
|
|
236
|
-
) {
|
|
163
|
+
constructor(sessionData: SignerSessionData, storage: SignerSessionStorage) {
|
|
237
164
|
super(sessionData.env["Dev-CubeSignerStack"], sessionData.org_id, storage);
|
|
238
|
-
this.cs = cs;
|
|
239
165
|
this.#client = this.createClient(sessionData.token);
|
|
240
166
|
}
|
|
241
167
|
}
|