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/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 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)); 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; } 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",