Skip to content

Commit

Permalink
nit: fix DeprecationWarning: Buffer() by using Buffer.from(string, en…
Browse files Browse the repository at this point in the history
…coding)
  • Loading branch information
hamzahejja committed Jan 2, 2020
1 parent 7dd4cb3 commit 5f16b0d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/handlers/basiccreds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
12 changes: 11 additions & 1 deletion lib/handlers/ntlm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion lib/handlers/personalaccesstoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
4 changes: 2 additions & 2 deletions test/units/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5f16b0d

Please sign in to comment.