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" ; /**
 * Resource manager endpoints to match in order to specify a valid scope to the AzureIdentityCredentialAdapter.
 */ export const azureResourceManagerEndpoints = [ "https://management.windows.net" , "https://management.chinacloudapi.cn" , "https://management.usgovcloudapi.net" , "https://management.cloudapi.de" , ] ; /**
 * This class provides a simple extension to use  TokenCredential from `@azure/identity` library to
 * use with legacy Azure SDKs that accept  ServiceClientCredentials family of credentials for authentication.
  export class AzureIdentityCredentialAdapter implements ServiceClientCredentials { private azureTokenCredential : TokenCredential ; private scopes : string | string [ ] ; constructor ( azureTokenCredential , scopes = "https://management.azure.com/.default" ) { this . azureTokenCredential = azureTokenCredential ; this . scopes = scopes ; } public async getToken ( ) { const accessToken = await this . azureTokenCredential . getToken ( this . scopes ) ; if ( accessToken !== null ) { const result = { 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 ) { const tokenResponse = await this . getToken ( ) ; webResource . headers . set ( MSRestConstants . HeaderConstants . AUTHORIZATION , `${ tokenResponse . tokenType } ${ tokenResponse . accessToken }` ) ; return Promise . resolve ( webResource ) ; } }