@opentdf/sdk 0.2.0-beta.1758 → 0.2.0-beta.1941
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/README.md +45 -38
- package/dist/cjs/src/access.js +47 -11
- package/dist/cjs/src/auth/auth.js +5 -5
- package/dist/cjs/src/auth/oidc-clientcredentials-provider.js +1 -1
- package/dist/cjs/src/auth/oidc-externaljwt-provider.js +1 -1
- package/dist/cjs/src/auth/oidc-refreshtoken-provider.js +1 -1
- package/dist/cjs/src/auth/oidc.js +1 -1
- package/dist/cjs/src/auth/providers.js +1 -1
- package/dist/cjs/src/concurrency.js +3 -4
- package/dist/cjs/src/encodings/base64.js +4 -4
- package/dist/cjs/src/encodings/hex.js +5 -6
- package/dist/cjs/src/encodings/index.js +18 -8
- package/dist/cjs/src/errors.js +1 -1
- package/dist/cjs/src/index.js +28 -318
- package/dist/cjs/src/nanoclients.js +285 -0
- package/dist/cjs/src/nanoindex.js +47 -0
- package/dist/cjs/src/nanotdf/Client.js +18 -8
- package/dist/cjs/src/nanotdf/NanoTDF.js +1 -1
- package/dist/cjs/src/nanotdf/decrypt.js +2 -2
- package/dist/cjs/src/nanotdf/encrypt-dataset.js +2 -2
- package/dist/cjs/src/nanotdf/encrypt.js +2 -2
- package/dist/cjs/src/nanotdf/helpers/calculateByCurve.js +3 -4
- package/dist/cjs/src/nanotdf/helpers/getHkdfSalt.js +2 -2
- package/dist/cjs/src/nanotdf/models/Ciphers.js +3 -3
- package/dist/cjs/src/nanotdf/models/EcCurves.js +3 -3
- package/dist/cjs/src/nanotdf/models/Header.js +1 -1
- package/dist/cjs/src/nanotdf/models/Payload.js +1 -1
- package/dist/cjs/src/nanotdf/models/Policy/AbstractPolicy.js +1 -1
- package/dist/cjs/src/nanotdf/models/Policy/EmbeddedPolicy.js +1 -1
- package/dist/cjs/src/nanotdf/models/Policy/PolicyFactory.js +1 -1
- package/dist/cjs/src/nanotdf/models/ResourceLocator.js +1 -1
- package/dist/cjs/src/nanotdf/models/Signature.js +1 -1
- package/dist/cjs/src/nanotdf-crypto/ciphers.js +1 -1
- package/dist/cjs/src/nanotdf-crypto/decrypt.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/digest.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/ecdsaSignature.js +4 -5
- package/dist/cjs/src/nanotdf-crypto/encrypt.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/exportCryptoKey.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/generateKeyPair.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/generateRandomNumber.js +2 -2
- package/dist/cjs/src/nanotdf-crypto/index.js +21 -13
- package/dist/cjs/src/nanotdf-crypto/keyAgreement.js +10 -8
- package/dist/cjs/src/nanotdf-crypto/pemPublicToCrypto.js +20 -11
- package/dist/cjs/src/opentdf.js +243 -0
- package/dist/cjs/src/policy/api.js +2 -3
- package/dist/cjs/src/policy/granter.js +3 -4
- package/dist/cjs/src/seekable.js +157 -0
- package/dist/cjs/src/tdf/AttributeObject.js +2 -4
- package/dist/cjs/src/tdf/Policy.js +1 -2
- package/dist/cjs/src/utils.js +12 -14
- package/dist/cjs/src/version.js +6 -2
- package/dist/cjs/tdf3/index.js +27 -15
- package/dist/cjs/tdf3/src/assertions.js +25 -11
- package/dist/cjs/tdf3/src/binary.js +1 -1
- package/dist/cjs/tdf3/src/ciphers/aes-gcm-cipher.js +1 -1
- package/dist/cjs/tdf3/src/ciphers/symmetric-cipher-base.js +1 -1
- package/dist/cjs/tdf3/src/client/DecoratedReadableStream.js +7 -74
- package/dist/cjs/tdf3/src/client/builders.js +26 -22
- package/dist/cjs/tdf3/src/client/index.js +88 -61
- package/dist/cjs/tdf3/src/client/validation.js +3 -3
- package/dist/cjs/tdf3/src/crypto/crypto-utils.js +1 -1
- package/dist/cjs/tdf3/src/crypto/index.js +18 -18
- package/dist/cjs/tdf3/src/index.js +22 -11
- package/dist/cjs/tdf3/src/models/attribute-set.js +1 -1
- package/dist/cjs/tdf3/src/models/encryption-information.js +3 -3
- package/dist/cjs/tdf3/src/models/key-access.js +67 -35
- package/dist/cjs/tdf3/src/models/policy.js +3 -3
- package/dist/cjs/tdf3/src/tdf.js +177 -151
- package/dist/cjs/tdf3/src/utils/buffer-crc32.js +2 -3
- package/dist/cjs/tdf3/src/utils/index.js +30 -28
- package/dist/cjs/tdf3/src/utils/keysplit.js +4 -5
- package/dist/cjs/tdf3/src/utils/unwrap.js +21 -0
- package/dist/cjs/tdf3/src/utils/zip-reader.js +4 -4
- package/dist/cjs/tdf3/src/utils/zip-writer.js +4 -4
- package/dist/types/src/access.d.ts +3 -0
- package/dist/types/src/access.d.ts.map +1 -1
- package/dist/types/src/auth/providers.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +5 -136
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/nanoclients.d.ts +107 -0
- package/dist/types/src/nanoclients.d.ts.map +1 -0
- package/dist/types/src/nanoindex.d.ts +5 -0
- package/dist/types/src/nanoindex.d.ts.map +1 -0
- package/dist/types/src/nanotdf/enum/CipherEnum.d.ts +1 -1
- package/dist/types/src/nanotdf/enum/CipherEnum.d.ts.map +1 -1
- package/dist/types/src/nanotdf/enum/PolicyTypeEnum.d.ts +1 -1
- package/dist/types/src/nanotdf/enum/PolicyTypeEnum.d.ts.map +1 -1
- package/dist/types/src/nanotdf/models/DefaultParams.d.ts +1 -1
- package/dist/types/src/nanotdf/models/ResourceLocator.d.ts.map +1 -1
- package/dist/types/src/nanotdf-crypto/generateKeyPair.d.ts +1 -1
- package/dist/types/src/nanotdf-crypto/generateKeyPair.d.ts.map +1 -1
- package/dist/types/src/nanotdf-crypto/generateRandomNumber.d.ts +1 -1
- package/dist/types/src/nanotdf-crypto/generateRandomNumber.d.ts.map +1 -1
- package/dist/types/src/nanotdf-crypto/index.d.ts +2 -3
- package/dist/types/src/nanotdf-crypto/index.d.ts.map +1 -1
- package/dist/types/src/nanotdf-crypto/keyAgreement.d.ts.map +1 -1
- package/dist/types/src/opentdf.d.ts +106 -0
- package/dist/types/src/opentdf.d.ts.map +1 -0
- package/dist/types/src/seekable.d.ts +39 -0
- package/dist/types/src/seekable.d.ts.map +1 -0
- package/dist/types/src/tdf/AttributeObject.d.ts +0 -2
- package/dist/types/src/tdf/AttributeObject.d.ts.map +1 -1
- package/dist/types/src/tdf/NanoTDF/NanoTDF.d.ts +2 -2
- package/dist/types/src/tdf/NanoTDF/NanoTDF.d.ts.map +1 -1
- package/dist/types/src/tdf/Policy.d.ts.map +1 -1
- package/dist/types/src/tdf/PolicyObject.d.ts +0 -1
- package/dist/types/src/tdf/PolicyObject.d.ts.map +1 -1
- package/dist/types/src/utils.d.ts +0 -1
- package/dist/types/src/utils.d.ts.map +1 -1
- package/dist/types/src/version.d.ts +4 -0
- package/dist/types/src/version.d.ts.map +1 -1
- package/dist/types/tdf3/index.d.ts +3 -2
- package/dist/types/tdf3/index.d.ts.map +1 -1
- package/dist/types/tdf3/src/assertions.d.ts +3 -3
- package/dist/types/tdf3/src/assertions.d.ts.map +1 -1
- package/dist/types/tdf3/src/client/DecoratedReadableStream.d.ts +1 -13
- package/dist/types/tdf3/src/client/DecoratedReadableStream.d.ts.map +1 -1
- package/dist/types/tdf3/src/client/builders.d.ts +43 -37
- package/dist/types/tdf3/src/client/builders.d.ts.map +1 -1
- package/dist/types/tdf3/src/client/index.d.ts +8 -9
- package/dist/types/tdf3/src/client/index.d.ts.map +1 -1
- package/dist/types/tdf3/src/client/validation.d.ts +3 -3
- package/dist/types/tdf3/src/client/validation.d.ts.map +1 -1
- package/dist/types/tdf3/src/crypto/crypto-utils.d.ts.map +1 -1
- package/dist/types/tdf3/src/index.d.ts +1 -1
- package/dist/types/tdf3/src/index.d.ts.map +1 -1
- package/dist/types/tdf3/src/models/key-access.d.ts +63 -15
- package/dist/types/tdf3/src/models/key-access.d.ts.map +1 -1
- package/dist/types/tdf3/src/models/manifest.d.ts +2 -0
- package/dist/types/tdf3/src/models/manifest.d.ts.map +1 -1
- package/dist/types/tdf3/src/models/policy.d.ts +0 -1
- package/dist/types/tdf3/src/models/policy.d.ts.map +1 -1
- package/dist/types/tdf3/src/tdf.d.ts +20 -24
- package/dist/types/tdf3/src/tdf.d.ts.map +1 -1
- package/dist/types/tdf3/src/utils/index.d.ts +0 -2
- package/dist/types/tdf3/src/utils/index.d.ts.map +1 -1
- package/dist/types/tdf3/src/utils/unwrap.d.ts +2 -0
- package/dist/types/tdf3/src/utils/unwrap.d.ts.map +1 -0
- package/dist/types/tdf3/src/utils/zip-reader.d.ts +1 -1
- package/dist/types/tdf3/src/utils/zip-reader.d.ts.map +1 -1
- package/dist/types/tdf3/src/utils/zip-writer.d.ts +2 -2
- package/dist/web/src/access.js +40 -7
- package/dist/web/src/auth/auth.js +1 -1
- package/dist/web/src/auth/oidc-clientcredentials-provider.js +1 -1
- package/dist/web/src/auth/oidc-externaljwt-provider.js +1 -1
- package/dist/web/src/auth/oidc-refreshtoken-provider.js +1 -1
- package/dist/web/src/auth/oidc.js +1 -1
- package/dist/web/src/auth/providers.js +1 -1
- package/dist/web/src/concurrency.js +1 -1
- package/dist/web/src/encodings/base64.js +1 -1
- package/dist/web/src/encodings/hex.js +1 -1
- package/dist/web/src/errors.js +1 -1
- package/dist/web/src/index.js +6 -310
- package/dist/web/src/nanoclients.js +280 -0
- package/dist/web/src/nanoindex.js +5 -0
- package/dist/web/src/nanotdf/Client.js +1 -1
- package/dist/web/src/nanotdf/NanoTDF.js +1 -1
- package/dist/web/src/nanotdf/encrypt-dataset.js +1 -1
- package/dist/web/src/nanotdf/encrypt.js +1 -1
- package/dist/web/src/nanotdf/models/Ciphers.js +1 -1
- package/dist/web/src/nanotdf/models/EcCurves.js +1 -1
- package/dist/web/src/nanotdf/models/Header.js +1 -1
- package/dist/web/src/nanotdf/models/Payload.js +1 -1
- package/dist/web/src/nanotdf/models/Policy/AbstractPolicy.js +1 -1
- package/dist/web/src/nanotdf/models/Policy/EmbeddedPolicy.js +1 -1
- package/dist/web/src/nanotdf/models/Policy/PolicyFactory.js +1 -1
- package/dist/web/src/nanotdf/models/ResourceLocator.js +1 -1
- package/dist/web/src/nanotdf/models/Signature.js +1 -1
- package/dist/web/src/nanotdf-crypto/ciphers.js +1 -1
- package/dist/web/src/nanotdf-crypto/ecdsaSignature.js +1 -1
- package/dist/web/src/nanotdf-crypto/generateKeyPair.js +2 -2
- package/dist/web/src/nanotdf-crypto/generateRandomNumber.js +2 -2
- package/dist/web/src/nanotdf-crypto/index.js +3 -4
- package/dist/web/src/nanotdf-crypto/keyAgreement.js +9 -6
- package/dist/web/src/nanotdf-crypto/pemPublicToCrypto.js +1 -1
- package/dist/web/src/opentdf.js +234 -0
- package/dist/web/src/policy/api.js +1 -1
- package/dist/web/src/policy/granter.js +1 -1
- package/dist/web/src/seekable.js +148 -0
- package/dist/web/src/tdf/AttributeObject.js +1 -2
- package/dist/web/src/tdf/Policy.js +1 -2
- package/dist/web/src/utils.js +2 -3
- package/dist/web/src/version.js +5 -1
- package/dist/web/tdf3/index.js +3 -2
- package/dist/web/tdf3/src/assertions.js +21 -6
- package/dist/web/tdf3/src/binary.js +1 -1
- package/dist/web/tdf3/src/ciphers/aes-gcm-cipher.js +1 -1
- package/dist/web/tdf3/src/ciphers/symmetric-cipher-base.js +1 -1
- package/dist/web/tdf3/src/client/DecoratedReadableStream.js +4 -68
- package/dist/web/tdf3/src/client/builders.js +26 -22
- package/dist/web/tdf3/src/client/index.js +69 -52
- package/dist/web/tdf3/src/client/validation.js +1 -1
- package/dist/web/tdf3/src/crypto/crypto-utils.js +1 -1
- package/dist/web/tdf3/src/crypto/index.js +1 -1
- package/dist/web/tdf3/src/index.js +2 -2
- package/dist/web/tdf3/src/models/attribute-set.js +1 -1
- package/dist/web/tdf3/src/models/encryption-information.js +3 -3
- package/dist/web/tdf3/src/models/key-access.js +47 -24
- package/dist/web/tdf3/src/models/policy.js +1 -1
- package/dist/web/tdf3/src/tdf.js +149 -130
- package/dist/web/tdf3/src/utils/buffer-crc32.js +1 -1
- package/dist/web/tdf3/src/utils/index.js +1 -5
- package/dist/web/tdf3/src/utils/keysplit.js +1 -1
- package/dist/web/tdf3/src/utils/unwrap.js +18 -0
- package/dist/web/tdf3/src/utils/zip-reader.js +1 -1
- package/dist/web/tdf3/src/utils/zip-writer.js +1 -1
- package/package.json +45 -42
- package/src/access.ts +37 -1
- package/src/index.ts +5 -435
- package/src/nanoclients.ts +405 -0
- package/src/nanoindex.ts +4 -0
- package/src/nanotdf-crypto/generateKeyPair.ts +1 -1
- package/src/nanotdf-crypto/generateRandomNumber.ts +1 -1
- package/src/nanotdf-crypto/index.ts +2 -3
- package/src/nanotdf-crypto/keyAgreement.ts +14 -7
- package/src/opentdf.ts +441 -0
- package/{tdf3/src/utils/chunkers.ts → src/seekable.ts} +69 -20
- package/src/tdf/AttributeObject.ts +0 -3
- package/src/tdf/Policy.ts +0 -1
- package/src/tdf/PolicyObject.ts +0 -1
- package/src/utils.ts +1 -3
- package/src/version.ts +5 -0
- package/tdf3/index.ts +14 -2
- package/tdf3/src/assertions.ts +33 -8
- package/tdf3/src/client/DecoratedReadableStream.ts +2 -78
- package/tdf3/src/client/builders.ts +44 -26
- package/tdf3/src/client/index.ts +101 -86
- package/tdf3/src/index.ts +1 -1
- package/tdf3/src/models/encryption-information.ts +2 -2
- package/tdf3/src/models/key-access.ts +120 -38
- package/tdf3/src/models/manifest.ts +3 -0
- package/tdf3/src/models/policy.ts +0 -1
- package/tdf3/src/tdf.ts +251 -207
- package/tdf3/src/utils/index.ts +0 -5
- package/tdf3/src/utils/unwrap.ts +17 -0
- package/tdf3/src/utils/zip-reader.ts +1 -1
- package/dist/cjs/src/nanotdf-crypto/importRawKey.js +0 -18
- package/dist/cjs/tdf3/src/templates/default.html.js +0 -98
- package/dist/cjs/tdf3/src/templates/escaper.js +0 -15
- package/dist/cjs/tdf3/src/templates/index.js +0 -12
- package/dist/cjs/tdf3/src/utils/chunkers.js +0 -114
- package/dist/cjs/tdf3/src/version.js +0 -6
- package/dist/types/src/nanotdf-crypto/importRawKey.d.ts +0 -13
- package/dist/types/src/nanotdf-crypto/importRawKey.d.ts.map +0 -1
- package/dist/types/tdf3/src/templates/default.html.d.ts +0 -8
- package/dist/types/tdf3/src/templates/default.html.d.ts.map +0 -1
- package/dist/types/tdf3/src/templates/escaper.d.ts +0 -6
- package/dist/types/tdf3/src/templates/escaper.d.ts.map +0 -1
- package/dist/types/tdf3/src/templates/index.d.ts +0 -3
- package/dist/types/tdf3/src/templates/index.d.ts.map +0 -1
- package/dist/types/tdf3/src/utils/chunkers.d.ts +0 -29
- package/dist/types/tdf3/src/utils/chunkers.d.ts.map +0 -1
- package/dist/types/tdf3/src/version.d.ts +0 -3
- package/dist/types/tdf3/src/version.d.ts.map +0 -1
- package/dist/web/src/nanotdf-crypto/importRawKey.js +0 -15
- package/dist/web/tdf3/src/templates/default.html.js +0 -96
- package/dist/web/tdf3/src/templates/escaper.js +0 -10
- package/dist/web/tdf3/src/templates/index.js +0 -3
- package/dist/web/tdf3/src/utils/chunkers.js +0 -107
- package/dist/web/tdf3/src/version.js +0 -3
- package/src/nanotdf-crypto/importRawKey.ts +0 -19
- package/tdf3/src/templates/default.html.ts +0 -105
- package/tdf3/src/templates/escaper.ts +0 -10
- package/tdf3/src/templates/index.ts +0 -2
- package/tdf3/src/version.ts +0 -2
package/tdf3/src/assertions.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type KeyLike, SignJWT, jwtVerify } from 'jose';
|
|
|
3
3
|
import { base64, hex } from '../../src/encodings/index.js';
|
|
4
4
|
import { ConfigurationError, IntegrityError, InvalidFileError } from '../../src/errors.js';
|
|
5
5
|
|
|
6
|
-
export type AssertionKeyAlg = 'RS256' | 'HS256';
|
|
6
|
+
export type AssertionKeyAlg = 'ES256' | 'RS256' | 'HS256';
|
|
7
7
|
export type AssertionType = 'handling' | 'other';
|
|
8
8
|
export type Scope = 'tdo' | 'payload';
|
|
9
9
|
export type AppliesToState = 'encrypted' | 'unencrypted';
|
|
@@ -110,8 +110,9 @@ export function isAssertionConfig(obj: unknown): obj is AssertionConfig {
|
|
|
110
110
|
*/
|
|
111
111
|
export async function verify(
|
|
112
112
|
thiz: Assertion,
|
|
113
|
-
aggregateHash:
|
|
114
|
-
key: AssertionKey
|
|
113
|
+
aggregateHash: Uint8Array,
|
|
114
|
+
key: AssertionKey,
|
|
115
|
+
isLegacyTDF: boolean
|
|
115
116
|
): Promise<void> {
|
|
116
117
|
let payload: AssertionPayload;
|
|
117
118
|
try {
|
|
@@ -126,14 +127,25 @@ export async function verify(
|
|
|
126
127
|
|
|
127
128
|
// Get the hash of the assertion
|
|
128
129
|
const hashOfAssertion = await hash(thiz);
|
|
129
|
-
const combinedHash = aggregateHash + hashOfAssertion;
|
|
130
|
-
const encodedHash = base64.encode(combinedHash);
|
|
131
130
|
|
|
132
131
|
// check if assertionHash is same as hashOfAssertion
|
|
133
132
|
if (hashOfAssertion !== assertionHash) {
|
|
134
133
|
throw new IntegrityError('Assertion hash mismatch');
|
|
135
134
|
}
|
|
136
135
|
|
|
136
|
+
let encodedHash: string;
|
|
137
|
+
if (isLegacyTDF) {
|
|
138
|
+
const aggregateHashAsStr = new TextDecoder('utf-8').decode(aggregateHash);
|
|
139
|
+
const combinedHash = aggregateHashAsStr + hashOfAssertion;
|
|
140
|
+
encodedHash = base64.encode(combinedHash);
|
|
141
|
+
} else {
|
|
142
|
+
const combinedHash = concatenateUint8Arrays(
|
|
143
|
+
aggregateHash,
|
|
144
|
+
new Uint8Array(hex.decodeArrayBuffer(assertionHash))
|
|
145
|
+
);
|
|
146
|
+
encodedHash = base64.encodeArrayBuffer(combinedHash);
|
|
147
|
+
}
|
|
148
|
+
|
|
137
149
|
// check if assertionSig is same as encodedHash
|
|
138
150
|
if (assertionSig !== encodedHash) {
|
|
139
151
|
throw new IntegrityError('Failed integrity check on assertion signature');
|
|
@@ -144,7 +156,7 @@ export async function verify(
|
|
|
144
156
|
* Creates an Assertion object with the specified properties.
|
|
145
157
|
*/
|
|
146
158
|
export async function CreateAssertion(
|
|
147
|
-
aggregateHash:
|
|
159
|
+
aggregateHash: Uint8Array,
|
|
148
160
|
assertionConfig: AssertionConfig
|
|
149
161
|
): Promise<Assertion> {
|
|
150
162
|
if (!assertionConfig.signingKey) {
|
|
@@ -162,8 +174,11 @@ export async function CreateAssertion(
|
|
|
162
174
|
};
|
|
163
175
|
|
|
164
176
|
const assertionHash = await hash(a);
|
|
165
|
-
const combinedHash =
|
|
166
|
-
|
|
177
|
+
const combinedHash = concatenateUint8Arrays(
|
|
178
|
+
aggregateHash,
|
|
179
|
+
new Uint8Array(hex.decodeArrayBuffer(assertionHash))
|
|
180
|
+
);
|
|
181
|
+
const encodedHash = base64.encodeArrayBuffer(combinedHash);
|
|
167
182
|
|
|
168
183
|
return await sign(a, assertionHash, encodedHash, assertionConfig.signingKey);
|
|
169
184
|
}
|
|
@@ -189,3 +204,13 @@ export type AssertionVerificationKeys = {
|
|
|
189
204
|
DefaultKey?: AssertionKey;
|
|
190
205
|
Keys: Record<string, AssertionKey>;
|
|
191
206
|
};
|
|
207
|
+
|
|
208
|
+
function concatenateUint8Arrays(array1: Uint8Array, array2: Uint8Array): Uint8Array {
|
|
209
|
+
const combinedLength = array1.length + array2.length;
|
|
210
|
+
const combinedArray = new Uint8Array(combinedLength);
|
|
211
|
+
|
|
212
|
+
combinedArray.set(array1, 0);
|
|
213
|
+
combinedArray.set(array2, array1.length);
|
|
214
|
+
|
|
215
|
+
return combinedArray;
|
|
216
|
+
}
|
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import { EventEmitter } from 'eventemitter3';
|
|
2
|
-
import streamSaver from 'streamsaver';
|
|
3
|
-
import { fileSave } from 'browser-fs-access';
|
|
4
|
-
import { isFirefox } from '../../../src/utils.js';
|
|
5
|
-
|
|
6
1
|
import { type Metadata } from '../tdf.js';
|
|
7
2
|
import { type Manifest } from '../models/index.js';
|
|
8
|
-
import { ConfigurationError } from '../../../src/errors.js';
|
|
9
3
|
|
|
10
4
|
export async function streamToBuffer(stream: ReadableStream<Uint8Array>): Promise<Uint8Array> {
|
|
11
5
|
const accumulator = await new Response(stream).arrayBuffer();
|
|
@@ -24,9 +18,6 @@ export class DecoratedReadableStream {
|
|
|
24
18
|
tdfSize: number;
|
|
25
19
|
fileSize: number | undefined;
|
|
26
20
|
stream: ReadableStream<Uint8Array>;
|
|
27
|
-
ee: EventEmitter;
|
|
28
|
-
on: EventEmitter['on'];
|
|
29
|
-
emit: EventEmitter['emit'];
|
|
30
21
|
metadata?: Metadata;
|
|
31
22
|
manifest: Manifest;
|
|
32
23
|
fileStreamServiceWorker?: string;
|
|
@@ -42,23 +33,10 @@ export class DecoratedReadableStream {
|
|
|
42
33
|
this.stream = new ReadableStream(underlyingSource, {
|
|
43
34
|
highWaterMark: 1,
|
|
44
35
|
}) as ReadableStream<Uint8Array>;
|
|
45
|
-
this.ee = new EventEmitter();
|
|
46
|
-
this.on = (...args) => this.ee.on(...args);
|
|
47
|
-
this.emit = (...args) => this.ee.emit(...args);
|
|
48
36
|
}
|
|
49
37
|
|
|
50
38
|
async getMetadata() {
|
|
51
|
-
return
|
|
52
|
-
if (this.metadata) {
|
|
53
|
-
resolve(this.metadata);
|
|
54
|
-
} else {
|
|
55
|
-
this.on('error', reject);
|
|
56
|
-
this.on('rewrap', (rewrapResponse: Metadata) => {
|
|
57
|
-
this.metadata = rewrapResponse;
|
|
58
|
-
resolve(rewrapResponse);
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
});
|
|
39
|
+
return this.metadata;
|
|
62
40
|
}
|
|
63
41
|
|
|
64
42
|
/**
|
|
@@ -82,66 +60,12 @@ export class DecoratedReadableStream {
|
|
|
82
60
|
async toString(): Promise<string> {
|
|
83
61
|
return new Response(this.stream).text();
|
|
84
62
|
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Dump the stream content to a local file. This will consume the stream.
|
|
88
|
-
*
|
|
89
|
-
* @param filepath The path of the local file to write plaintext to.
|
|
90
|
-
* @param encoding The charset encoding to use. Defaults to utf-8.
|
|
91
|
-
*/
|
|
92
|
-
async toFile(
|
|
93
|
-
filepath = 'download.tdf',
|
|
94
|
-
options?: BufferEncoding | DecoratedReadableStreamSinkOptions
|
|
95
|
-
): Promise<void> {
|
|
96
|
-
if (options && typeof options === 'string') {
|
|
97
|
-
throw new ConfigurationError('unsupported operation: Cannot set encoding in browser');
|
|
98
|
-
}
|
|
99
|
-
if (isFirefox()) {
|
|
100
|
-
await fileSave(new Response(this.stream), {
|
|
101
|
-
fileName: filepath,
|
|
102
|
-
extensions: [`.${filepath.split('.').pop()}`],
|
|
103
|
-
});
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (this.fileStreamServiceWorker) {
|
|
108
|
-
streamSaver.mitm = this.fileStreamServiceWorker;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const fileStream = streamSaver.createWriteStream(filepath, {
|
|
112
|
-
writableStrategy: { highWaterMark: 1 },
|
|
113
|
-
readableStrategy: { highWaterMark: 1 },
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
if (WritableStream) {
|
|
117
|
-
return this.stream.pipeTo(fileStream, options);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Write (pipe) manually
|
|
121
|
-
const reader = this.stream.getReader();
|
|
122
|
-
const writer = fileStream.getWriter();
|
|
123
|
-
const pump = async (): Promise<void> => {
|
|
124
|
-
const res = await reader.read();
|
|
125
|
-
|
|
126
|
-
if (res.done) {
|
|
127
|
-
return await writer.close();
|
|
128
|
-
} else {
|
|
129
|
-
await writer.write(res.value);
|
|
130
|
-
return pump();
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
return pump();
|
|
134
|
-
|
|
135
|
-
// const pump = (): Promise<void> =>
|
|
136
|
-
// reader.read().then((res) => (res.done ? writer.close() : writer.write(res.value).then(pump)));
|
|
137
|
-
// pump();
|
|
138
|
-
}
|
|
139
63
|
}
|
|
140
64
|
|
|
141
65
|
export function isDecoratedReadableStream(s: unknown): s is DecoratedReadableStream {
|
|
142
66
|
return (
|
|
67
|
+
typeof (s as DecoratedReadableStream)?.stream !== 'undefined' &&
|
|
143
68
|
typeof (s as DecoratedReadableStream)?.toBuffer !== 'undefined' &&
|
|
144
|
-
typeof (s as DecoratedReadableStream)?.toFile !== 'undefined' &&
|
|
145
69
|
typeof (s as DecoratedReadableStream)?.toString !== 'undefined'
|
|
146
70
|
);
|
|
147
71
|
}
|
|
@@ -6,9 +6,10 @@ import { Binary } from '../binary.js';
|
|
|
6
6
|
import { ConfigurationError } from '../../../src/errors.js';
|
|
7
7
|
import { PemKeyPair } from '../crypto/declarations.js';
|
|
8
8
|
import { DecoratedReadableStream } from './DecoratedReadableStream.js';
|
|
9
|
-
import { type Chunker } from '
|
|
9
|
+
import { type Chunker } from '../../../src/seekable.js';
|
|
10
10
|
import { AssertionConfig, AssertionVerificationKeys } from '../assertions.js';
|
|
11
11
|
import { Value } from '../../../src/policy/attributes.js';
|
|
12
|
+
import { KasPublicKeyAlgorithm, OriginAllowList } from '../../../src/access.js';
|
|
12
13
|
|
|
13
14
|
export const DEFAULT_SEGMENT_SIZE: number = 1024 * 1024;
|
|
14
15
|
export type Scope = {
|
|
@@ -34,16 +35,14 @@ export type SplitStep = {
|
|
|
34
35
|
};
|
|
35
36
|
|
|
36
37
|
export type EncryptParams = {
|
|
38
|
+
byteLimit?: number;
|
|
37
39
|
source: ReadableStream<Uint8Array>;
|
|
38
40
|
opts?: { keypair: PemKeyPair };
|
|
39
41
|
autoconfigure?: boolean;
|
|
40
42
|
scope?: Scope;
|
|
41
43
|
metadata?: Metadata;
|
|
42
44
|
keypair?: CryptoKeyPair;
|
|
43
|
-
// Deprecated: Only offline more is currently supported
|
|
44
|
-
offline?: boolean;
|
|
45
45
|
windowSize?: number;
|
|
46
|
-
asHtml?: boolean;
|
|
47
46
|
getPolicyId?: () => Scope['policyId'];
|
|
48
47
|
mimeType?: string;
|
|
49
48
|
payloadKey?: Binary;
|
|
@@ -51,10 +50,18 @@ export type EncryptParams = {
|
|
|
51
50
|
splitPlan?: SplitStep[];
|
|
52
51
|
streamMiddleware?: EncryptStreamMiddleware;
|
|
53
52
|
assertionConfigs?: AssertionConfig[];
|
|
53
|
+
defaultKASEndpoint?: string;
|
|
54
|
+
|
|
55
|
+
// Preferred wrapping key algorithm. Used when KID resolution is not available.
|
|
56
|
+
wrappingKeyAlgorithm?: KasPublicKeyAlgorithm;
|
|
57
|
+
|
|
58
|
+
// Unsupported
|
|
59
|
+
asHtml?: boolean;
|
|
60
|
+
// Unsupported
|
|
61
|
+
offline?: boolean;
|
|
54
62
|
};
|
|
55
63
|
|
|
56
64
|
// 'Readonly<EncryptParams>': scope, metadata, offline, windowSize, asHtml
|
|
57
|
-
|
|
58
65
|
// deep copy is expensive, could be faster is Immer used, but to keep SDK work
|
|
59
66
|
// stable we can just make this object readonly
|
|
60
67
|
function freeze<Type>(obj: Type): Readonly<Type> {
|
|
@@ -75,9 +82,7 @@ class EncryptParamsBuilder {
|
|
|
75
82
|
attributes: [],
|
|
76
83
|
},
|
|
77
84
|
keypair: undefined,
|
|
78
|
-
offline: false,
|
|
79
85
|
windowSize: DEFAULT_SEGMENT_SIZE,
|
|
80
|
-
asHtml: false,
|
|
81
86
|
assertionConfigs: [],
|
|
82
87
|
}
|
|
83
88
|
) {
|
|
@@ -93,6 +98,11 @@ class EncryptParamsBuilder {
|
|
|
93
98
|
* @param {Readable} readStream - a Readable Stream to encrypt.
|
|
94
99
|
*/
|
|
95
100
|
setStreamSource(readStream: ReadableStream<Uint8Array>) {
|
|
101
|
+
if (!readStream?.getReader) {
|
|
102
|
+
throw new ConfigurationError(
|
|
103
|
+
`Source must be a WebReadableStream. Run node streams through stream.Readable.toWeb()`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
96
106
|
this._params.source = readStream;
|
|
97
107
|
}
|
|
98
108
|
|
|
@@ -117,6 +127,9 @@ class EncryptParamsBuilder {
|
|
|
117
127
|
* @param {string} string - a string to encrypt.
|
|
118
128
|
*/
|
|
119
129
|
setStringSource(string: string) {
|
|
130
|
+
if (!(string && typeof string === 'string')) {
|
|
131
|
+
throw new ConfigurationError('StringSource must be a string');
|
|
132
|
+
}
|
|
120
133
|
const stream = new ReadableStream({
|
|
121
134
|
pull(controller) {
|
|
122
135
|
controller.enqueue(new TextEncoder().encode(string));
|
|
@@ -382,37 +395,24 @@ class EncryptParamsBuilder {
|
|
|
382
395
|
}
|
|
383
396
|
|
|
384
397
|
/**
|
|
385
|
-
*
|
|
386
|
-
* double click and read using the Virtru Secure Reader, at the cost of reduced space efficiency.
|
|
387
|
-
* <br/><br/>
|
|
388
|
-
* This is enabled by default.
|
|
389
|
-
* @return {boolean} true if the encrypted data will be in html format.
|
|
398
|
+
* @deprecated This feature is not supported
|
|
390
399
|
*/
|
|
391
400
|
hasHtmlFormat(): boolean {
|
|
392
|
-
return
|
|
401
|
+
return false;
|
|
393
402
|
}
|
|
394
403
|
|
|
395
404
|
/**
|
|
396
|
-
*
|
|
397
|
-
* double click and read using the Virtru Secure Reader, at the cost of reduced space efficiency.
|
|
398
|
-
* <br/><br/>
|
|
399
|
-
* This is enabled by default.
|
|
405
|
+
* @deprecated This feature is not supported
|
|
400
406
|
*/
|
|
401
407
|
setHtmlFormat() {
|
|
402
|
-
|
|
408
|
+
throw new ConfigurationError('HTML format is not supported');
|
|
403
409
|
}
|
|
404
410
|
|
|
405
411
|
/**
|
|
406
|
-
*
|
|
407
|
-
* double click and read using the Virtru Secure Reader, at the cost of reduced space efficiency.
|
|
408
|
-
* Returns this object for method chaining.
|
|
409
|
-
* <br/><br/>
|
|
410
|
-
* This is enabled by default.
|
|
411
|
-
* @return {EncryptParamsBuilder} - this object.
|
|
412
|
+
* @deprecated This feature is not supported
|
|
412
413
|
*/
|
|
413
414
|
withHtmlFormat(): EncryptParamsBuilder {
|
|
414
|
-
|
|
415
|
-
return this;
|
|
415
|
+
throw new ConfigurationError('HTML format is not supported');
|
|
416
416
|
}
|
|
417
417
|
|
|
418
418
|
/**
|
|
@@ -514,11 +514,13 @@ export type DecryptSource =
|
|
|
514
514
|
|
|
515
515
|
export type DecryptParams = {
|
|
516
516
|
source: DecryptSource;
|
|
517
|
+
allowList?: OriginAllowList;
|
|
517
518
|
keyMiddleware?: DecryptKeyMiddleware;
|
|
518
519
|
streamMiddleware?: DecryptStreamMiddleware;
|
|
519
520
|
assertionVerificationKeys?: AssertionVerificationKeys;
|
|
520
521
|
concurrencyLimit?: number;
|
|
521
522
|
noVerifyAssertions?: boolean;
|
|
523
|
+
wrappingKeyAlgorithm?: KasPublicKeyAlgorithm;
|
|
522
524
|
};
|
|
523
525
|
|
|
524
526
|
/**
|
|
@@ -637,6 +639,9 @@ class DecryptParamsBuilder {
|
|
|
637
639
|
* @param source (node) the path of the local file to decrypt, or the Blob (browser/node)
|
|
638
640
|
*/
|
|
639
641
|
setFileSource(source: Blob) {
|
|
642
|
+
if (!(source instanceof Blob)) {
|
|
643
|
+
throw new ConfigurationError('File source must be a Blob');
|
|
644
|
+
}
|
|
640
645
|
this._params.source = { type: 'file-browser', location: source };
|
|
641
646
|
}
|
|
642
647
|
|
|
@@ -680,6 +685,19 @@ class DecryptParamsBuilder {
|
|
|
680
685
|
return this;
|
|
681
686
|
}
|
|
682
687
|
|
|
688
|
+
/**
|
|
689
|
+
* Sets the assertion verification keys for the decryption parameters.
|
|
690
|
+
*
|
|
691
|
+
* @param {AssertionVerificationKeys} assertionVerificationKeys - An array of assertion configurations to be set.
|
|
692
|
+
* @returns {DecryptParamsBuilder} The current instance of the EncryptParamsBuilder for method chaining.
|
|
693
|
+
*/
|
|
694
|
+
withAssertionVerificationKeys(
|
|
695
|
+
assertionVerificationKeys: AssertionVerificationKeys
|
|
696
|
+
): DecryptParamsBuilder {
|
|
697
|
+
this._params.assertionVerificationKeys = assertionVerificationKeys;
|
|
698
|
+
return this;
|
|
699
|
+
}
|
|
700
|
+
|
|
683
701
|
_deepCopy(_params: DecryptParams) {
|
|
684
702
|
return freeze({ ..._params });
|
|
685
703
|
}
|