UNPKG

@azure/communication-common

Version:
43 lines 2.03 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { shaHMAC, shaHash } from "./cryptoUtils.js"; import { isNodeLike } from "@azure/core-util"; /** * CommunicationKeyCredentialPolicy provides a means of signing requests made through * the SmsClient. */ const communicationAccessKeyCredentialPolicy = "CommunicationAccessKeyCredentialPolicy"; /** * Creates an HTTP pipeline policy to authenticate a request using a `KeyCredential`. * @hidden * * @param credential - The key credential. */ export function createCommunicationAccessKeyCredentialPolicy(credential) { return { name: communicationAccessKeyCredentialPolicy, async sendRequest(request, next) { var _a; const verb = request.method.toUpperCase(); const utcNow = new Date().toUTCString(); const contentHash = await shaHash(((_a = request.body) === null || _a === void 0 ? void 0 : _a.toString()) || ""); const dateHeader = "x-ms-date"; const signedHeaders = `${dateHeader};host;x-ms-content-sha256`; const url = new URL(request.url); const query = url.searchParams.toString(); const urlPathAndQuery = query ? `${url.pathname}?${query}` : url.pathname; const port = url.port; const hostAndPort = port ? `${url.host}:${port}` : url.host; const stringToSign = `${verb}\n${urlPathAndQuery}\n${utcNow};${hostAndPort};${contentHash}`; const signature = await shaHMAC(credential.key, stringToSign); if (isNodeLike) { request.headers.set("Host", hostAndPort || ""); } request.headers.set(dateHeader, utcNow); request.headers.set("x-ms-content-sha256", contentHash); request.headers.set("Authorization", `HMAC-SHA256 SignedHeaders=${signedHeaders}&Signature=${signature}`); return next(request); }, }; } //# sourceMappingURL=communicationAccessKeyCredentialPolicy.js.map