-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.ts
345 lines (321 loc) · 8.99 KB
/
key.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { BloockBridge } from "../bridge/bridge";
import { ConfigData } from "../bridge/proto/config";
import {
GenerateLocalCertificateRequest,
GenerateLocalKeyRequest,
GenerateManagedCertificateRequest,
GenerateManagedKeyRequest,
ImportManagedCertificateRequest,
LoadLocalCertificateRequest,
LoadLocalKeyRequest,
LoadManagedCertificateRequest,
LoadManagedKeyRequest,
RecoverTotpAccessControlRequest,
SetupSecretAccessControlRequest,
SetupTotpAccessControlRequest
} from "../bridge/proto/keys";
import { NewConfigData } from "../config/config";
import { CertificateType, KeyType, LocalCertificate } from "../entity/key";
import { LocalKey } from "../entity/key";
import { ManagedKey, ManagedKeyParams } from "../entity/key";
import { LocalCertificateParams } from "../entity/key/local_certificate_args";
import { Managed } from "../entity/key/managed";
import { ManagedCertificate } from "../entity/key/managed_certificate";
import {
ImportCertificateParams,
ManagedCertificateParams
} from "../entity/key/managed_certificate_params";
import { TotpAccessControlReceipt } from "../entity/key/totp_access_control_receipt";
/**
* Provides functionality to interact with the [Bloock Keys service](https://dashboard.bloock.com/login).
*/
export class KeyClient {
private bridge: BloockBridge;
private configData: ConfigData;
/**
* Creates a new KeyClient with default configuration.
* @param configData
*/
constructor(configData?: ConfigData) {
this.bridge = new BloockBridge();
this.configData = NewConfigData(configData);
}
/**
* Generates a new local key of the specified type.
* @param keyType
* @returns
*/
async newLocalKey(keyType: KeyType): Promise<LocalKey> {
const request = GenerateLocalKeyRequest.fromPartial({
configData: this.configData,
keyType: KeyType.toProto(keyType)
});
return this.bridge
.getKey()
.GenerateLocalKey(request)
.then(res => {
if (res.error) {
throw res.error;
}
return LocalKey.fromProto(res.localKey!);
});
}
/**
* Loads a local key of the specified type from a public key string.
* @param keyType
* @param key
* @returns
*/
async loadLocalKey(keyType: KeyType, key: string): Promise<LocalKey> {
const request = LoadLocalKeyRequest.fromPartial({
configData: this.configData,
keyType: KeyType.toProto(keyType),
key
});
return this.bridge
.getKey()
.LoadLocalKey(request)
.then(res => {
if (res.error) {
throw res.error;
}
return LocalKey.fromProto(res.localKey!);
});
}
/**
* Generates a new managed key with the specified parameters.
* @param params
* @returns
*/
async newManagedKey(params: ManagedKeyParams): Promise<ManagedKey> {
const request = GenerateManagedKeyRequest.fromPartial({
configData: this.configData,
params: params.toProto()
});
return this.bridge
.getKey()
.GenerateManagedKey(request)
.then(res => {
if (res.error) {
throw res.error;
}
return ManagedKey.fromProto(res.managedKey!);
});
}
/**
* Loads a managed key by its ID (ex: 51d22546-68f1-4340-b94b-2a80e60b8933).
* @param id
* @returns
*/
async loadManagedKey(id: string): Promise<ManagedKey> {
const request = LoadManagedKeyRequest.fromPartial({
configData: this.configData,
id
});
return this.bridge
.getKey()
.LoadManagedKey(request)
.then(res => {
if (res.error) {
throw res.error;
}
return ManagedKey.fromProto(res.managedKey!);
});
}
/**
* Generates a new local certificate with the specified parameters.
* @param params
* @returns
*/
async newLocalCertificate(
params: LocalCertificateParams
): Promise<LocalCertificate> {
const request = GenerateLocalCertificateRequest.fromPartial({
configData: this.configData,
params: params.toProto()
});
return this.bridge
.getKey()
.GenerateLocalCertificate(request)
.then(res => {
if (res.error) {
throw res.error;
}
return LocalCertificate.fromProto(res.localCertificate!);
});
}
/**
* Loads a local certificate from a PKCS12 file.
* @param pkcs12
* @param password
* @returns
*/
async loadLocalCertificate(
pkcs12: Uint8Array,
password: string
): Promise<LocalCertificate> {
const request = LoadLocalCertificateRequest.fromPartial({
configData: this.configData,
pkcs12: pkcs12,
password: password
});
return this.bridge
.getKey()
.LoadLocalCertificate(request)
.then(res => {
if (res.error) {
throw res.error;
}
return LocalCertificate.fromProto(res.localCertificate!);
});
}
/**
* Generates a new managed certificate with the specified parameters.
* @param params
* @returns
*/
async newManagedCertificate(
params: ManagedCertificateParams
): Promise<ManagedCertificate> {
const request = GenerateManagedCertificateRequest.fromPartial({
configData: this.configData,
params: params.toProto()
});
return this.bridge
.getKey()
.GenerateManagedCertificate(request)
.then(res => {
if (res.error) {
throw res.error;
}
return ManagedCertificate.fromProto(res.managedCertificate!);
});
}
/**
* Loads a managed certificate by its ID (ex: ceef5b02-af17-43d8-ae7b-31d9bdf8027f).
* @param id
* @returns
*/
async loadManagedCertificate(id: string): Promise<ManagedCertificate> {
const request = LoadManagedCertificateRequest.fromPartial({
configData: this.configData,
id
});
return this.bridge
.getKey()
.LoadManagedCertificate(request)
.then(res => {
if (res.error) {
throw res.error;
}
return ManagedCertificate.fromProto(res.managedCertificate!);
});
}
/**
* Imports a managed certificate with the specified parameters, supported types: .pem, .pfx.
* @param type
* @param certificate
* @param params
* @returns
*/
async importManagedCertificate(
type: CertificateType,
certificate: Uint8Array,
params: ImportCertificateParams
): Promise<ManagedCertificate> {
const request = ImportManagedCertificateRequest.fromPartial({
configData: this.configData,
certificate: certificate,
certificateType: CertificateType.toProto(type),
password: params.password
});
return this.bridge
.getKey()
.ImportManagedCertificate(request)
.then(res => {
if (res.error) {
throw res.error;
}
return ManagedCertificate.fromProto(res.managedCertificate!);
});
}
/**
* Sets up TOTP-based access control for the given managed key or managed certificate.
* @param key
* @returns
*/
async setupTotpAccessControl(key: Managed): Promise<TotpAccessControlReceipt> {
const request = SetupTotpAccessControlRequest.fromPartial({
configData: this.configData,
managedKey: key.managedKey?.toProto(),
managedCertificate: key.managedCertificate?.toProto()
});
return this.bridge
.getKey()
.SetupTotpAccessControl(request)
.then(res => {
if (res.error) {
throw res.error;
}
return new TotpAccessControlReceipt(
res.secret!,
res.secretQr!,
res.recoveryCodes!
);
});
}
/**
* Recovers TOTP-based access control for the given managed key or managed certificate using a recovery code.
* @param key
* @param code
* @returns
*/
async recoverTotpAccessControl(
key: Managed,
code: string
): Promise<TotpAccessControlReceipt> {
const request = RecoverTotpAccessControlRequest.fromPartial({
configData: this.configData,
managedKey: key.managedKey?.toProto(),
managedCertificate: key.managedCertificate?.toProto(),
code: code
});
return this.bridge
.getKey()
.RecoverTotpAccessControl(request)
.then(res => {
if (res.error) {
throw res.error;
}
return new TotpAccessControlReceipt(
res.secret!,
res.secretQr!,
res.recoveryCodes!
);
});
}
/**
* Sets up secret-based access control for the given managed key or managed certificate.
* @param key
* @param secret
* @param email
* @returns
*/
async setupSecretAccessControl(key: Managed, secret: string, email: string) {
const request = SetupSecretAccessControlRequest.fromPartial({
configData: this.configData,
managedKey: key.managedKey?.toProto(),
managedCertificate: key.managedCertificate?.toProto(),
secret: secret,
email: email
});
return this.bridge
.getKey()
.SetupSecretAccessControl(request)
.then(res => {
if (res.error) {
throw res.error;
}
});
}
}