import { Constants as MSRestConstants , WebResource } from "@azure/ms-rest-js" ; import { Environment } from "@azure/ms-rest-azure-env" ; import { TokenAudience } from "../util/authConstants" ; import { TokenClientCredentials } from "./tokenClientCredentials" ; import { TokenResponse , AuthenticationContext , MemoryCache , ErrorResponse , TokenCache } from "adal-node" ; export abstract class TokenCredentialsBase implements TokenClientCredentials { public authContext : AuthenticationContext ; public constructor ( public readonly clientId , public domain , public readonly tokenAudience ? , public readonly environment = Environment . AzureCloud , public tokenCache = new MemoryCache ( ) ) { if ( ! clientId || typeof clientId . valueOf ( ) !== "string" ) { throw new Error ( "clientId must be a non empty string." ) ; } if ( ! domain || typeof domain . valueOf ( ) !== "string" ) { throw new Error ( "domain must be a non empty string." ) ; } if ( this . tokenAudience === "graph" && this . domain . toLowerCase ( ) === "common" ) { throw new Error ( `${ 'If the tokenAudience is specified as "graph" then "domain" cannot be defaulted to "common" tenant.\
        It must be the actual tenant (preferably a string in a guid format).' }` ) ; } const authorityUrl = this . environment . activeDirectoryEndpointUrl + this . domain ; this . authContext = new AuthenticationContext ( authorityUrl , this . environment . validateAuthority , this . tokenCache ) ; } public setDomain ( domain ) { this . domain = domain ; const authorityUrl = this . environment . activeDirectoryEndpointUrl + this . domain ; this . authContext = new AuthenticationContext ( authorityUrl , this . environment . validateAuthority , this . tokenCache ) ; } protected getActiveDirectoryResourceId ( ) { let resource = this . environment . activeDirectoryResourceId ; if ( this . tokenAudience ) { resource = this . tokenAudience ; if ( this . tokenAudience . toLowerCase ( ) === "graph" ) { resource = this . environment . activeDirectoryGraphResourceId as string ; } else if ( this . tokenAudience . toLowerCase ( ) === "batch" ) { resource = this . environment . batchResourceId as string ; } } return resource ; } protected getTokenFromCache ( username ? ) { const self = this ; const resource = this . getActiveDirectoryResourceId ( ) ; return new Promise < TokenResponse > ( ( resolve , reject ) => { self . authContext . acquireToken ( resource , username ! , self . clientId , ( error , tokenResponse ) => { if ( error ) { return reject ( error ) ; } if ( tokenResponse . error || tokenResponse . errorDescription ) { return reject ( tokenResponse ) ; } return resolve ( tokenResponse as TokenResponse ) ; } ) ; } ) ; } returns public abstract async getToken ( ) ; param webResource public async signRequest ( webResource ) { const tokenResponse = await this . getToken ( ) ; webResource . headers . set ( MSRestConstants . HeaderConstants . AUTHORIZATION , `${ tokenResponse . tokenType } ${ tokenResponse . accessToken }` ) ; return webResource ; } }