forked from aws/aws-encryption-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkms_keyring_browser.ts
81 lines (73 loc) · 2.03 KB
/
kms_keyring_browser.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import {
KmsKeyringClass,
KmsKeyringInput,
KMSConstructible,
KmsClientSupplier,
getClient,
limitRegions,
excludeRegions,
cacheClients,
AwsEsdkKMSInterface,
} from '@aws-crypto/kms-keyring'
import {
WebCryptoAlgorithmSuite,
WebCryptoEncryptionMaterial,
WebCryptoDecryptionMaterial,
EncryptedDataKey,
immutableClass,
importForWebCryptoEncryptionMaterial,
importForWebCryptoDecryptionMaterial,
KeyringWebCrypto,
Newable,
} from '@aws-crypto/material-management-browser'
import { KMS } from 'aws-sdk'
import { version } from './version'
const getKmsClient = getClient(KMS, {
customUserAgent: `AwsEncryptionSdkJavascriptBrowser/${version}`,
})
const cacheKmsClients = cacheClients(getKmsClient)
export type KmsKeyringWebCryptoInput = Partial<
KmsKeyringInput<AwsEsdkKMSInterface>
>
export type KMSWebCryptoConstructible = KMSConstructible<
KMS,
KMS.ClientConfiguration
>
export type KmsWebCryptoClientSupplier = KmsClientSupplier<AwsEsdkKMSInterface>
export class KmsKeyringBrowser extends KmsKeyringClass<
WebCryptoAlgorithmSuite,
AwsEsdkKMSInterface
>(KeyringWebCrypto as Newable<KeyringWebCrypto>) {
constructor({
clientProvider = cacheKmsClients,
keyIds,
generatorKeyId,
grantTokens,
discovery,
}: KmsKeyringWebCryptoInput = {}) {
super({ clientProvider, keyIds, generatorKeyId, grantTokens, discovery })
}
async _onEncrypt(material: WebCryptoEncryptionMaterial) {
const _material = await super._onEncrypt(material)
return importForWebCryptoEncryptionMaterial(_material)
}
async _onDecrypt(
material: WebCryptoDecryptionMaterial,
encryptedDataKeys: EncryptedDataKey[]
) {
const _material = await super._onDecrypt(material, encryptedDataKeys)
return importForWebCryptoDecryptionMaterial(_material)
}
}
immutableClass(KmsKeyringBrowser)
export {
getClient,
cacheKmsClients,
getKmsClient,
limitRegions,
excludeRegions,
cacheClients,
KMS,
}