From 66e2c30e69c24b52a43fd2ed6c82e0d2c6b20e4c Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 24 Sep 2020 14:30:04 -0700 Subject: [PATCH 1/4] Make buildTenantsList more resilient to error responses --- lib/login.ts | 8 ++++++-- lib/subscriptionManagement/subscriptionUtils.ts | 10 +++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/login.ts b/lib/login.ts index 756876d..6a9c08e 100644 --- a/lib/login.ts +++ b/lib/login.ts @@ -173,10 +173,14 @@ export async function withUsernamePasswordWithAuthResponse(username: string, pas } const creds = new UserTokenCredentials(options.clientId, options.domain, username, password, options.tokenAudience, options.environment); - await creds.getToken(); + const tokenResponse = await creds.getToken(); // The token cache gets propulated for all the tenants as a part of building the tenantList. - const tenantList = await buildTenantList(creds); + let tenantList = await buildTenantList(creds); + if (tenantList.length === 0 && tokenResponse.tenantId) { + tenantList = [tokenResponse.tenantId]; + } + const subscriptionList: LinkedSubscription[] = await _getSubscriptions(creds, tenantList, options.tokenAudience); return { credentials: creds, subscriptions: subscriptionList }; diff --git a/lib/subscriptionManagement/subscriptionUtils.ts b/lib/subscriptionManagement/subscriptionUtils.ts index 679febc..8d0edd0 100644 --- a/lib/subscriptionManagement/subscriptionUtils.ts +++ b/lib/subscriptionManagement/subscriptionUtils.ts @@ -90,10 +90,14 @@ export async function buildTenantList(credentials: TokenCredentialsBase, apiVers }; const res = await client.sendRequest(req); const result: string[] = []; - const tenants: any = res.parsedBody; - for (const tenant of tenants.value) { - result.push(tenant.tenantId); + + if (res.status < 300) { + const tenants: any = res.parsedBody; + for (const tenant of tenants.value) { + result.push(tenant.tenantId); + } } + return result; } From efa32aec1b0ef98b727da421ab404135d3f243ea Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 24 Sep 2020 14:58:57 -0700 Subject: [PATCH 2/4] Add existing token example to README.md --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index f07bbd4..1a79d7b 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,26 @@ function getUrl(subscriptionId: string): string { main(); ``` +### Authenticating with an existing token + +If you have acquired a valid Azure Active Directory token from another source, you can use it to authenticate with Azure SDK libraries using the following code snippet: + +```js +const { HttpHeaders } = require("@azure/ms-rest-js"); + +function getCredentialForToken(accessToken) { + return { + signRequest: (request) => { + if (!request.headers) request.headers = new HttpHeaders(); + request.headers.set("Authorization", `Bearer ${accessToken}`); + return Promise.resolve(request); + } + }; +} + +const creds = getCredentialForToken("your existing token"); +``` + ### Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a From a742b582bbd34af3b9cb88a5adc1e12cfa604384 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 24 Sep 2020 15:39:57 -0700 Subject: [PATCH 3/4] Fix compilation issue with Date constructor --- lib/credentials/msiTokenCredentials.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/credentials/msiTokenCredentials.ts b/lib/credentials/msiTokenCredentials.ts index d60172f..a6c949d 100644 --- a/lib/credentials/msiTokenCredentials.ts +++ b/lib/credentials/msiTokenCredentials.ts @@ -116,7 +116,7 @@ export abstract class MSITokenCredentials implements TokenClientCredentials { if (typeof parsedBody["expires_on"] === "string") { // possibly a Date string '09/14/2017 00:00:00 PM +00:00' if (parsedBody["expires_on"].includes(":") || parsedBody["expires_on"].includes("/")) { - parsedBody.expiresOn = new Date(parsedBody["expires_on"], 10); + parsedBody.expiresOn = new Date(parsedBody["expires_on"]); } else { // normal number as a string '1504130527' parsedBody.expiresOn = new Date(parseInt(parsedBody["expires_on"], 10)); From acd0d2e374e059811748cbb80bdf6ebce07cbc6b Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 24 Sep 2020 15:47:01 -0700 Subject: [PATCH 4/4] Bump version to 2.0.6 an update changelog --- Changelog.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 3d2f241..2d5014e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,10 @@ # Changelog +## 2.0.6 - 2020/09/24 + +- Fixed a bug where `buildTenantsList` will throw an error when it can't list tenants +- Added instructions for authenticating with an existing token + ## 2.0.5 - 2019/08/22 - Fixed a bug where the callback to `loginWithServicePrincipalSecretWithAuthResponse` is sometimes not called. - Fix bug prevent tenant IDs from being discovered on auth diff --git a/package.json b/package.json index c121884..d0d4f3a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "email": "azsdkteam@microsoft.com", "url": "https://github.com/Azure/ms-rest-nodeauth" }, - "version": "2.0.5", + "version": "2.0.6", "description": "Azure Authentication library in node.js with type definitions.", "keywords": [ "node",