import { ApplicationTokenCredentials } from "./applicationTokenCredentials" ; import { ApplicationTokenCertificateCredentials } from "./applicationTokenCertificateCredentials" ; import { DeviceTokenCredentials } from "./deviceTokenCredentials" ; import { MSIAppServiceTokenCredentials } from "./msiAppServiceTokenCredentials" ; import { MSITokenCredentials } from "./msiTokenCredentials" ; import { MSIVmTokenCredentials } from "./msiVmTokenCredentials" ; import { TokenCredentialsBase } from "./tokenCredentialsBase" ; import { UserTokenCredentials } from "./userTokenCredentials" ; import { AuthenticationContext , TokenResponse , ErrorResponse } from "adal-node" ; import { Authenticator } from "@azure/ms-rest-js" ; export function createAuthenticator ( credentials ) { const convertedCredentials = _convert ( credentials ) ; const authenticator = _createAuthenticatorMapper ( convertedCredentials ) ; return authenticator ; } function _convert ( credentials ) { if ( credentials instanceof MSIAppServiceTokenCredentials ) { return new MSIAppServiceTokenCredentials ( { msiEndpoint : credentials . msiEndpoint , msiSecret : credentials . msiSecret , msiApiVersion : credentials . msiApiVersion , resource : credentials . resource } ) ; } else if ( credentials instanceof MSIVmTokenCredentials ) { return new MSIVmTokenCredentials ( { resource : credentials . resource , msiEndpoint : credentials . msiEndpoint } ) ; } else if ( credentials instanceof MSITokenCredentials ) { throw new Error ( "MSI-credentials not one of: MSIVmTokenCredentials, MSIAppServiceTokenCredentials" ) ; } else { return credentials ; } } function _createAuthenticatorMapper ( credentials ) { return ( challenge ) => new Promise ( ( resolve , reject ) => { const _formAuthorizationValue = ( err , tokenResponse ) => { if ( err ) { return reject ( err ) ; } if ( tokenResponse . error ) { return reject ( tokenResponse . error ) ; } tokenResponse = tokenResponse as TokenResponse ; const authorizationValue = tokenResponse . tokenType + " " + tokenResponse . accessToken ; return resolve ( authorizationValue ) ; } ; if ( credentials instanceof TokenCredentialsBase ) { const context = new AuthenticationContext ( challenge . authorization , true , credentials . authContext && credentials . authContext . cache ) ; if ( credentials instanceof ApplicationTokenCredentials ) { return context . acquireTokenWithClientCredentials ( challenge . resource , credentials . clientId , credentials . secret , _formAuthorizationValue ) ; } else if ( credentials instanceof ApplicationTokenCertificateCredentials ) { return context . acquireTokenWithClientCertificate ( challenge . resource , credentials . clientId , credentials . certificate , credentials . thumbprint , _formAuthorizationValue ) ; } else if ( credentials instanceof UserTokenCredentials ) { return context . acquireTokenWithUsernamePassword ( challenge . resource , credentials . username , credentials . password , credentials . clientId , _formAuthorizationValue ) ; } else if ( credentials instanceof DeviceTokenCredentials ) { return context . acquireToken ( challenge . resource , credentials . username , credentials . clientId , _formAuthorizationValue ) ; } } else if ( credentials instanceof MSITokenCredentials ) { return credentials . getToken ( ) ; } else { return reject ( new Error ( "credentials must be one of: ApplicationTokenCredentials, UserTokenCredentials, " + "DeviceTokenCredentials, MSITokenCredentials" ) ) ; } } ) ; }