Socket
Socket
Sign inDemoInstall

@azure/communication-common

Package Overview
Dependencies
Maintainers
1
Versions
200
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@azure/communication-common - npm Package Compare versions

Comparing version 1.2.0-alpha.20220207.1 to 2.0.0-alpha.20220225.1

dist-esm/src/azureCommunicationTokenCredential.js

5

CHANGELOG.md
# Release History
## 1.2.0 (Unreleased)
## 2.0.0 (Unreleased)

@@ -11,2 +11,5 @@ ### Features Added

- Migrated from using `@azure/core-http` to `@azure/core-rest-pipeline` for the handling of HTTP requests. See [Azure Core v1 vs v2](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-rest-pipeline/documentation/core2.md) for more on the difference and benefits of the move.
- `createCommunicationAccessKeyCredentialPolicy` and `createCommunicationAuthPolicy` newly return `PipelinePolicy` instead of `RequestPolicyFactory`.
### Bugs Fixed

@@ -13,0 +16,0 @@

40

dist-esm/src/communicationTokenCredential.js
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { parseToken } from "./tokenParser";
import { StaticTokenCredential } from "./staticTokenCredential";
import { AutoRefreshTokenCredential, } from "./autoRefreshTokenCredential";
/**
* The CommunicationTokenCredential implementation with support for proactive token refresh.
*/
export class AzureCommunicationTokenCredential {
constructor(tokenOrRefreshOptions) {
this.disposed = false;
if (typeof tokenOrRefreshOptions === "string") {
this.tokenCredential = new StaticTokenCredential(parseToken(tokenOrRefreshOptions));
}
else {
this.tokenCredential = new AutoRefreshTokenCredential(tokenOrRefreshOptions);
}
}
/**
* Gets an `AccessToken` for the user. Throws if already disposed.
* @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.
*/
async getToken(options) {
this.throwIfDisposed();
const token = await this.tokenCredential.getToken(options);
this.throwIfDisposed();
return token;
}
/**
* Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.
*/
dispose() {
this.disposed = true;
this.tokenCredential.dispose();
}
throwIfDisposed() {
if (this.disposed) {
throw new Error("User credential is disposed");
}
}
}
export {};
//# sourceMappingURL=communicationTokenCredential.js.map
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { isTokenCredential } from "@azure/core-auth";
import { URLBuilder } from "@azure/core-http";
import { parseConnectionString } from "./connectionString";
const isValidEndpoint = (host) => {
var _a;
const url = URLBuilder.parse(host);
return (!!((_a = url.getScheme()) === null || _a === void 0 ? void 0 : _a.match(/^http[s]?/)) &&
url.getHost() !== undefined &&
url.getHost() !== "" &&
(url.getPath() === undefined || url.getPath() === "" || url.getPath() === "/"));
const url = new URL(host);
return (!!((_a = url.protocol) === null || _a === void 0 ? void 0 : _a.match(/^http[s]?/)) &&
url.host !== undefined &&
url.host !== "" &&
(url.pathname === undefined || url.pathname === "" || url.pathname === "/"));
};

@@ -14,0 +13,0 @@ const assertValidEndpoint = (host) => {

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { URLBuilder, isNode, BaseRequestPolicy, } from "@azure/core-http";
import { shaHash, shaHMAC } from "./cryptoUtils";
import { shaHMAC, shaHash } from "./cryptoUtils";
import { isNode } from "./isNode";
/**
* 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`.

@@ -11,59 +16,29 @@ * @hidden

*/
export const createCommunicationAccessKeyCredentialPolicy = (credential) => {
export function createCommunicationAccessKeyCredentialPolicy(credential) {
return {
create: (nextpolicy, options) => {
return new CommunicationAccessKeyCredentialPolicy(credential, nextpolicy, options);
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;
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 (isNode) {
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);
},
};
};
/**
* CommunicationAccessKeyCredentialPolicy provides a means of signing requests made through
* the SmsClient.
*/
class CommunicationAccessKeyCredentialPolicy extends BaseRequestPolicy {
/**
* Initializes a new instance of the CommunicationAccessKeyCredential class
* using a base64 encoded key.
* @param accessKey - The base64 encoded key to be used for signing.
*/
constructor(accessKey, nextPolicy, options) {
super(nextPolicy, options);
this.accessKey = accessKey;
}
/**
* Signs a request with the provided access key.
*
* @param webResource - The WebResource to be signed.
*/
async signRequest(webResource) {
const verb = webResource.method.toUpperCase();
const utcNow = new Date().toUTCString();
const contentHash = await shaHash(webResource.body || "");
const dateHeader = "x-ms-date";
const signedHeaders = `${dateHeader};host;x-ms-content-sha256`;
const url = URLBuilder.parse(webResource.url);
const query = url.getQuery();
const urlPathAndQuery = query ? `${url.getPath()}?${query}` : url.getPath();
const port = url.getPort();
const hostAndPort = port ? `${url.getHost()}:${port}` : url.getHost();
const stringToSign = `${verb}\n${urlPathAndQuery}\n${utcNow};${hostAndPort};${contentHash}`;
const signature = await shaHMAC(this.accessKey.key, stringToSign);
if (isNode) {
webResource.headers.set("Host", hostAndPort || "");
}
webResource.headers.set(dateHeader, utcNow);
webResource.headers.set("x-ms-content-sha256", contentHash);
webResource.headers.set("Authorization", `HMAC-SHA256 SignedHeaders=${signedHeaders}&Signature=${signature}`);
return webResource;
}
/**
* Signs the request and calls the next policy in the factory.
*/
async sendRequest(webResource) {
if (!webResource) {
throw new Error("webResource cannot be null or undefined");
}
return this._nextPolicy.sendRequest(await this.signRequest(webResource));
}
}
//# sourceMappingURL=communicationAccessKeyCredentialPolicy.js.map
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { bearerTokenAuthenticationPolicy, } from "@azure/core-rest-pipeline";
import { isTokenCredential } from "@azure/core-auth";
import { bearerTokenAuthenticationPolicy } from "@azure/core-http";
import { createCommunicationAccessKeyCredentialPolicy } from "./communicationAccessKeyCredentialPolicy";

@@ -13,5 +13,9 @@ /**

*/
export const createCommunicationAuthPolicy = (credential) => {
export function createCommunicationAuthPolicy(credential) {
if (isTokenCredential(credential)) {
return bearerTokenAuthenticationPolicy(credential, "https://communication.azure.com//.default");
const policyOptions = {
credential: credential,
scopes: ["https://communication.azure.com//.default"],
};
return bearerTokenAuthenticationPolicy(policyOptions);
}

@@ -21,3 +25,3 @@ else {

}
};
}
//# sourceMappingURL=communicationAuthPolicy.js.map
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { encodeUTF8, encodeBase64, encodeUTF8fromBase64 } from "./encodeUtils.browser";
const globalRef = globalThis;
const getCrypto = () => {
if (!globalRef) {
throw new Error("Could not find global");
}
if (!globalRef.crypto || !globalRef.crypto.subtle) {
throw new Error("Browser does not support cryptography functions");
}
return globalRef.crypto.subtle;
};
var _a, _b;
/// <reference lib="dom" />
import { encodeBase64, encodeUTF8, encodeUTF8fromBase64 } from "./encodeUtils.browser";
const subtle = (_b = (_a = globalThis) === null || _a === void 0 ? void 0 : _a.crypto) === null || _b === void 0 ? void 0 : _b.subtle;
export const shaHash = async (content) => {
const data = encodeUTF8(content);
const hash = await getCrypto().digest("SHA-256", data);
const hash = await subtle.digest("SHA-256", data);
return encodeBase64(hash);

@@ -23,3 +16,3 @@ };

const encodedKey = encodeUTF8fromBase64(secret);
const crypto = getCrypto();
const crypto = subtle;
const cryptoKey = await crypto.importKey("raw", encodedKey, importParams, false, ["sign"]);

@@ -26,0 +19,0 @@ const signature = await crypto.sign(importParams, cryptoKey, encodedMessage);

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
export { AzureCommunicationTokenCredential, } from "./communicationTokenCredential";
export { AzureCommunicationTokenCredential } from "./azureCommunicationTokenCredential";
export * from "./credential";

@@ -5,0 +5,0 @@ export * from "./credential";

@@ -7,4 +7,4 @@ 'use strict';

var tslib = require('tslib');
var coreHttp = require('@azure/core-http');
var crypto = require('crypto');
var coreRestPipeline = require('@azure/core-rest-pipeline');
var coreAuth = require('@azure/core-auth');

@@ -26,19 +26,2 @@

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* StaticTokenCredential
*/
class StaticTokenCredential {
constructor(token) {
this.token = token;
}
async getToken() {
return this.token;
}
dispose() {
/* intentionally empty */
}
}
// Copyright (c) Microsoft Corporation.
const expiredToken = { token: "", expiresOnTimestamp: -10 };

@@ -143,3 +126,20 @@ const minutesToMs = (minutes) => minutes * 1000 * 60;

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* StaticTokenCredential
*/
class StaticTokenCredential {
constructor(token) {
this.token = token;
}
async getToken() {
return this.token;
}
dispose() {
/* intentionally empty */
}
}
// Copyright (c) Microsoft Corporation.
/**
* The CommunicationTokenCredential implementation with support for proactive token refresh.

@@ -189,3 +189,16 @@ */

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
var _a;
/**
* A constant that indicates whether the environment the code is running is Node.JS.
*/
const isNode = typeof process !== "undefined" && Boolean(process.version) && Boolean((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node);
// Copyright (c) Microsoft Corporation.
/**
* 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`.

@@ -196,58 +209,28 @@ * @hidden

*/
const createCommunicationAccessKeyCredentialPolicy = (credential) => {
function createCommunicationAccessKeyCredentialPolicy(credential) {
return {
create: (nextpolicy, options) => {
return new CommunicationAccessKeyCredentialPolicy(credential, nextpolicy, options);
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;
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 (isNode) {
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);
},
};
};
/**
* CommunicationAccessKeyCredentialPolicy provides a means of signing requests made through
* the SmsClient.
*/
class CommunicationAccessKeyCredentialPolicy extends coreHttp.BaseRequestPolicy {
/**
* Initializes a new instance of the CommunicationAccessKeyCredential class
* using a base64 encoded key.
* @param accessKey - The base64 encoded key to be used for signing.
*/
constructor(accessKey, nextPolicy, options) {
super(nextPolicy, options);
this.accessKey = accessKey;
}
/**
* Signs a request with the provided access key.
*
* @param webResource - The WebResource to be signed.
*/
async signRequest(webResource) {
const verb = webResource.method.toUpperCase();
const utcNow = new Date().toUTCString();
const contentHash = await shaHash(webResource.body || "");
const dateHeader = "x-ms-date";
const signedHeaders = `${dateHeader};host;x-ms-content-sha256`;
const url = coreHttp.URLBuilder.parse(webResource.url);
const query = url.getQuery();
const urlPathAndQuery = query ? `${url.getPath()}?${query}` : url.getPath();
const port = url.getPort();
const hostAndPort = port ? `${url.getHost()}:${port}` : url.getHost();
const stringToSign = `${verb}\n${urlPathAndQuery}\n${utcNow};${hostAndPort};${contentHash}`;
const signature = await shaHMAC(this.accessKey.key, stringToSign);
if (coreHttp.isNode) {
webResource.headers.set("Host", hostAndPort || "");
}
webResource.headers.set(dateHeader, utcNow);
webResource.headers.set("x-ms-content-sha256", contentHash);
webResource.headers.set("Authorization", `HMAC-SHA256 SignedHeaders=${signedHeaders}&Signature=${signature}`);
return webResource;
}
/**
* Signs the request and calls the next policy in the factory.
*/
async sendRequest(webResource) {
if (!webResource) {
throw new Error("webResource cannot be null or undefined");
}
return this._nextPolicy.sendRequest(await this.signRequest(webResource));
}
}

@@ -263,5 +246,9 @@

*/
const createCommunicationAuthPolicy = (credential) => {
function createCommunicationAuthPolicy(credential) {
if (coreAuth.isTokenCredential(credential)) {
return coreHttp.bearerTokenAuthenticationPolicy(credential, "https://communication.azure.com//.default");
const policyOptions = {
credential: credential,
scopes: ["https://communication.azure.com//.default"],
};
return coreRestPipeline.bearerTokenAuthenticationPolicy(policyOptions);
}

@@ -271,3 +258,3 @@ else {

}
};
}

@@ -304,7 +291,7 @@ // Copyright (c) Microsoft Corporation.

var _a;
const url = coreHttp.URLBuilder.parse(host);
return (!!((_a = url.getScheme()) === null || _a === void 0 ? void 0 : _a.match(/^http[s]?/)) &&
url.getHost() !== undefined &&
url.getHost() !== "" &&
(url.getPath() === undefined || url.getPath() === "" || url.getPath() === "/"));
const url = new URL(host);
return (!!((_a = url.protocol) === null || _a === void 0 ? void 0 : _a.match(/^http[s]?/)) &&
url.host !== undefined &&
url.host !== "" &&
(url.pathname === undefined || url.pathname === "" || url.pathname === "/"));
};

@@ -311,0 +298,0 @@ const assertValidEndpoint = (host) => {

{
"name": "@azure/communication-common",
"version": "1.2.0-alpha.20220207.1",
"version": "2.0.0-alpha.20220225.1",
"description": "Common package for Azure Communication services.",

@@ -10,3 +10,4 @@ "sdk-type": "client",

"browser": {
"./dist-esm/src/credential/cryptoUtils.js": "./dist-esm/src/credential/cryptoUtils.browser.js"
"./dist-esm/src/credential/cryptoUtils.js": "./dist-esm/src/credential/cryptoUtils.browser.js",
"./dist-esm/src/credential/isNode.js": "./dist-esm/src/credential/isNode.browser.js"
},

@@ -67,3 +68,3 @@ "scripts": {

"@azure/core-auth": "^1.3.0",
"@azure/core-http": "^2.0.0",
"@azure/core-rest-pipeline": "^1.3.2",
"@azure/core-tracing": "1.0.0-preview.13",

@@ -70,0 +71,0 @@ "events": "^3.0.0",

@@ -1,5 +0,5 @@

import { AbortSignalLike } from '@azure/core-http';
import { AccessToken } from '@azure/core-http';
import { AbortSignalLike } from '@azure/abort-controller';
import { AccessToken } from '@azure/core-auth';
import { KeyCredential } from '@azure/core-auth';
import { RequestPolicyFactory } from '@azure/core-http';
import { PipelinePolicy } from '@azure/core-rest-pipeline';
import { TokenCredential } from '@azure/core-auth';

@@ -117,3 +117,3 @@

*/
export declare const createCommunicationAccessKeyCredentialPolicy: (credential: KeyCredential) => RequestPolicyFactory;
export declare function createCommunicationAccessKeyCredentialPolicy(credential: KeyCredential): PipelinePolicy;

@@ -127,3 +127,3 @@ /**

*/
export declare const createCommunicationAuthPolicy: (credential: KeyCredential | TokenCredential) => RequestPolicyFactory;
export declare function createCommunicationAuthPolicy(credential: KeyCredential | TokenCredential): PipelinePolicy;

@@ -130,0 +130,0 @@ /**

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc