Skip to content

Commit

Permalink
[Cosmos] Support browser crypto in Internet Explorer (Azure#8769)
Browse files Browse the repository at this point in the history
* [Cosmos] Support browser crypto in Internet Explorer

* Casing

* Update sdk/cosmosdb/cosmos/src/utils/globalCrypto.ts

Co-authored-by: Zachary Foster <zfoster@users.noreply.github.com>

Co-authored-by: Zachary Foster <zfoster@users.noreply.github.com>
  • Loading branch information
southpolesteve and zfoster authored May 7, 2020
1 parent a98996d commit 1862965
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
7 changes: 4 additions & 3 deletions sdk/cosmosdb/cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Release History

## 3.6.4 (Unreleased)
## 3.7.0 (Unreleased)

- FEATURE: Allows string value `partitionKey` parameter when creating containers.
- FEATURE: Allows string value `partitionKey` parameter when creating containers
- BUGFIX: Support crypto functions in Internet Explorer browser

The following result in the same behavior:

Expand Down Expand Up @@ -67,7 +68,7 @@ Based on customer feedback, we identified scenarios where it still makes sense t
## 3.5.3 (2020-1-06)

- BUG FIX: maxDegreeOfParallelism was defaulting to 1 and should default to the number of partitions of the collection
- BUF FIX: maxItemCount was defaulting to 10 and should default to undefined
- BUG FIX: maxItemCount was defaulting to 10 and should default to undefined
- Set default TLS version to 1.2 (#6761)
- Use tslib 1.10.0 (#6710)
- Add partition key to code sample (#6612)
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure/cosmos",
"version": "3.6.4",
"version": "3.7.0",
"description": "Microsoft Azure Cosmos DB Service Node.js SDK for SQL API",
"sdk-type": "client",
"keywords": [
Expand Down
9 changes: 2 additions & 7 deletions sdk/cosmosdb/cosmos/src/utils/digest.browser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { encodeUTF8 } from "./encode";

const globalThis = typeof self === "undefined" ? window : self;
import { globalCrypto } from "./globalCrypto";

export async function digest(str: string) {
if (!globalThis || !globalThis.crypto || !globalThis.crypto.subtle) {
throw new Error("Browser does not support cryptography functions");
}

const data = encodeUTF8(str);
const hash = await globalThis.crypto.subtle.digest("SHA-256", data);
const hash = await globalCrypto.subtle.digest("SHA-256", data);
return bufferToHex(hash);
}

Expand Down
13 changes: 13 additions & 0 deletions sdk/cosmosdb/cosmos/src/utils/globalCrypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const globalRef: any = typeof self === "undefined" ? window : self;

if (!globalThis) {
throw new Error("Could not find global");
}

const globalCrypto: Crypto = globalRef.crypto || globalRef.msCrypto;

if (!globalCrypto || !globalCrypto.subtle) {
throw new Error("Browser does not support cryptography functions");
}

export { globalCrypto };
18 changes: 5 additions & 13 deletions sdk/cosmosdb/cosmos/src/utils/hmac.browser.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import { encodeUTF8, encodeBase64 } from "./encode";
import atob from "./atob";

const globalThis = typeof self === "undefined" ? window : self;
import { globalCrypto } from "./globalCrypto";

export async function hmac(key: string, message: string) {
if (!globalThis || !globalThis.crypto || !globalThis.crypto.subtle) {
throw new Error("Browser does not support cryptography functions");
}
const importParams: HmacImportParams = { name: "HMAC", hash: { name: "SHA-256" } };
const encodedMessage = new Uint8Array(
[...unescape(encodeURIComponent(message))].map((c) => c.charCodeAt(0))
);
const encodedKey = encodeUTF8(atob(key));
const cryptoKey = await globalThis.crypto.subtle.importKey(
"raw",
encodedKey,
importParams,
false,
["sign"]
);
const signature = await globalThis.crypto.subtle.sign(importParams, cryptoKey, encodedMessage);
const cryptoKey = await globalCrypto.subtle.importKey("raw", encodedKey, importParams, false, [
"sign"
]);
const signature = await globalCrypto.subtle.sign(importParams, cryptoKey, encodedMessage);

return encodeBase64(signature);
}

0 comments on commit 1862965

Please sign in to comment.