Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cosmos] remove react-native runtime dependency on isomorphic-webcrypto #23156

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/cosmosdb/cosmos/src/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.
import { setAuthorizationHeader } from "../auth";
import { Constants, HTTPMethod, jsonStringifyAndEscapeNonASCII, ResourceType } from "../common";
import { defaultLogger } from "../common/logger";
import { CosmosClientOptions } from "../CosmosClientOptions";
import { PartitionKey } from "../documents";
import { CosmosHeaders } from "../queryExecutionContext";
Expand Down
4 changes: 2 additions & 2 deletions sdk/cosmosdb/cosmos/src/utils/digest.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Licensed under the MIT license.

import { encodeUTF8 } from "./encode";
import { globalCrypto } from "./globalCrypto";
import { getGlobalCrypto } from "./globalCrypto";

export async function digest(str: string): Promise<string> {
const data = encodeUTF8(str);
const hash = await globalCrypto.subtle.digest("SHA-256", data);
const hash = await getGlobalCrypto().subtle.digest("SHA-256", data);
return bufferToHex(hash);
}

Expand Down
8 changes: 0 additions & 8 deletions sdk/cosmosdb/cosmos/src/utils/globalCrypto.native.ts

This file was deleted.

27 changes: 17 additions & 10 deletions sdk/cosmosdb/cosmos/src/utils/globalCrypto.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

// eslint-disable-next-line @azure/azure-sdk/ts-no-window
const globalRef: any = typeof self === "undefined" ? window : self;
let globalCrypto: Crypto | undefined;

if (!globalRef) {
throw new Error("Could not find global");
}
export function getGlobalCrypto(): Crypto {
if (globalCrypto) {
return globalCrypto;
}
// eslint-disable-next-line @azure/azure-sdk/ts-no-window
const globalRef: any = typeof self === "undefined" ? window : self;

const globalCrypto: Crypto = globalRef.crypto || globalRef.msCrypto;
if (!globalRef) {
throw new Error("Could not find global");
}

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

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

return globalCrypto;
}
14 changes: 9 additions & 5 deletions sdk/cosmosdb/cosmos/src/utils/hmac.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@

import { encodeUTF8, encodeBase64 } from "./encode";
import atob from "./atob";
import { globalCrypto } from "./globalCrypto";
import { getGlobalCrypto } from "./globalCrypto";

export async function hmac(key: string, message: string): Promise<string> {
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 globalCrypto.subtle.importKey("raw", encodedKey, importParams, false, [
"sign",
]);
const signature = await globalCrypto.subtle.sign(importParams, cryptoKey, encodedMessage);
const cryptoKey = await getGlobalCrypto().subtle.importKey(
"raw",
encodedKey,
importParams,
false,
["sign"]
);
const signature = await getGlobalCrypto().subtle.sign(importParams, cryptoKey, encodedMessage);

return encodeBase64(signature);
}