import { TokenCredentialsBase } from "./tokenCredentialsBase" ; import { Environment } from "@azure/ms-rest-azure-env" ; import { TokenAudience } from "../util/authConstants" ; import { TokenResponse , ErrorResponse , TokenCache } from "adal-node" ; export class UserTokenCredentials extends TokenCredentialsBase { readonly username : string ; readonly password : string ; param clientId - The active directory application client id.
     * See  https * for an example.
     *  param domain param username param password param tokenAudience param environment param tokenCache public constructor ( clientId , domain , username , password , tokenAudience ? , environment ? , tokenCache ? ) { 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 ( ! username || typeof username . valueOf ( ) !== "string" ) { throw new Error ( "username must be a non empty string." ) ; } if ( ! password || typeof password . valueOf ( ) !== "string" ) { throw new Error ( "password must be a non empty string." ) ; } super ( clientId , domain , tokenAudience , environment , tokenCache ) ; this . username = username ; this . password = password ; } private crossCheckUserNameWithToken ( username , userIdFromToken ) { return username . toLowerCase ( ) === userIdFromToken . toLowerCase ( ) ; } returns public async getToken ( ) { try { return await this . getTokenFromCache ( this . username ) ; } catch ( error ) { const self = this ; const resource = this . getActiveDirectoryResourceId ( ) ; return new Promise < TokenResponse > ( ( resolve , reject ) => { self . authContext . acquireTokenWithUsernamePassword ( resource , self . username , self . password , self . clientId , ( error , tokenResponse ) => { if ( error ) { return reject ( error ) ; } if ( tokenResponse . error || tokenResponse . errorDescription ) { return reject ( tokenResponse ) ; } tokenResponse = tokenResponse as TokenResponse ; if ( self . crossCheckUserNameWithToken ( self . username , tokenResponse . userId ! ) ) { return resolve ( tokenResponse as TokenResponse ) ; } else { return reject ( `The userId "${ tokenResponse . userId }" in access token doesn't match the username "${ self . username }" provided during authentication.` ) ; } } ) ; } ) ; } } }