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

Add a forward compatible token to use for T1&T2 sdks #418

Merged
merged 12 commits into from
Feb 3, 2022
2 changes: 1 addition & 1 deletion src/login/AuthProviderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import { TokenCredential } from "@azure/core-auth";
import { Environment } from "@azure/ms-rest-azure-env";
import { DeviceTokenCredentials as DeviceTokenCredentials2 } from '@azure/ms-rest-nodeauth';
import { AccountInfo } from "@azure/msal-node";
import { randomBytes } from "crypto";
import { ServerResponse } from "http";
Expand All @@ -17,6 +16,7 @@ import { redirectUrlAAD, redirectUrlADFS } from "../constants";
import { localize } from "../utils/localize";
import { logErrorMessage } from "../utils/logErrorMessage";
import { openUri } from "../utils/openUri";
import { DeviceTokenCredentials2 } from "./adal/DeviceTokenCredentials2";
import { AzureSessionInternal } from "./AzureSessionInternal";
import { getEnvironments } from "./environments";
import { exchangeCodeForToken } from "./exchangeCodeForToken";
Expand Down
6 changes: 3 additions & 3 deletions src/login/adal/AdalAuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/

import { Environment } from "@azure/ms-rest-azure-env";
import { DeviceTokenCredentials as DeviceTokenCredentials2 } from '@azure/ms-rest-nodeauth';
import { Logging, MemoryCache, TokenResponse, UserCodeInfo } from "adal-node";
import { DeviceTokenCredentials } from "ms-rest-azure";
import { AzureSession } from "../../azure-account.api";
Expand All @@ -13,7 +12,8 @@ import { AzureLoginError } from "../../errors";
import { ext } from "../../extensionVariables";
import { localize } from "../../utils/localize";
import { timeout } from "../../utils/timeUtils";
import { AbstractCredentials, AbstractCredentials2, AuthProviderBase } from "../AuthProviderBase";
import { AbstractCredentials, AuthProviderBase } from "../AuthProviderBase";
import { DeviceTokenCredentials2 } from "./DeviceTokenCredentials2";
import { getUserCode } from "./getUserCode";
import { addTokenToCache, clearTokenCache, deleteRefreshToken, getStoredCredentials, getTokenResponse, getTokensFromToken, getTokenWithAuthorizationCode, ProxyTokenCache, storeRefreshToken, tokenFromRefreshToken } from "./tokens";

Expand Down Expand Up @@ -103,7 +103,7 @@ export class AdalAuthProvider extends AuthProviderBase<TokenResponse[]> {
return new DeviceTokenCredentials({ environment: (<any>Environment)[environment], username: userId, clientId, tokenCache: this.delayedTokenCache, domain: tenantId });
}

public getCredentials2(environment: Environment, userId: string, tenantId: string): AbstractCredentials2 {
public getCredentials2(environment: Environment, userId: string, tenantId: string): DeviceTokenCredentials2 {
return new DeviceTokenCredentials2(clientId, tenantId, userId, undefined, environment, this.delayedTokenCache);
}

Expand Down
24 changes: 24 additions & 0 deletions src/login/adal/DeviceTokenCredentials2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { AccessToken, TokenCredential } from "@azure/core-auth";
import { DeviceTokenCredentials } from '@azure/ms-rest-nodeauth';
import { TokenResponse } from "adal-node";

/**
* Token that is forward compatible with track 2 Azure SDK for Node.js
* `getToken: Promise<AccessToken>` is required for use with T2 Azure SDK, but doesn't
* affect T1 SDKs as those require `signRequest`
* `DeviceTokenCredentials` requires `getToken` to return `TokenResponse` so this overwrites that
*/
export class DeviceTokenCredentials2 extends DeviceTokenCredentials implements TokenCredential {
public async getToken(): Promise<AccessToken & TokenResponse> {
const tokenResponse = await super.getToken();
return Object.assign(tokenResponse, {
token: tokenResponse.accessToken,
expiresOnTimestamp: new Date().getTime() + tokenResponse.expiresIn
})
}
}