@3sixty/vault-models 0.0.0-stage → 0.20.0-next.1

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 (32) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +24 -2
  3. package/dist/es/factories/vaultConnectorFactory.js +9 -0
  4. package/dist/es/factories/vaultConnectorFactory.js.map +1 -0
  5. package/dist/es/index.js +8 -0
  6. package/dist/es/index.js.map +1 -0
  7. package/dist/es/models/IVaultConnector.js +2 -0
  8. package/dist/es/models/IVaultConnector.js.map +1 -0
  9. package/dist/es/models/vaultEncryptionType.js +13 -0
  10. package/dist/es/models/vaultEncryptionType.js.map +1 -0
  11. package/dist/es/models/vaultKeyType.js +17 -0
  12. package/dist/es/models/vaultKeyType.js.map +1 -0
  13. package/dist/es/utils/vaultConnectorHelper.js +57 -0
  14. package/dist/es/utils/vaultConnectorHelper.js.map +1 -0
  15. package/dist/types/factories/vaultConnectorFactory.d.ts +6 -0
  16. package/dist/types/index.d.ts +5 -0
  17. package/dist/types/models/IVaultConnector.d.ts +129 -0
  18. package/dist/types/models/vaultEncryptionType.d.ts +13 -0
  19. package/dist/types/models/vaultKeyType.d.ts +17 -0
  20. package/dist/types/utils/vaultConnectorHelper.d.ts +37 -0
  21. package/docs/changelog.md +9 -0
  22. package/docs/examples.md +141 -0
  23. package/docs/reference/classes/VaultConnectorHelper.md +127 -0
  24. package/docs/reference/index.md +20 -0
  25. package/docs/reference/interfaces/IVaultConnector.md +449 -0
  26. package/docs/reference/type-aliases/VaultEncryptionType.md +5 -0
  27. package/docs/reference/type-aliases/VaultKeyType.md +5 -0
  28. package/docs/reference/variables/VaultConnectorFactory.md +5 -0
  29. package/docs/reference/variables/VaultEncryptionType.md +13 -0
  30. package/docs/reference/variables/VaultKeyType.md +19 -0
  31. package/locales/en.json +7 -0
  32. package/package.json +54 -5
@@ -0,0 +1,141 @@
1
+ # Vault Models Examples
2
+
3
+ These examples show how to register vault connectors, use helper utilities for JWT workflows, and work with the shared key and encryption types.
4
+
5
+ ## VaultConnectorHelper
6
+
7
+ ```typescript
8
+ import { Jwt, type IJwtHeader, type IJwtPayload } from '@3sixty/web';
9
+ import {
10
+ VaultConnectorFactory,
11
+ VaultConnectorHelper,
12
+ VaultEncryptionType,
13
+ VaultKeyType,
14
+ type IVaultConnector
15
+ } from '@3sixty/vault-models';
16
+
17
+ class InMemoryVaultConnector implements IVaultConnector {
18
+ public className(): string {
19
+ return 'InMemoryVaultConnector';
20
+ }
21
+
22
+ public async createKey(name: string, type: VaultKeyType): Promise<Uint8Array> {
23
+ return Promise.resolve(new Uint8Array([type, name.length]));
24
+ }
25
+
26
+ public async addKey(
27
+ _name: string,
28
+ _type: VaultKeyType,
29
+ _privateKey: Uint8Array,
30
+ _publicKey?: Uint8Array
31
+ ): Promise<void> {
32
+ return Promise.resolve();
33
+ }
34
+
35
+ public async getKey(_name: string): Promise<{
36
+ type: VaultKeyType;
37
+ privateKey: Uint8Array;
38
+ publicKey?: Uint8Array;
39
+ }> {
40
+ return Promise.resolve({
41
+ type: VaultKeyType.Ed25519,
42
+ privateKey: new Uint8Array([1, 2, 3]),
43
+ publicKey: new Uint8Array([4, 5, 6])
44
+ });
45
+ }
46
+
47
+ public async getKeyType(_name: string): Promise<VaultKeyType> {
48
+ return Promise.resolve(VaultKeyType.Ed25519);
49
+ }
50
+
51
+ public async renameKey(_name: string, _newName: string): Promise<void> {
52
+ return Promise.resolve();
53
+ }
54
+
55
+ public async removeKey(_name: string): Promise<void> {
56
+ return Promise.resolve();
57
+ }
58
+
59
+ public async sign(_name: string, data: Uint8Array): Promise<Uint8Array> {
60
+ return Promise.resolve(data);
61
+ }
62
+
63
+ public async verify(_name: string, data: Uint8Array, signature: Uint8Array): Promise<boolean> {
64
+ return Promise.resolve(data.length === signature.length);
65
+ }
66
+
67
+ public async encrypt(
68
+ _name: string,
69
+ _encryptionType: VaultEncryptionType,
70
+ data: Uint8Array
71
+ ): Promise<Uint8Array> {
72
+ return Promise.resolve(data);
73
+ }
74
+
75
+ public async decrypt(
76
+ _name: string,
77
+ _encryptionType: VaultEncryptionType,
78
+ encryptedData: Uint8Array
79
+ ): Promise<Uint8Array> {
80
+ return Promise.resolve(encryptedData);
81
+ }
82
+
83
+ public async setSecret<T>(_name: string, _data: T): Promise<void> {
84
+ return Promise.resolve();
85
+ }
86
+
87
+ public async getSecret<T>(_name: string): Promise<T> {
88
+ throw new Error('Not implemented in this example');
89
+ }
90
+
91
+ public async removeSecret(_name: string): Promise<void> {
92
+ return Promise.resolve();
93
+ }
94
+ }
95
+
96
+ VaultConnectorFactory.register('in-memory', () => new InMemoryVaultConnector());
97
+
98
+ const connector = VaultConnectorFactory.get('in-memory');
99
+
100
+ const header: IJwtHeader = {
101
+ alg: 'EdDSA',
102
+ typ: 'JWT'
103
+ };
104
+
105
+ const payload: IJwtPayload = {
106
+ iss: 'example-service',
107
+ sub: 'user-123',
108
+ exp: Math.floor(Date.now() / 1000) + 600
109
+ };
110
+
111
+ const token = await VaultConnectorHelper.jwtSigner(connector, 'jwt-key', header, payload);
112
+ console.log(token.length > 0); // true
113
+
114
+ const decoded = Jwt.decode(token);
115
+ console.log(decoded.payload.sub); // user-123
116
+
117
+ const verified = await VaultConnectorHelper.jwtVerifier<IJwtHeader, IJwtPayload>(
118
+ connector,
119
+ 'jwt-key',
120
+ token
121
+ );
122
+
123
+ console.log(verified.payload.iss); // example-service
124
+ ```
125
+
126
+ ## Types And Constants
127
+
128
+ ```typescript
129
+ import { VaultEncryptionType, VaultKeyType } from '@3sixty/vault-models';
130
+
131
+ const keyTypes: VaultKeyType[] = [
132
+ VaultKeyType.Ed25519,
133
+ VaultKeyType.Secp256k1,
134
+ VaultKeyType.ChaCha20Poly1305
135
+ ];
136
+
137
+ const encryptionType: VaultEncryptionType = VaultEncryptionType.ChaCha20Poly1305;
138
+
139
+ console.log(keyTypes.includes(VaultKeyType.Ed25519)); // true
140
+ console.log(encryptionType === VaultEncryptionType.ChaCha20Poly1305); // true
141
+ ```
@@ -0,0 +1,127 @@
1
+ # Class: VaultConnectorHelper
2
+
3
+ Helpers for vault connectors.
4
+
5
+ ## Constructors
6
+
7
+ ### Constructor
8
+
9
+ > **new VaultConnectorHelper**(): `VaultConnectorHelper`
10
+
11
+ #### Returns
12
+
13
+ `VaultConnectorHelper`
14
+
15
+ ## Properties
16
+
17
+ ### CLASS\_NAME {#class_name}
18
+
19
+ > `readonly` `static` **CLASS\_NAME**: `string`
20
+
21
+ Runtime name for the class.
22
+
23
+ ## Methods
24
+
25
+ ### jwtSigner() {#jwtsigner}
26
+
27
+ > `static` **jwtSigner**(`vaultConnector`, `keyName`, `header`, `payload`): `Promise`\<`string`\>
28
+
29
+ Sign a JWT using vault connector.
30
+
31
+ #### Parameters
32
+
33
+ ##### vaultConnector
34
+
35
+ [`IVaultConnector`](../interfaces/IVaultConnector.md)
36
+
37
+ The vault connector to use.
38
+
39
+ ##### keyName
40
+
41
+ `string`
42
+
43
+ The name of the key to sign with.
44
+
45
+ ##### header
46
+
47
+ `JWTHeaderParameters`
48
+
49
+ The header to sign.
50
+
51
+ ##### payload
52
+
53
+ `JWTPayload`
54
+
55
+ The payload to sign.
56
+
57
+ #### Returns
58
+
59
+ `Promise`\<`string`\>
60
+
61
+ The token.
62
+
63
+ ***
64
+
65
+ ### jwtVerifier() {#jwtverifier}
66
+
67
+ > `static` **jwtVerifier**\<`T`, `U`\>(`vaultConnector`, `keyName`, `token`): `Promise`\<\{ `header`: `T`; `payload`: `U`; \}\>
68
+
69
+ Verify a JWT using a vault connector.
70
+
71
+ #### Type Parameters
72
+
73
+ ##### T {#t}
74
+
75
+ `T` *extends* `JWTHeaderParameters`
76
+
77
+ ##### U {#u}
78
+
79
+ `U` *extends* `JWTPayload`
80
+
81
+ #### Parameters
82
+
83
+ ##### vaultConnector
84
+
85
+ [`IVaultConnector`](../interfaces/IVaultConnector.md)
86
+
87
+ The vault connector to use.
88
+
89
+ ##### keyName
90
+
91
+ `string`
92
+
93
+ The name of the key to verify with.
94
+
95
+ ##### token
96
+
97
+ `string`
98
+
99
+ The token to verify.
100
+
101
+ #### Returns
102
+
103
+ `Promise`\<\{ `header`: `T`; `payload`: `U`; \}\>
104
+
105
+ The header and payload if verification successful.
106
+
107
+ ***
108
+
109
+ ### buildKeyName() {#buildkeyname}
110
+
111
+ > `static` **buildKeyName**(...`keyParts`): `string`
112
+
113
+ Build a key name from parts.
114
+
115
+ #### Parameters
116
+
117
+ ##### keyParts
118
+
119
+ ...`string`[]
120
+
121
+ The parts of the key.
122
+
123
+ #### Returns
124
+
125
+ `string`
126
+
127
+ The constructed key name.
@@ -0,0 +1,20 @@
1
+ # @3sixty/vault-models
2
+
3
+ ## Classes
4
+
5
+ - [VaultConnectorHelper](classes/VaultConnectorHelper.md)
6
+
7
+ ## Interfaces
8
+
9
+ - [IVaultConnector](interfaces/IVaultConnector.md)
10
+
11
+ ## Type Aliases
12
+
13
+ - [VaultEncryptionType](type-aliases/VaultEncryptionType.md)
14
+ - [VaultKeyType](type-aliases/VaultKeyType.md)
15
+
16
+ ## Variables
17
+
18
+ - [VaultConnectorFactory](variables/VaultConnectorFactory.md)
19
+ - [VaultEncryptionType](variables/VaultEncryptionType.md)
20
+ - [VaultKeyType](variables/VaultKeyType.md)
@@ -0,0 +1,449 @@
1
+ # Interface: IVaultConnector
2
+
3
+ Interface describing a vault securely storing data.
4
+
5
+ ## Extends
6
+
7
+ - `IComponent`
8
+
9
+ ## Methods
10
+
11
+ ### createKey() {#createkey}
12
+
13
+ > **createKey**(`name`, `type`): `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
14
+
15
+ Generate a new key and store it in the vault.
16
+
17
+ #### Parameters
18
+
19
+ ##### name
20
+
21
+ `string`
22
+
23
+ The name of the key to generate and store in the vault.
24
+
25
+ ##### type
26
+
27
+ [`VaultKeyType`](../type-aliases/VaultKeyType.md)
28
+
29
+ The type of key to create.
30
+
31
+ #### Returns
32
+
33
+ `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
34
+
35
+ The public key for the key pair.
36
+
37
+ ***
38
+
39
+ ### addKey() {#addkey}
40
+
41
+ > **addKey**(`name`, `type`, `privateKey`, `publicKey?`): `Promise`\<`void`\>
42
+
43
+ Add an existing key to the vault.
44
+
45
+ #### Parameters
46
+
47
+ ##### name
48
+
49
+ `string`
50
+
51
+ The name of the key to add to the vault.
52
+
53
+ ##### type
54
+
55
+ [`VaultKeyType`](../type-aliases/VaultKeyType.md)
56
+
57
+ The type of key to add.
58
+
59
+ ##### privateKey
60
+
61
+ `Uint8Array`
62
+
63
+ The private key.
64
+
65
+ ##### publicKey?
66
+
67
+ `Uint8Array`\<`ArrayBufferLike`\>
68
+
69
+ The public key, can be undefined if the key type is symmetric.
70
+
71
+ #### Returns
72
+
73
+ `Promise`\<`void`\>
74
+
75
+ A promise that resolves when the key has been stored.
76
+
77
+ ***
78
+
79
+ ### getKey() {#getkey}
80
+
81
+ > **getKey**(`name`, `components?`): `Promise`\<\{ `type`: [`VaultKeyType`](../type-aliases/VaultKeyType.md); `privateKey?`: `Uint8Array`\<`ArrayBufferLike`\>; `publicKey?`: `Uint8Array`\<`ArrayBufferLike`\>; \}\>
82
+
83
+ Get a key from the vault.
84
+
85
+ #### Parameters
86
+
87
+ ##### name
88
+
89
+ `string`
90
+
91
+ The name of the key to get from the vault.
92
+
93
+ ##### components?
94
+
95
+ `"public"` \| `"private"` \| `"both"`
96
+
97
+ Which key components to return, defaults to "both".
98
+
99
+ #### Returns
100
+
101
+ `Promise`\<\{ `type`: [`VaultKeyType`](../type-aliases/VaultKeyType.md); `privateKey?`: `Uint8Array`\<`ArrayBufferLike`\>; `publicKey?`: `Uint8Array`\<`ArrayBufferLike`\>; \}\>
102
+
103
+ The key, publicKey can be undefined if key is symmetric, privateKey can be undefined if only public was requested.
104
+
105
+ #### Throws
106
+
107
+ GeneralError if "private" is requested for a symmetric key.
108
+
109
+ ***
110
+
111
+ ### keyExists() {#keyexists}
112
+
113
+ > **keyExists**(`name`): `Promise`\<`boolean`\>
114
+
115
+ Check if a key exists in the vault.
116
+
117
+ #### Parameters
118
+
119
+ ##### name
120
+
121
+ `string`
122
+
123
+ The name of the key to check.
124
+
125
+ #### Returns
126
+
127
+ `Promise`\<`boolean`\>
128
+
129
+ True if the key exists, false otherwise.
130
+
131
+ ***
132
+
133
+ ### getKeyType() {#getkeytype}
134
+
135
+ > **getKeyType**(`name`): `Promise`\<[`VaultKeyType`](../type-aliases/VaultKeyType.md)\>
136
+
137
+ Get the type of a key from the vault without retrieving the key material.
138
+
139
+ #### Parameters
140
+
141
+ ##### name
142
+
143
+ `string`
144
+
145
+ The name of the key.
146
+
147
+ #### Returns
148
+
149
+ `Promise`\<[`VaultKeyType`](../type-aliases/VaultKeyType.md)\>
150
+
151
+ The key type.
152
+
153
+ #### Throws
154
+
155
+ NotFoundError if the key does not exist.
156
+
157
+ ***
158
+
159
+ ### renameKey() {#renamekey}
160
+
161
+ > **renameKey**(`name`, `newName`): `Promise`\<`void`\>
162
+
163
+ Rename a key in the vault.
164
+
165
+ #### Parameters
166
+
167
+ ##### name
168
+
169
+ `string`
170
+
171
+ The name of the key to rename.
172
+
173
+ ##### newName
174
+
175
+ `string`
176
+
177
+ The new name of the key.
178
+
179
+ #### Returns
180
+
181
+ `Promise`\<`void`\>
182
+
183
+ A promise that resolves when the key has been renamed.
184
+
185
+ ***
186
+
187
+ ### removeKey() {#removekey}
188
+
189
+ > **removeKey**(`name`): `Promise`\<`void`\>
190
+
191
+ Remove a key from the vault.
192
+
193
+ #### Parameters
194
+
195
+ ##### name
196
+
197
+ `string`
198
+
199
+ The name of the key to remove from the vault.
200
+
201
+ #### Returns
202
+
203
+ `Promise`\<`void`\>
204
+
205
+ A promise that resolves when the key has been removed.
206
+
207
+ ***
208
+
209
+ ### sign() {#sign}
210
+
211
+ > **sign**(`name`, `data`): `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
212
+
213
+ Sign the data using a key in the vault.
214
+
215
+ #### Parameters
216
+
217
+ ##### name
218
+
219
+ `string`
220
+
221
+ The name of the key to use for signing.
222
+
223
+ ##### data
224
+
225
+ `Uint8Array`
226
+
227
+ The data to sign.
228
+
229
+ #### Returns
230
+
231
+ `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
232
+
233
+ The signature for the data.
234
+
235
+ ***
236
+
237
+ ### verify() {#verify}
238
+
239
+ > **verify**(`name`, `data`, `signature`): `Promise`\<`boolean`\>
240
+
241
+ Verify the signature of the data using a key in the vault.
242
+
243
+ #### Parameters
244
+
245
+ ##### name
246
+
247
+ `string`
248
+
249
+ The name of the key to use for verification.
250
+
251
+ ##### data
252
+
253
+ `Uint8Array`
254
+
255
+ The data that was signed.
256
+
257
+ ##### signature
258
+
259
+ `Uint8Array`
260
+
261
+ The signature to verify.
262
+
263
+ #### Returns
264
+
265
+ `Promise`\<`boolean`\>
266
+
267
+ True if the verification is successful.
268
+
269
+ ***
270
+
271
+ ### encrypt() {#encrypt}
272
+
273
+ > **encrypt**(`name`, `encryptionType`, `data`): `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
274
+
275
+ Encrypt the data using a key in the vault.
276
+
277
+ #### Parameters
278
+
279
+ ##### name
280
+
281
+ `string`
282
+
283
+ The name of the key to use for encryption.
284
+
285
+ ##### encryptionType
286
+
287
+ `0`
288
+
289
+ The type of encryption to use.
290
+
291
+ ##### data
292
+
293
+ `Uint8Array`
294
+
295
+ The data to encrypt.
296
+
297
+ #### Returns
298
+
299
+ `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
300
+
301
+ The encrypted data.
302
+
303
+ ***
304
+
305
+ ### decrypt() {#decrypt}
306
+
307
+ > **decrypt**(`name`, `encryptionType`, `encryptedData`): `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
308
+
309
+ Decrypt the data using a key in the vault.
310
+
311
+ #### Parameters
312
+
313
+ ##### name
314
+
315
+ `string`
316
+
317
+ The name of the key to use for decryption.
318
+
319
+ ##### encryptionType
320
+
321
+ `0`
322
+
323
+ The type of encryption to use.
324
+
325
+ ##### encryptedData
326
+
327
+ `Uint8Array`
328
+
329
+ The data to decrypt.
330
+
331
+ #### Returns
332
+
333
+ `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
334
+
335
+ The decrypted data.
336
+
337
+ ***
338
+
339
+ ### setSecret() {#setsecret}
340
+
341
+ > **setSecret**\<`T`\>(`name`, `data`): `Promise`\<`void`\>
342
+
343
+ Store a secret in the vault.
344
+
345
+ #### Type Parameters
346
+
347
+ ##### T {#t}
348
+
349
+ `T`
350
+
351
+ #### Parameters
352
+
353
+ ##### name
354
+
355
+ `string`
356
+
357
+ The name of the secret in the vault to set.
358
+
359
+ ##### data
360
+
361
+ `T`
362
+
363
+ The secret to add to the vault.
364
+
365
+ #### Returns
366
+
367
+ `Promise`\<`void`\>
368
+
369
+ A promise that resolves when the secret has been stored.
370
+
371
+ ***
372
+
373
+ ### secretExists() {#secretexists}
374
+
375
+ > **secretExists**(`name`): `Promise`\<`boolean`\>
376
+
377
+ Check if a secret exists in the vault.
378
+
379
+ #### Parameters
380
+
381
+ ##### name
382
+
383
+ `string`
384
+
385
+ The name of the secret to check.
386
+
387
+ #### Returns
388
+
389
+ `Promise`\<`boolean`\>
390
+
391
+ True if the secret exists, false otherwise.
392
+
393
+ ***
394
+
395
+ ### getSecret() {#getsecret}
396
+
397
+ > **getSecret**\<`T`\>(`name`): `Promise`\<`T`\>
398
+
399
+ Get a secret from the vault.
400
+
401
+ #### Type Parameters
402
+
403
+ ##### T {#t-1}
404
+
405
+ `T`
406
+
407
+ #### Parameters
408
+
409
+ ##### name
410
+
411
+ `string`
412
+
413
+ The name of the secret in the vault to get.
414
+
415
+ #### Returns
416
+
417
+ `Promise`\<`T`\>
418
+
419
+ The secret from the vault.
420
+
421
+ #### Throws
422
+
423
+ Error if the secret is not found.
424
+
425
+ ***
426
+
427
+ ### removeSecret() {#removesecret}
428
+
429
+ > **removeSecret**(`name`): `Promise`\<`void`\>
430
+
431
+ Remove a secret from the vault.
432
+
433
+ #### Parameters
434
+
435
+ ##### name
436
+
437
+ `string`
438
+
439
+ The name of the secret in the vault to remove.
440
+
441
+ #### Returns
442
+
443
+ `Promise`\<`void`\>
444
+
445
+ A promise that resolves when the secret has been removed.
446
+
447
+ #### Throws
448
+
449
+ Error if the secret is not found.