Skip to content

Commit

Permalink
[appconf] use SHA-256 helpers from core-util instead (Azure#21715)
Browse files Browse the repository at this point in the history
and remove its own helpers.
  • Loading branch information
jeremymeng authored May 4, 2022
1 parent a28ea00 commit ebbfcff
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 74 deletions.
2 changes: 2 additions & 0 deletions sdk/appconfiguration/app-configuration/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
- Notable changes include Removal of `@opentelemetry/api` as a transitive dependency and ensuring that the active context is properly propagated.
- Customers who would like to continue using OpenTelemetry driven tracing should visit our [OpenTelemetry Instrumentation](https://www.npmjs.com/package/@azure/opentelemetry-instrumentation-azure-sdk) package for instructions.

- Move to depend on `@azure/core-util` for SHA256 Digest and HMAC computing.

## 1.3.1 (2021-12-14)

### Bugs Fixed
Expand Down
4 changes: 1 addition & 3 deletions sdk/appconfiguration/app-configuration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"browser": {
"./dist-esm/src/internal/cryptoHelpers.js": "./dist-esm/src/internal/cryptoHelpers.browser.js"
},
"react-native": {
"./dist/index.js": "./dist-esm/src/index.js"
},
Expand Down Expand Up @@ -94,6 +91,7 @@
"@azure/core-rest-pipeline": "^1.6.0",
"@azure/core-tracing": "^1.0.0",
"@azure/core-auth": "^1.3.0",
"@azure/core-util": "^1.0.0",
"tslib": "^2.2.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PipelineResponse,
SendRequest,
} from "@azure/core-rest-pipeline";
import { sha256Digest, sha256Hmac } from "./internal/cryptoHelpers";
import { computeSha256Hash, computeSha256Hmac } from "@azure/core-util";

/**
* Create an HTTP pipeline policy to authenticate a request
Expand All @@ -19,13 +19,13 @@ export function appConfigKeyCredentialPolicy(credential: string, secret: string)
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
const verb = request.method;
const utcNow = new Date().toUTCString();
const contentHash = await sha256Digest(request.body?.toString() || "");
const contentHash = await computeSha256Hash(request.body?.toString() || "", "base64");
const signedHeaders = "x-ms-date;host;x-ms-content-sha256";
const url = new URL(request.url);
const query = url.search;
const urlPathAndQuery = query ? `${url.pathname}${query}` : url.pathname;
const stringToSign = `${verb}\n${urlPathAndQuery}\n${utcNow};${url.host};${contentHash}`;
const signature = await sha256Hmac(secret, stringToSign);
const signature = await computeSha256Hmac(secret, stringToSign, "base64");

request.headers.set("x-ms-date", utcNow);
request.headers.set("x-ms-content-sha256", contentHash);
Expand Down

This file was deleted.

This file was deleted.

25 changes: 18 additions & 7 deletions sdk/core/core-util/src/sha256.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ function getCrypto(): SubtleCrypto {
return subtleCrypto;
}

const importParams: HmacImportParams = {
name: "HMAC",
hash: { name: "SHA-256" },
};

/**
* Generates a SHA-256 HMAC signature.
* @param key - The HMAC key represented as a base64 string, used to generate the cryptographic HMAC hash.
Expand All @@ -87,8 +82,24 @@ export async function computeSha256Hmac(
const keyBytes = base64ToBytes(key);
const stringToSignBytes = utf8ToBytes(stringToSign);

const cryptoKey = await crypto.importKey("raw", keyBytes, importParams, false, ["sign"]);
const signature = await crypto.sign(importParams, cryptoKey, stringToSignBytes);
const cryptoKey = await crypto.importKey(
"raw",
keyBytes,
{
name: "HMAC",
hash: { name: "SHA-256" },
},
false,
["sign"]
);
const signature = await crypto.sign(
{
name: "HMAC",
hash: { name: "SHA-256" },
},
cryptoKey,
stringToSignBytes
);

switch (encoding) {
case "base64":
Expand Down

0 comments on commit ebbfcff

Please sign in to comment.