import * as url from 'url' ; import { request } from './../../config' ; import * as http from 'http' ; import * as https from 'https' ; const ntlm = require ( 'node-ntlm-client/lib/ntlm' ) ; import { IAuthResolver } from './../IAuthResolver' ; import { IOnpremiseUserCredentials } from './../IAuthOptions' ; import { IAuthResponse } from './../IAuthResponse' ; export class OnpremiseUserCredentials implements IAuthResolver { constructor ( private _siteUrl , private _authOptions ) { } public getAuth ( ) { const ntlmOptions = Object . assign ( { } , this . _authOptions ) ; ntlmOptions . url = this . _siteUrl ; if ( ntlmOptions . username . indexOf ( '\\' ) > 0 ) { const parts = ntlmOptions . username . split ( '\\' ) ; ntlmOptions . username = parts [ 1 ] ; ntlmOptions . domain = parts [ 0 ] . toUpperCase ( ) ; } if ( ntlmOptions . username . indexOf ( '@' ) > 0 ) { ntlmOptions . domain = '' ; } const type1msg = ntlm . createType1Message ( ) ; const isHttps = url . parse ( this . _siteUrl ) . protocol === 'https:' ; const keepaliveAgent = isHttps ? new https . Agent ( { keepAlive : true , rejectUnauthorized : ! ! ntlmOptions . rejectUnauthorized } ) : new http . Agent ( { keepAlive : true } ) ; return request ( { url : this . _siteUrl , method : 'GET' , headers : { 'Connection' : 'keep-alive' , 'Authorization' : type1msg , 'Accept' : 'application/json;odata=verbose' } , agent : keepaliveAgent } ) . then ( response => { const type2msg = ntlm . decodeType2Message ( response . headers [ 'www-authenticate' ] ) ; const type3msg = ntlm . createType3Message ( type2msg , ntlmOptions . username , ntlmOptions . password , ntlmOptions . workstation , ntlmOptions . domain ) ; return { headers : { 'Connection' : 'Close' , 'Authorization' : type3msg } , options : { agent : keepaliveAgent } } ; } ) as Promise < IAuthResponse > ; } }