diff --git a/lib/handlers/basiccreds.ts b/lib/handlers/basiccreds.ts index b4030efe..d8f31f77 100644 --- a/lib/handlers/basiccreds.ts +++ b/lib/handlers/basiccreds.ts @@ -15,7 +15,7 @@ export class BasicCredentialHandler implements ifm.IRequestHandler { // currently implements pre-authorization // TODO: support preAuth = false where it hooks on 401 prepareRequest(options:any): void { - options.headers['Authorization'] = 'Basic ' + new Buffer(this.username + ':' + this.password).toString('base64'); + options.headers['Authorization'] = `Basic ${Buffer.from(`${this.username}:${this.password}`).toString('base64')}`; options.headers['X-TFS-FedAuthRedirect'] = 'Suppress'; } diff --git a/lib/handlers/ntlm.ts b/lib/handlers/ntlm.ts index 49f7f78c..96350761 100644 --- a/lib/handlers/ntlm.ts +++ b/lib/handlers/ntlm.ts @@ -147,7 +147,17 @@ export class NtlmCredentialHandler implements ifm.IRequestHandler { throw new Error('www-authenticate not found on response of second request'); } - const serverNonce: Buffer = new Buffer((res.message.headers['www-authenticate'].match(/^NTLM\s+(.+?)(,|\s+|$)/) || [])[1], 'base64'); + /** + * Server will respond with challenge/nonce + * assigned to response's "WWW-AUTHENTICATE" header + * and should be starting with NTLM + */ + const serverNonceRegex = /^NTLM\s+(.+?)(,|\s+|$)/; + const serverNonce: Buffer = Buffer.from( + (res.message.headers['www-authenticate'].match(serverNonceRegex) || [])[1], + 'base64' + ); + const type2msg: Buffer = ntlm.decodeType2(serverNonce); const type3msg: string = ntlm.encodeType3( diff --git a/lib/handlers/personalaccesstoken.ts b/lib/handlers/personalaccesstoken.ts index 8c8ada33..eab46875 100644 --- a/lib/handlers/personalaccesstoken.ts +++ b/lib/handlers/personalaccesstoken.ts @@ -13,7 +13,7 @@ export class PersonalAccessTokenCredentialHandler implements ifm.IRequestHandler // currently implements pre-authorization // TODO: support preAuth = false where it hooks on 401 prepareRequest(options:any): void { - options.headers['Authorization'] = 'Basic ' + new Buffer('PAT:' + this.token).toString('base64'); + options.headers['Authorization'] = `Basic ${Buffer.from(`PAT:${this.token}`).toString('base64')}`; options.headers['X-TFS-FedAuthRedirect'] = 'Suppress'; } diff --git a/test/units/handlers.ts b/test/units/handlers.ts index 5ce0c791..02963982 100644 --- a/test/units/handlers.ts +++ b/test/units/handlers.ts @@ -107,7 +107,7 @@ describe('Authentication Handlers Tests', function () { it('[Personal Access Token] - does basic http get request with PAT token auth', async() => { const url: string = 'http://microsoft.com'; const secret: string = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs'; - const personalAccessToken: string = new Buffer(`PAT:${secret}`).toString('base64'); + const personalAccessToken: string = Buffer.from(`PAT:${secret}`).toString('base64'); const expectedAuthHeader: string = `Basic ${personalAccessToken}`; const patAuthHandler: hm.PersonalAccessTokenCredentialHandler = new hm.PersonalAccessTokenCredentialHandler(secret); @@ -204,7 +204,7 @@ describe('Authentication Handlers Tests', function () { assert(httpResponse.message.statusCode === httpm.HttpCodes.Unauthorized); //statusCode is 401 - Unauthorized }); - it('does basic http get request with NTLM Authentication', async() => { + it('[NTLM] - does basic http get request with NTLM Authentication', async() => { /** * Following NTLM Authentication Example on: * https://www.innovation.ch/personal/ronald/ntlm.html