@cubist-labs/cubesigner-sdk 0.1.23
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/LICENSE-APACHE +177 -0
- package/LICENSE-MIT +25 -0
- package/NOTICE +13 -0
- package/README.md +470 -0
- package/dist/examples/ethers.d.ts +1 -0
- package/dist/examples/ethers.js +142 -0
- package/dist/spec/env/beta.json +9 -0
- package/dist/spec/env/gamma.json +9 -0
- package/dist/spec/env/prod.json +9 -0
- package/dist/src/client.d.ts +10 -0
- package/dist/src/client.js +21 -0
- package/dist/src/env.d.ts +15 -0
- package/dist/src/env.js +35 -0
- package/dist/src/ethers/index.d.ts +50 -0
- package/dist/src/ethers/index.js +122 -0
- package/dist/src/index.d.ts +114 -0
- package/dist/src/index.js +205 -0
- package/dist/src/key.d.ts +114 -0
- package/dist/src/key.js +201 -0
- package/dist/src/mfa.d.ts +23 -0
- package/dist/src/mfa.js +63 -0
- package/dist/src/org.d.ts +161 -0
- package/dist/src/org.js +264 -0
- package/dist/src/role.d.ts +224 -0
- package/dist/src/role.js +256 -0
- package/dist/src/schema.d.ts +3049 -0
- package/dist/src/schema.js +7 -0
- package/dist/src/session/generic.d.ts +47 -0
- package/dist/src/session/generic.js +3 -0
- package/dist/src/session/management_session_manager.d.ts +59 -0
- package/dist/src/session/management_session_manager.js +111 -0
- package/dist/src/session/oidc_session_manager.d.ts +78 -0
- package/dist/src/session/oidc_session_manager.js +142 -0
- package/dist/src/session/session_manager.d.ts +74 -0
- package/dist/src/session/session_manager.js +79 -0
- package/dist/src/session/session_storage.d.ts +47 -0
- package/dist/src/session/session_storage.js +76 -0
- package/dist/src/session/signer_session_manager.d.ts +88 -0
- package/dist/src/session/signer_session_manager.js +159 -0
- package/dist/src/sign.d.ts +114 -0
- package/dist/src/sign.js +248 -0
- package/dist/src/signer_session.d.ts +180 -0
- package/dist/src/signer_session.js +369 -0
- package/dist/src/util.d.ts +35 -0
- package/dist/src/util.js +75 -0
- package/dist/test/sessions.d.ts +35 -0
- package/dist/test/sessions.js +56 -0
- package/package.json +61 -0
- package/src/client.ts +12 -0
- package/src/env.ts +25 -0
- package/src/ethers/index.ts +131 -0
- package/src/index.ts +220 -0
- package/src/key.ts +249 -0
- package/src/org.ts +333 -0
- package/src/role.ts +385 -0
- package/src/schema.ts +3054 -0
- package/src/session/management_session_manager.ts +136 -0
- package/src/session/oidc_session_manager.ts +193 -0
- package/src/session/session_manager.ts +114 -0
- package/src/session/session_storage.ts +73 -0
- package/src/session/signer_session_manager.ts +211 -0
- package/src/signer_session.ts +464 -0
- package/src/util.ts +58 -0
- package/tsconfig.json +32 -0
package/src/key.ts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { CubeSigner, KeyPolicy } from ".";
|
|
2
|
+
import { components } from "./client";
|
|
3
|
+
import { assertOk } from "./util";
|
|
4
|
+
|
|
5
|
+
/** Secp256k1 key type */
|
|
6
|
+
export enum Secp256k1 {
|
|
7
|
+
Evm = "SecpEthAddr", // eslint-disable-line no-unused-vars
|
|
8
|
+
Btc = "SecpBtc", // eslint-disable-line no-unused-vars
|
|
9
|
+
BtcTest = "SecpBtcTest", // eslint-disable-line no-unused-vars
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** BLS key type */
|
|
13
|
+
export enum BLS {
|
|
14
|
+
Eth2Deposited = "BlsPub", // eslint-disable-line no-unused-vars
|
|
15
|
+
Eth2Inactive = "BlsInactive", // eslint-disable-line no-unused-vars
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Ed25519 key type */
|
|
19
|
+
export enum Ed25519 {
|
|
20
|
+
Solana = "Ed25519SolanaAddr", // eslint-disable-line no-unused-vars
|
|
21
|
+
Sui = "Ed25519SuiAddr", // eslint-disable-line no-unused-vars
|
|
22
|
+
Aptos = "Ed25519AptosAddr", // eslint-disable-line no-unused-vars
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Key type */
|
|
26
|
+
export type KeyType = Secp256k1 | BLS | Ed25519;
|
|
27
|
+
|
|
28
|
+
/** Schema key type (i.e., key type at the API level) */
|
|
29
|
+
type SchemaKeyType = components["schemas"]["KeyType"];
|
|
30
|
+
|
|
31
|
+
type UpdateKeyRequest = components["schemas"]["UpdateKeyRequest"];
|
|
32
|
+
type KeyInfo = components["schemas"]["KeyInfo"];
|
|
33
|
+
|
|
34
|
+
/** Signing keys. */
|
|
35
|
+
export class Key {
|
|
36
|
+
/** The CubeSigner instance that this key is associated with */
|
|
37
|
+
readonly #cs: CubeSigner;
|
|
38
|
+
/** The organization that this key is in */
|
|
39
|
+
readonly orgId: string;
|
|
40
|
+
/**
|
|
41
|
+
* The id of the key: "Key#" followed by a unique identifier specific to
|
|
42
|
+
* the type of key (such as a public key for BLS or an ethereum address for Secp)
|
|
43
|
+
* @example Key#0x8e3484687e66cdd26cf04c3647633ab4f3570148
|
|
44
|
+
* */
|
|
45
|
+
readonly id: string;
|
|
46
|
+
|
|
47
|
+
/** The type of key. */
|
|
48
|
+
readonly type: KeyType;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A unique identifier specific to the type of key, such as a public key or an ethereum address
|
|
52
|
+
* @example 0x8e3484687e66cdd26cf04c3647633ab4f3570148
|
|
53
|
+
* */
|
|
54
|
+
readonly materialId: string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @description Hex-encoded, serialized public key. The format used depends on the key type:
|
|
58
|
+
* - secp256k1 keys use 65-byte uncompressed SECG format
|
|
59
|
+
* - BLS keys use 48-byte compressed BLS12-381 (ZCash) format
|
|
60
|
+
* @example 0x04d2688b6bc2ce7f9879b9e745f3c4dc177908c5cef0c1b64cff19ae7ff27dee623c64fe9d9c325c7fbbc748bbd5f607ce14dd83e28ebbbb7d3e7f2ffb70a79431
|
|
61
|
+
* */
|
|
62
|
+
readonly publicKey: string;
|
|
63
|
+
|
|
64
|
+
/** Is the key enabled? */
|
|
65
|
+
async enabled(): Promise<boolean> {
|
|
66
|
+
const data = await this.fetch();
|
|
67
|
+
return data.enabled;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Enable the key. */
|
|
71
|
+
async enable() {
|
|
72
|
+
await this.update({ enabled: true });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Disable the key. */
|
|
76
|
+
async disable() {
|
|
77
|
+
await this.update({ enabled: false });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Set new policy (overwriting any policies previously set for this key)
|
|
82
|
+
* @param {KeyPolicy} policy The new policy to set
|
|
83
|
+
*/
|
|
84
|
+
async setPolicy(policy: KeyPolicy) {
|
|
85
|
+
await this.update({ policy: policy as unknown as Record<string, never>[] });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Append to existing key policy. This append is not atomic -- it uses {@link policy} to fetch the current policy and then {@link setPolicy} to set the policy -- and should not be used in across concurrent sessions.
|
|
90
|
+
* @param {KeyPolicy} policy The policy to append to the existing one.
|
|
91
|
+
*/
|
|
92
|
+
async appendPolicy(policy: KeyPolicy) {
|
|
93
|
+
const existing = await this.policy();
|
|
94
|
+
await this.setPolicy([...existing, ...policy]);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Get the policy for the org.
|
|
99
|
+
* @return {Promise<KeyPolicy>} The policy for the org.
|
|
100
|
+
*/
|
|
101
|
+
async policy(): Promise<KeyPolicy> {
|
|
102
|
+
const data = await this.fetch();
|
|
103
|
+
return (data.policy ?? []) as unknown as KeyPolicy;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @description Owner of the key
|
|
108
|
+
* @example User#c3b9379c-4e8c-4216-bd0a-65ace53cf98f
|
|
109
|
+
* */
|
|
110
|
+
async owner(): Promise<string> {
|
|
111
|
+
const data = await this.fetch();
|
|
112
|
+
return data.owner;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Set the owner of the key. Only the key (or org) owner can change the owner of the key.
|
|
116
|
+
* @param {string} owner The user-id of the new owner of the key.
|
|
117
|
+
* */
|
|
118
|
+
async setOwner(owner: string) {
|
|
119
|
+
await this.update({ owner });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// --------------------------------------------------------------------------
|
|
123
|
+
// -- INTERNAL --------------------------------------------------------------
|
|
124
|
+
// --------------------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
/** Create a new key.
|
|
127
|
+
* @param {CubeSigner} cs The CubeSigner instance to use for signing.
|
|
128
|
+
* @param {string} orgId The id of the organization to which the key belongs.
|
|
129
|
+
* @param {KeyInfo} data The JSON response from the API server.
|
|
130
|
+
* @internal
|
|
131
|
+
* */
|
|
132
|
+
constructor(cs: CubeSigner, orgId: string, data: KeyInfo) {
|
|
133
|
+
this.#cs = cs;
|
|
134
|
+
this.orgId = orgId;
|
|
135
|
+
this.id = data.key_id;
|
|
136
|
+
this.type = fromSchemaKeyType(data.key_type);
|
|
137
|
+
this.materialId = data.material_id;
|
|
138
|
+
this.publicKey = data.public_key;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Update the key.
|
|
142
|
+
* @param {UpdateKeyRequest} request The JSON request to send to the API server.
|
|
143
|
+
* @return {KeyInfo} The JSON response from the API server.
|
|
144
|
+
* */
|
|
145
|
+
private async update(request: UpdateKeyRequest): Promise<KeyInfo> {
|
|
146
|
+
const resp = await (
|
|
147
|
+
await this.#cs.management()
|
|
148
|
+
).patch("/v0/org/{org_id}/keys/{key_id}", {
|
|
149
|
+
params: { path: { org_id: this.orgId, key_id: this.id } },
|
|
150
|
+
body: request,
|
|
151
|
+
parseAs: "json",
|
|
152
|
+
});
|
|
153
|
+
return assertOk(resp);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Create new signing keys.
|
|
157
|
+
* @param {CubeSigner} cs The CubeSigner instance to use for signing.
|
|
158
|
+
* @param {string} orgId The id of the organization to which the key belongs.
|
|
159
|
+
* @param {KeyType} keyType The type of key to create.
|
|
160
|
+
* @param {number} count The number of keys to create.
|
|
161
|
+
* @param {string?} ownerId The owner of the keys. Defaults to the session's user.
|
|
162
|
+
* @return {Key[]} The new keys.
|
|
163
|
+
* @internal
|
|
164
|
+
* */
|
|
165
|
+
static async createKeys(
|
|
166
|
+
cs: CubeSigner,
|
|
167
|
+
orgId: string,
|
|
168
|
+
keyType: KeyType,
|
|
169
|
+
count: number,
|
|
170
|
+
ownerId?: string,
|
|
171
|
+
): Promise<Key[]> {
|
|
172
|
+
const chain_id = 0; // not used anymore
|
|
173
|
+
const resp = await (
|
|
174
|
+
await cs.management()
|
|
175
|
+
).post("/v0/org/{org_id}/keys", {
|
|
176
|
+
params: { path: { org_id: orgId } },
|
|
177
|
+
body: {
|
|
178
|
+
count,
|
|
179
|
+
chain_id,
|
|
180
|
+
key_type: keyType,
|
|
181
|
+
owner: ownerId || null,
|
|
182
|
+
},
|
|
183
|
+
parseAs: "json",
|
|
184
|
+
});
|
|
185
|
+
const data = assertOk(resp);
|
|
186
|
+
return data.keys.map((k: KeyInfo) => new Key(cs, orgId, k));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Get a key by id.
|
|
190
|
+
* @param {CubeSigner} cs The CubeSigner instance to use for signing.
|
|
191
|
+
* @param {string} orgId The id of the organization to which the key belongs.
|
|
192
|
+
* @param {string} keyId The id of the key to get.
|
|
193
|
+
* @return {Key} The key.
|
|
194
|
+
* @internal
|
|
195
|
+
* */
|
|
196
|
+
static async getKey(cs: CubeSigner, orgId: string, keyId: string): Promise<Key> {
|
|
197
|
+
const resp = await (
|
|
198
|
+
await cs.management()
|
|
199
|
+
).get("/v0/org/{org_id}/keys/{key_id}", {
|
|
200
|
+
params: { path: { org_id: orgId, key_id: keyId } },
|
|
201
|
+
parseAs: "json",
|
|
202
|
+
});
|
|
203
|
+
const data = assertOk(resp);
|
|
204
|
+
return new Key(cs, orgId, data);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Fetches the key information.
|
|
208
|
+
* @return {KeyInfo} The key information.
|
|
209
|
+
* @internal
|
|
210
|
+
* */
|
|
211
|
+
private async fetch(): Promise<KeyInfo> {
|
|
212
|
+
const resp = await (
|
|
213
|
+
await this.#cs.management()
|
|
214
|
+
).get("/v0/org/{org_id}/keys/{key_id}", {
|
|
215
|
+
params: { path: { org_id: this.orgId, key_id: this.id } },
|
|
216
|
+
parseAs: "json",
|
|
217
|
+
});
|
|
218
|
+
const data = assertOk(resp);
|
|
219
|
+
return data;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Convert a schema key type to a key type.
|
|
224
|
+
* @param {SchemaKeyType} ty The schema key type.
|
|
225
|
+
* @return {KeyType} The key type.
|
|
226
|
+
* @internal
|
|
227
|
+
* */
|
|
228
|
+
function fromSchemaKeyType(ty: SchemaKeyType): KeyType {
|
|
229
|
+
switch (ty) {
|
|
230
|
+
case "SecpEthAddr":
|
|
231
|
+
return Secp256k1.Evm;
|
|
232
|
+
case "SecpBtc":
|
|
233
|
+
return Secp256k1.Btc;
|
|
234
|
+
case "SecpBtcTest":
|
|
235
|
+
return Secp256k1.BtcTest;
|
|
236
|
+
case "BlsPub":
|
|
237
|
+
return BLS.Eth2Deposited;
|
|
238
|
+
case "BlsInactive":
|
|
239
|
+
return BLS.Eth2Inactive;
|
|
240
|
+
case "Ed25519SolanaAddr":
|
|
241
|
+
return Ed25519.Solana;
|
|
242
|
+
case "Ed25519SuiAddr":
|
|
243
|
+
return Ed25519.Sui;
|
|
244
|
+
case "Ed25519AptosAddr":
|
|
245
|
+
return Ed25519.Aptos;
|
|
246
|
+
default:
|
|
247
|
+
throw new Error(`Unknown key type: ${ty}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
package/src/org.ts
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { CubeSigner, KeyInfo, MfaRequestInfo } from ".";
|
|
2
|
+
import { components, paths } from "./client";
|
|
3
|
+
import { assertOk } from "./util";
|
|
4
|
+
import { KeyType, Key } from "./key";
|
|
5
|
+
import { Role, RoleInfo } from "./role";
|
|
6
|
+
|
|
7
|
+
/** Organization id */
|
|
8
|
+
export type OrgId = string;
|
|
9
|
+
|
|
10
|
+
/** Org-wide policy */
|
|
11
|
+
export type OrgPolicy = SourceIpAllowlistPolicy | OriginAllowlistPolicy | MaxDailyUnstakePolicy;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Only allow requests from the specified origins.
|
|
15
|
+
* @example {"OriginAllowlist": "*"}
|
|
16
|
+
*/
|
|
17
|
+
export interface OriginAllowlistPolicy {
|
|
18
|
+
OriginAllowlist: string[] | "*";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Restrict signing to specific source IP addresses.
|
|
23
|
+
* @example {"SourceIpAllowlist": ["10.1.2.3/8", "169.254.17.1/16"]}
|
|
24
|
+
*/
|
|
25
|
+
export interface SourceIpAllowlistPolicy {
|
|
26
|
+
SourceIpAllowlist: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Restrict the number of unstakes per day.
|
|
31
|
+
* @example {"MaxDailyUnstake": 5 }
|
|
32
|
+
*/
|
|
33
|
+
export interface MaxDailyUnstakePolicy {
|
|
34
|
+
MaxDailyUnstake: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type OrgInfo = components["schemas"]["OrgInfo"];
|
|
38
|
+
type UserIdInfo = components["schemas"]["UserIdInfo"];
|
|
39
|
+
type UpdateOrgRequest =
|
|
40
|
+
paths["/v0/org/{org_id}"]["patch"]["requestBody"]["content"]["application/json"];
|
|
41
|
+
type UpdateOrgResponse =
|
|
42
|
+
paths["/v0/org/{org_id}"]["patch"]["responses"]["200"]["content"]["application/json"];
|
|
43
|
+
|
|
44
|
+
export type OidcIdentity = components["schemas"]["OIDCIdentity"];
|
|
45
|
+
export type MemberRole = components["schemas"]["MemberRole"];
|
|
46
|
+
|
|
47
|
+
/** An organization. */
|
|
48
|
+
export class Org {
|
|
49
|
+
readonly #cs: CubeSigner;
|
|
50
|
+
/**
|
|
51
|
+
* The ID of the organization.
|
|
52
|
+
* @example Org#124dfe3e-3bbd-487d-80c0-53c55e8ab87a
|
|
53
|
+
*/
|
|
54
|
+
readonly #id: string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @description The org id
|
|
58
|
+
* @example Org#c3b9379c-4e8c-4216-bd0a-65ace53cf98f
|
|
59
|
+
* */
|
|
60
|
+
get id(): OrgId {
|
|
61
|
+
return this.#id;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Human-readable name for the org */
|
|
65
|
+
async name(): Promise<string | undefined> {
|
|
66
|
+
const data = await this.fetch();
|
|
67
|
+
return data.name ?? undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Set the human-readable name for the org.
|
|
71
|
+
* @param {string} name The new human-readable name for the org (must be alphanumeric).
|
|
72
|
+
* @example my_org_name
|
|
73
|
+
* */
|
|
74
|
+
async setName(name: string) {
|
|
75
|
+
if (!/^[a-zA-Z0-9_]{3,30}$/.test(name)) {
|
|
76
|
+
throw new Error("Org name must be alphanumeric and between 3 and 30 characters");
|
|
77
|
+
}
|
|
78
|
+
await this.update({ name });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Is the org enabled? */
|
|
82
|
+
async enabled(): Promise<boolean> {
|
|
83
|
+
const data = await this.fetch();
|
|
84
|
+
return data.enabled;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Enable the org. */
|
|
88
|
+
async enable() {
|
|
89
|
+
await this.update({ enabled: true });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Disable the org. */
|
|
93
|
+
async disable() {
|
|
94
|
+
await this.update({ enabled: false });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Get the policy for the org. */
|
|
98
|
+
async policy(): Promise<OrgPolicy[]> {
|
|
99
|
+
const data = await this.fetch();
|
|
100
|
+
return (data.policy ?? []) as unknown as OrgPolicy[];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Set the policy for the org.
|
|
104
|
+
* @param {OrgPolicy[]} policy The new policy for the org.
|
|
105
|
+
* */
|
|
106
|
+
async setPolicy(policy: OrgPolicy[]) {
|
|
107
|
+
const p = policy as unknown as Record<string, never>[];
|
|
108
|
+
await this.update({ policy: p });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Create a new signing key.
|
|
112
|
+
* @param {KeyType} type The type of key to create.
|
|
113
|
+
* @param {string?} ownerId The owner of the key. Defaults to the session's user.
|
|
114
|
+
* @return {Key[]} The new keys.
|
|
115
|
+
* */
|
|
116
|
+
async createKey(type: KeyType, ownerId?: string): Promise<Key> {
|
|
117
|
+
return (await Key.createKeys(this.#cs, this.id, type, 1, ownerId))[0];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Create new signing keys.
|
|
121
|
+
* @param {KeyType} type The type of key to create.
|
|
122
|
+
* @param {nummber} count The number of keys to create.
|
|
123
|
+
* @param {string?} ownerId The owner of the keys. Defaults to the session's user.
|
|
124
|
+
* @return {Key[]} The new keys.
|
|
125
|
+
* */
|
|
126
|
+
async createKeys(type: KeyType, count: number, ownerId?: string): Promise<Key[]> {
|
|
127
|
+
return Key.createKeys(this.#cs, this.id, type, count, ownerId);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Create a new user in the organization and sends an invitation to that user
|
|
132
|
+
* @param {string} email Email of the user
|
|
133
|
+
* @param {string} name The full name of the user
|
|
134
|
+
*/
|
|
135
|
+
async createUser(email: string, name: string): Promise<void> {
|
|
136
|
+
const resp = await (
|
|
137
|
+
await this.#cs.management()
|
|
138
|
+
).post("/v0/org/{org_id}/invite", {
|
|
139
|
+
params: { path: { org_id: this.id } },
|
|
140
|
+
body: {
|
|
141
|
+
email,
|
|
142
|
+
name,
|
|
143
|
+
skip_email: false,
|
|
144
|
+
},
|
|
145
|
+
parseAs: "json",
|
|
146
|
+
});
|
|
147
|
+
assertOk(resp);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Create a new OIDC user
|
|
152
|
+
* @param {OidcIdentity} identity The identity of the OIDC user
|
|
153
|
+
* @param {MemberRole} memberRole The type of membership of the new user
|
|
154
|
+
* @return {string} User id of the new user
|
|
155
|
+
*/
|
|
156
|
+
async createOidcUser(identity: OidcIdentity, memberRole: MemberRole): Promise<string> {
|
|
157
|
+
const resp = await (
|
|
158
|
+
await this.#cs.management()
|
|
159
|
+
).post("/v0/org/{org_id}/users", {
|
|
160
|
+
params: { path: { org_id: this.id } },
|
|
161
|
+
body: {
|
|
162
|
+
identity,
|
|
163
|
+
role: memberRole,
|
|
164
|
+
},
|
|
165
|
+
parseAs: "json",
|
|
166
|
+
});
|
|
167
|
+
return assertOk(resp).user_id;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* List users in the organization
|
|
172
|
+
* @return {UserIdInfo[]} List of users
|
|
173
|
+
*/
|
|
174
|
+
async users(): Promise<UserIdInfo[]> {
|
|
175
|
+
const resp = await (
|
|
176
|
+
await this.#cs.management()
|
|
177
|
+
).get("/v0/org/{org_id}/users", {
|
|
178
|
+
params: { path: { org_id: this.id } },
|
|
179
|
+
parseAs: "json",
|
|
180
|
+
});
|
|
181
|
+
return assertOk(resp).users;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Get a key by id.
|
|
185
|
+
* @param {string} keyId The id of the key to get.
|
|
186
|
+
* @return {Key} The key.
|
|
187
|
+
* */
|
|
188
|
+
async getKey(keyId: string): Promise<Key> {
|
|
189
|
+
return await Key.getKey(this.#cs, this.id, keyId);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Get all keys in the org.
|
|
193
|
+
* @param {KeyType?} type Optional key type to filter list for.
|
|
194
|
+
* @return {Key} The key.
|
|
195
|
+
* */
|
|
196
|
+
async keys(type?: KeyType): Promise<Key[]> {
|
|
197
|
+
const resp = await (
|
|
198
|
+
await this.#cs.management()
|
|
199
|
+
).get("/v0/org/{org_id}/keys", {
|
|
200
|
+
params: {
|
|
201
|
+
path: { org_id: this.id },
|
|
202
|
+
query: type ? { key_type: type } : undefined,
|
|
203
|
+
},
|
|
204
|
+
parseAs: "json",
|
|
205
|
+
});
|
|
206
|
+
const data = assertOk(resp);
|
|
207
|
+
return data.keys.map((k: KeyInfo) => new Key(this.#cs, this.id, k));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Create a new role.
|
|
211
|
+
* @param {string?} name The name of the role.
|
|
212
|
+
* @return {Role} The new role.
|
|
213
|
+
* */
|
|
214
|
+
async createRole(name?: string): Promise<Role> {
|
|
215
|
+
return Role.createRole(this.#cs, this.id, name);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Get a role by id or name.
|
|
219
|
+
* @param {string} roleId The id or name of the role to get.
|
|
220
|
+
* @return {Role} The role.
|
|
221
|
+
* */
|
|
222
|
+
async getRole(roleId: string): Promise<Role> {
|
|
223
|
+
return Role.getRole(this.#cs, this.id, roleId);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** List all roles in the org..
|
|
227
|
+
* @return {Role[]} The roles.
|
|
228
|
+
* */
|
|
229
|
+
async list(): Promise<Role[]> {
|
|
230
|
+
return Org.roles(this.#cs, this.id);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Get a pending MFA request by its id.
|
|
235
|
+
* @param {string} mfaId The id of the MFA request.
|
|
236
|
+
* @return {Promise<MfaRequestInfo>} The MFA request.
|
|
237
|
+
*/
|
|
238
|
+
async mfaGet(mfaId: string): Promise<MfaRequestInfo> {
|
|
239
|
+
const resp = await (
|
|
240
|
+
await this.#cs.management()
|
|
241
|
+
).get("/v0/org/{org_id}/mfa/{mfa_id}", {
|
|
242
|
+
params: { path: { org_id: this.#id, mfa_id: mfaId } },
|
|
243
|
+
});
|
|
244
|
+
return assertOk(resp);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Approve a pending MFA request.
|
|
249
|
+
*
|
|
250
|
+
* @param {string} mfaId The id of the MFA request.
|
|
251
|
+
* @return {Promise<MfaRequestInfo>} The MFA request.
|
|
252
|
+
*/
|
|
253
|
+
async mfaApprove(mfaId: string): Promise<MfaRequestInfo> {
|
|
254
|
+
return Org.mfaApprove(this.#cs, this.#id, mfaId);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// --------------------------------------------------------------------------
|
|
258
|
+
// -- INTERNAL --------------------------------------------------------------
|
|
259
|
+
// --------------------------------------------------------------------------
|
|
260
|
+
|
|
261
|
+
/** Create a new org.
|
|
262
|
+
* @param {CubeSigner} cs The CubeSigner instance.
|
|
263
|
+
* @param {OrgInfo} data The JSON response from the API server.
|
|
264
|
+
* @internal
|
|
265
|
+
* */
|
|
266
|
+
constructor(cs: CubeSigner, data: OrgInfo) {
|
|
267
|
+
this.#cs = cs;
|
|
268
|
+
this.#id = data.org_id;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Approve a pending MFA request.
|
|
273
|
+
*
|
|
274
|
+
* @param {CubeSigner} cs The CubeSigner instance to use for requests
|
|
275
|
+
* @param {string} orgId The org id of the MFA request
|
|
276
|
+
* @param {string} mfaId The id of the MFA request
|
|
277
|
+
* @return {Promise<MfaRequestInfo>} The result of the MFA request
|
|
278
|
+
*/
|
|
279
|
+
static async mfaApprove(cs: CubeSigner, orgId: string, mfaId: string): Promise<MfaRequestInfo> {
|
|
280
|
+
const resp = await (
|
|
281
|
+
await cs.management()
|
|
282
|
+
).patch("/v0/org/{org_id}/mfa/{mfa_id}", {
|
|
283
|
+
params: { path: { org_id: orgId, mfa_id: mfaId } },
|
|
284
|
+
});
|
|
285
|
+
return assertOk(resp);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Fetch org info.
|
|
289
|
+
* @return {OrgInfo} The org info.
|
|
290
|
+
* */
|
|
291
|
+
private async fetch(): Promise<OrgInfo> {
|
|
292
|
+
const resp = await (
|
|
293
|
+
await this.#cs.management()
|
|
294
|
+
).get("/v0/org/{org_id}", {
|
|
295
|
+
params: { path: { org_id: this.id } },
|
|
296
|
+
parseAs: "json",
|
|
297
|
+
});
|
|
298
|
+
const data = assertOk(resp);
|
|
299
|
+
return data;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Update the org.
|
|
303
|
+
* @param {UpdateOrgRequest} request The JSON request to send to the API server.
|
|
304
|
+
* @return {UpdateOrgResponse} The JSON response from the API server.
|
|
305
|
+
* */
|
|
306
|
+
private async update(request: UpdateOrgRequest): Promise<UpdateOrgResponse> {
|
|
307
|
+
const resp = await (
|
|
308
|
+
await this.#cs.management()
|
|
309
|
+
).patch("/v0/org/{org_id}", {
|
|
310
|
+
params: { path: { org_id: this.id } },
|
|
311
|
+
body: request,
|
|
312
|
+
parseAs: "json",
|
|
313
|
+
});
|
|
314
|
+
return assertOk(resp);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** List roles.
|
|
318
|
+
* @param {CubeSigner} cs The CubeSigner instance to use for signing.
|
|
319
|
+
* @param {string} orgId The id of the organization to which the role belongs.
|
|
320
|
+
* @return {Role} The role.
|
|
321
|
+
* @internal
|
|
322
|
+
* */
|
|
323
|
+
private static async roles(cs: CubeSigner, orgId: string): Promise<Role[]> {
|
|
324
|
+
const resp = await (
|
|
325
|
+
await cs.management()
|
|
326
|
+
).get("/v0/org/{org_id}/roles", {
|
|
327
|
+
params: { path: { org_id: orgId } },
|
|
328
|
+
parseAs: "json",
|
|
329
|
+
});
|
|
330
|
+
const data = assertOk(resp);
|
|
331
|
+
return data.roles.map((r: RoleInfo) => new Role(cs, orgId, r));
|
|
332
|
+
}
|
|
333
|
+
}
|