import { HttpHeaders } from "../httpHeaders" ; import * as base64 from "../util/base64" ; import { Constants } from "../util/constants" ; import { WebResourceLike } from "../webResource" ; import { ServiceClientCredentials } from "./serviceClientCredentials" ; const HeaderConstants = Constants . HeaderConstants ; const DEFAULT_AUTHORIZATION_SCHEME = "Basic" ; export class BasicAuthenticationCredentials implements ServiceClientCredentials { userName : string ; password : string ; authorizationScheme : string = DEFAULT_AUTHORIZATION_SCHEME ; constructor param { string } userName param { string } password param { string } authorizationScheme constructor ( userName , password , authorizationScheme = DEFAULT_AUTHORIZATION_SCHEME ) { if ( userName === null || userName === undefined || typeof userName . valueOf ( ) !== "string" ) { throw new Error ( "userName cannot be null or undefined and must be of type string." ) ; } if ( password === null || password === undefined || typeof password . valueOf ( ) !== "string" ) { throw new Error ( "password cannot be null or undefined and must be of type string." ) ; } this . userName = userName ; this . password = password ; this . authorizationScheme = authorizationScheme ; } param { WebResourceLike } webResource returns { Promise < WebResourceLike > } signRequest ( webResource ) { const credentials = `${ this . userName }:${ this . password }` ; const encodedCredentials = `${ this . authorizationScheme } ${ base64 . encodeString ( credentials ) }` ; if ( ! webResource . headers ) webResource . headers = new HttpHeaders ( ) ; webResource . headers . set ( HeaderConstants . AUTHORIZATION , encodedCredentials ) ; return Promise . resolve ( webResource ) ; } }