import { Constants , WebResource , HttpClient , DefaultHttpClient } from "@azure/ms-rest-js" ; import { TokenClientCredentials , TokenResponse } from "./tokenClientCredentials" ; import { AuthConstants } from "../util/authConstants" ; /**
 * Defines the optional parameters for authentication with MSI.
 */ export interface MSIOptions { prop resource ? : string ; /**
     * The client responsible for sending HTTP requests.
     * By default it is Axios-based  DefaultHttpClient .
      httpClient ? : HttpClient ; } /**
 * Describes the MSITokenResponse.
 */ export interface MSITokenResponse extends TokenResponse { /**
     * Placeholder for unknown properties.
     */ readonly [ x ] : any ; } /**
 * Provides information about managed service identity token credentials.
 * This object can only be used to acquire token on a virtual machine provisioned in Azure with managed service identity.
 */ export abstract class MSITokenCredentials implements TokenClientCredentials { /**
     * Azure resource endpoints.
     * - Defaults to Azure Resource Manager from environment: AzureCloud. "https://management.azure.com/"
     * - For ServiceManagement (ASM): "https://management.core.windows.net/"
     * - For Azure KeyVault: "https://vault.azure.net"
     * - For Azure Batch: "https://batch.core.windows.net"
     * - For Azure Active Directory Graph: "https://graph.windows.net"
     */ resource : string ; protected _httpClient : HttpClient ; param options param options . resource constructor ( options ) { if ( ! options ) options = { } ; if ( ! options . resource ) { options . resource = AuthConstants . RESOURCE_MANAGER_ENDPOINT ; } else if ( typeof options . resource . valueOf ( ) !== "string" ) { throw new Error ( "resource must be a uri." ) ; } this . resource = options . resource ; this . _httpClient = options . httpClient || new DefaultHttpClient ( ) ; } param body returns parseTokenResponse ( body ) { const parsedBody = JSON . parse ( body ) ; parsedBody . accessToken = parsedBody [ "access_token" ] ; delete parsedBody [ "access_token" ] ; parsedBody . tokenType = parsedBody [ "token_type" ] ; delete parsedBody [ "token_type" ] ; if ( parsedBody [ "refresh_token" ] ) { parsedBody . refreshToken = parsedBody [ "refresh_token" ] ; delete parsedBody [ "refresh_token" ] ; } if ( parsedBody [ "expires_in" ] ) { parsedBody . expiresIn = parsedBody [ "expires_in" ] ; if ( typeof parsedBody [ "expires_in" ] === "string" ) { parsedBody . expiresIn = parseInt ( parsedBody [ "expires_in" ] , 10 ) ; } delete parsedBody [ "expires_in" ] ; } if ( parsedBody [ "not_before" ] ) { parsedBody . notBefore = parsedBody [ "not_before" ] ; if ( typeof parsedBody [ "not_before" ] === "string" ) { parsedBody . notBefore = parseInt ( parsedBody [ "not_before" ] , 10 ) ; } delete parsedBody [ "not_before" ] ; } if ( parsedBody [ "expires_on" ] ) { parsedBody . expiresOn = parsedBody [ "expires_on" ] ; if ( typeof parsedBody [ "expires_on" ] === "string" ) { if ( parsedBody [ "expires_on" ] . includes ( ":" ) || parsedBody [ "expires_on" ] . includes ( "/" ) ) { parsedBody . expiresOn = new Date ( parseInt ( parsedBody [ "expires_on" ] , 10 ) ) ; } else { parsedBody . expiresOn = new Date ( parseInt ( parsedBody [ "expires_on" ] , 10 ) ) ; } } delete parsedBody [ "expires_on" ] ; } return parsedBody ; } param callback returns abstract async getToken ( ) ; protected abstract prepareRequestOptions ( ) ; param webResource returns public async signRequest ( webResource ) { const tokenResponse = await this . getToken ( ) ; webResource . headers . set ( Constants . HeaderConstants . AUTHORIZATION , `${ tokenResponse . tokenType } ${ tokenResponse . accessToken }` ) ; return webResource ; } }