Skip to content

Commit 2e81fcb

Browse files
authored
fix: sequential requests instead of promise.all (#339)
* sequential requests instead of promise.all * 🤖 🎨 Autoformat Signed-off-by: Tyler Biscoe <[email protected]> * add type --------- Signed-off-by: Tyler Biscoe <[email protected]> Co-authored-by: biscoe916 <[email protected]>
1 parent 63721f6 commit 2e81fcb

File tree

1 file changed

+67
-66
lines changed

1 file changed

+67
-66
lines changed

lib/tdf3/src/tdf.ts

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -868,79 +868,80 @@ async function unwrapKey({
868868
let responseMetadata;
869869
const isAppIdProvider = authProvider && isAppIdProviderCheck(authProvider);
870870
// Get key access information to know the KAS URLS
871-
const rewrappedKeys = await Promise.all(
872-
Object.entries(splitPotentials).map(async ([splitId, potentials]) => {
873-
if (!potentials || !Object.keys(potentials).length) {
874-
throw new UnsafeUrlError(
875-
`Unreconstructable key - no valid KAS found for split ${JSON.stringify(splitId)}`,
876-
''
877-
);
878-
}
879-
// If we have multiple ways of getting a value, try the 'best' way
880-
// or maybe retry across all potential ways? Currently, just tries them all
881-
const [keySplitInfo] = Object.values(potentials);
882-
const url = `${keySplitInfo.url}/${isAppIdProvider ? '' : 'v2/'}rewrap`;
871+
const rewrappedKeys: Uint8Array[] = [];
883872

884-
const ephemeralEncryptionKeys = await cryptoService.cryptoToPemPair(
885-
await cryptoService.generateKeyPair()
873+
for (const [splitId, potentials] of Object.entries(splitPotentials)) {
874+
if (!potentials || !Object.keys(potentials).length) {
875+
throw new UnsafeUrlError(
876+
`Unreconstructable key - no valid KAS found for split ${JSON.stringify(splitId)}`,
877+
''
886878
);
887-
const clientPublicKey = ephemeralEncryptionKeys.publicKey;
879+
}
888880

889-
const requestBodyStr = JSON.stringify({
890-
algorithm: 'RS256',
891-
keyAccess: keySplitInfo,
892-
policy: manifest.encryptionInformation.policy,
893-
clientPublicKey,
894-
});
881+
// If we have multiple ways of getting a value, try the 'best' way
882+
// or maybe retry across all potential ways? Currently, just tries them all
883+
const [keySplitInfo] = Object.values(potentials);
884+
const url = `${keySplitInfo.url}/${isAppIdProvider ? '' : 'v2/'}rewrap`;
895885

896-
const jwtPayload = { requestBody: requestBodyStr };
897-
const signedRequestToken = await reqSignature(
898-
isAppIdProvider ? {} : jwtPayload,
899-
dpopKeys.privateKey
900-
);
886+
const ephemeralEncryptionKeys = await cryptoService.cryptoToPemPair(
887+
await cryptoService.generateKeyPair()
888+
);
889+
const clientPublicKey = ephemeralEncryptionKeys.publicKey;
901890

902-
let requestBody;
903-
if (isAppIdProvider) {
904-
requestBody = {
905-
keyAccess: keySplitInfo,
906-
policy: manifest.encryptionInformation.policy,
907-
entity: {
908-
...entity,
909-
publicKey: clientPublicKey,
910-
},
911-
authToken: signedRequestToken,
912-
};
913-
} else {
914-
requestBody = {
915-
signedRequestToken,
916-
};
917-
}
891+
const requestBodyStr = JSON.stringify({
892+
algorithm: 'RS256',
893+
keyAccess: keySplitInfo,
894+
policy: manifest.encryptionInformation.policy,
895+
clientPublicKey,
896+
});
918897

919-
// Create a PoP token by signing the body so KAS knows we actually have a private key
920-
// Expires in 60 seconds
921-
const httpReq = await authProvider.withCreds(buildRequest('POST', url, requestBody));
898+
const jwtPayload = { requestBody: requestBodyStr };
899+
const signedRequestToken = await reqSignature(
900+
isAppIdProvider ? {} : jwtPayload,
901+
dpopKeys.privateKey
902+
);
922903

923-
try {
924-
// The response from KAS on a rewrap
925-
const {
926-
data: { entityWrappedKey, metadata },
927-
} = await axios.post(httpReq.url, httpReq.body, { headers: httpReq.headers });
928-
responseMetadata = metadata;
929-
const key = Binary.fromString(base64.decode(entityWrappedKey));
930-
const decryptedKeyBinary = await cryptoService.decryptWithPrivateKey(
931-
key,
932-
ephemeralEncryptionKeys.privateKey
933-
);
934-
return new Uint8Array(decryptedKeyBinary.asByteArray());
935-
} catch (e) {
936-
console.error(e);
937-
throw new KasDecryptError(
938-
`Unable to decrypt the response from KAS: [${e.name}: ${e.message}], response: [${e?.response?.body}]`,
939-
e
940-
);
941-
}
942-
})
943-
);
904+
let requestBody;
905+
if (isAppIdProvider) {
906+
requestBody = {
907+
keyAccess: keySplitInfo,
908+
policy: manifest.encryptionInformation.policy,
909+
entity: {
910+
...entity,
911+
publicKey: clientPublicKey,
912+
},
913+
authToken: signedRequestToken,
914+
};
915+
} else {
916+
requestBody = {
917+
signedRequestToken,
918+
};
919+
}
920+
921+
// Create a PoP token by signing the body so KAS knows we actually have a private key
922+
// Expires in 60 seconds
923+
const httpReq = await authProvider.withCreds(buildRequest('POST', url, requestBody));
924+
925+
try {
926+
// The response from KAS on a rewrap
927+
const {
928+
data: { entityWrappedKey, metadata },
929+
} = await axios.post(httpReq.url, httpReq.body, { headers: httpReq.headers });
930+
responseMetadata = metadata;
931+
const key = Binary.fromString(base64.decode(entityWrappedKey));
932+
const decryptedKeyBinary = await cryptoService.decryptWithPrivateKey(
933+
key,
934+
ephemeralEncryptionKeys.privateKey
935+
);
936+
rewrappedKeys.push(new Uint8Array(decryptedKeyBinary.asByteArray()));
937+
} catch (e) {
938+
console.error(e);
939+
throw new KasDecryptError(
940+
`Unable to decrypt the response from KAS: [${e.name}: ${e.message}], response: [${e?.response?.body}]`,
941+
e
942+
);
943+
}
944+
}
944945

945946
// Merge the unwrapped keys from each KAS
946947
const reconstructedKey = keyMerge(rewrappedKeys);

0 commit comments

Comments
 (0)