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

Adding support for @azure/identity in v1 #418

Merged
merged 4 commits into from
Feb 2, 2021
Merged
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
5 changes: 4 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## 1.9.1 - UNRELEASED
## 1.10.0 - 2021-02-02
- Add support for @azure/core-auth's TokenCredential (PR [#418](https://github.com/Azure/ms-rest-js/pull/418))

## 1.9.1 - 2021-01-07
- Upgrade axios dependency to fix vulnerability (PR [#407](https://github.com/Azure/ms-rest-js/pull/407))

## 1.9.0 - 2020-10-08
Expand Down
51 changes: 51 additions & 0 deletions lib/credentials/azureIdentityTokenCredentialAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

import { ServiceClientCredentials } from "./serviceClientCredentials";
import { Constants as MSRestConstants } from "../util/constants";
import { WebResource } from "../webResource";

import { TokenCredential } from "@azure/core-auth";
import { TokenResponse } from "./tokenResponse";

const DEFAULT_AUTHORIZATION_SCHEME = "Bearer";

/**
* This class provides a simple extension to use {@link TokenCredential} from `@azure/identity` library to
* use with legacy Azure SDKs that accept {@link ServiceClientCredentials} family of credentials for authentication.
*/
export class AzureIdentityCredentialAdapter
implements ServiceClientCredentials {
private azureTokenCredential: TokenCredential;
private scopes: string | string[];
constructor(
azureTokenCredential: TokenCredential,
scopes: string | string[] = "https://management.azure.com/.default"
) {
this.azureTokenCredential = azureTokenCredential;
this.scopes = scopes;
}

public async getToken(): Promise<TokenResponse> {
const accessToken = await this.azureTokenCredential.getToken(this.scopes);
if (accessToken !== null) {
const result: TokenResponse = {
accessToken: accessToken.token,
tokenType: DEFAULT_AUTHORIZATION_SCHEME,
expiresOn: accessToken.expiresOnTimestamp,
};
return result;
} else {
throw new Error("Could find token for scope");
}
}

public async signRequest(webResource: WebResource) {
const tokenResponse = await this.getToken();
webResource.headers.set(
MSRestConstants.HeaderConstants.AUTHORIZATION,
`${tokenResponse.tokenType} ${tokenResponse.accessToken}`
);
return Promise.resolve(webResource);
}
}
12 changes: 12 additions & 0 deletions lib/credentials/tokenResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

/**
* TokenResponse is defined in `@azure/ms-rest-nodeauth` and is copied here to not
* add an unnecessary dependency.
*/
export interface TokenResponse {
readonly tokenType: string;
readonly accessToken: string;
readonly [x: string]: any;
}
16 changes: 13 additions & 3 deletions lib/serviceClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

import { TokenCredential, isTokenCredential } from "@azure/core-auth";
import { ServiceClientCredentials } from "./credentials/serviceClientCredentials";
import { DefaultHttpClient } from "./defaultHttpClient";
import { HttpClient } from "./httpClient";
Expand Down Expand Up @@ -29,6 +30,7 @@ import { ServiceCallback } from "./util/utils";
import { agentPolicy } from "./policies/agentPolicy";
import { proxyPolicy, getDefaultProxySettings } from "./policies/proxyPolicy";
import { throttlingRetryPolicy } from "./policies/throttlingRetryPolicy";
import { AzureIdentityCredentialAdapter } from "./credentials/azureIdentityTokenCredentialAdapter";
import { Agent } from "http";


Expand Down Expand Up @@ -148,12 +150,20 @@ export class ServiceClient {
* @param {ServiceClientCredentials} [credentials] The credentials object used for authentication.
* @param {ServiceClientOptions} [options] The service client options that govern the behavior of the client.
*/
constructor(credentials?: ServiceClientCredentials, options?: ServiceClientOptions) {
constructor(credentials?: ServiceClientCredentials | TokenCredential, options?: ServiceClientOptions) {
if (!options) {
options = {};
}

if (credentials && !credentials.signRequest) {
let serviceClientCredentials: ServiceClientCredentials | undefined;
if (isTokenCredential(credentials)) {
serviceClientCredentials = new AzureIdentityCredentialAdapter(credentials);
} else {
serviceClientCredentials = credentials;
}


if (serviceClientCredentials && !serviceClientCredentials.signRequest) {
throw new Error("credentials argument needs to implement signRequest method");
}

Expand All @@ -165,7 +175,7 @@ export class ServiceClient {
if (Array.isArray(options.requestPolicyFactories)) {
requestPolicyFactories = options.requestPolicyFactories;
} else {
requestPolicyFactories = createDefaultRequestPolicyFactories(credentials, options);
requestPolicyFactories = createDefaultRequestPolicyFactories(serviceClientCredentials, options);
if (options.requestPolicyFactories) {
const newRequestPolicyFactories: void | RequestPolicyFactory[] = options.requestPolicyFactories(requestPolicyFactories);
if (newRequestPolicyFactories) {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const Constants = {
* @const
* @type {string}
*/
msRestVersion: "1.9.1",
msRestVersion: "1.10.0",

/**
* Specifies HTTP.
Expand Down
43 changes: 39 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"email": "azsdkteam@microsoft.com",
"url": "https://github.com/Azure/ms-rest-js"
},
"version": "1.9.1",
"version": "1.10.0",
"description": "Isomorphic client Runtime for Typescript/node.js/browser javascript client libraries generated using AutoRest",
"tags": [
"isomorphic",
Expand Down Expand Up @@ -60,6 +60,7 @@
"xml2js": "^0.4.19"
},
"devDependencies": {
"@azure/core-auth": "^1.1.4",
"@azure/logger-js": "^1.0.2",
"@ts-common/azure-js-dev-tools": "^15.2.0",
"@types/chai": "^4.1.7",
Expand Down Expand Up @@ -112,7 +113,7 @@
"ts-node": "^7.0.0",
"tslint": "^5.16.0",
"tslint-eslint-rules": "^5.4.0",
"typescript": "^3.4.5",
"typescript": "^3.6.0",
"uglify-js": "^3.4.9",
"webpack": "^4.27.1",
"webpack-cli": "^3.1.2",
Expand Down