Skip to content

Commit 84284af

Browse files
committed
Merge pull request colynb#4 from AndreasPizsa/master
Allow non-NTLM resources
2 parents 926e6e7 + 8766cba commit 84284af

File tree

7 files changed

+129
-25
lines changed

7 files changed

+129
-25
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
node_modules
2+
.idea
3+
coverage
4+
test/ntlm-options.js

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- "stable"

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.1.3
2+
3+
* Allow requests to non-NTLM resources
4+
15
# 0.1.1
26

37
* Bugfix to allow type1 message auth

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ ntlm.post(opts, json, function(err, response) {
3333
* don't assume the post body is an object and should be made into json
3434
* options.domain is in use by request. Use ntlm_domain instead
3535
* ability to set custom headers
36-
* ability to use http and not only https
36+
* ability to use http and not only https
37+
* gracefully complete the request if the server doesn't actually require NTLM.
38+
Fail only if `options.ntlm.strict` is set to `true` (default=`false`).

index.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
var async = require('async');
1+
var _ = require('lodash');
2+
var async = require('async');
23
var request = require('request');
3-
var ntlm = require('./lib/ntlm');
4-
var KeepAlive = require('agentkeepalive');
5-
var _ = require('lodash');
4+
var ntlm = require('./lib/ntlm');
65

76
var makeRequest = function(method, options, params, callback) {
8-
7+
8+
var KeepAlive = require('agentkeepalive');
99
if (options.url.toLowerCase().indexOf('https://') === 0) {
1010
KeepAlive = KeepAlive.HttpsAgent;
1111
}
@@ -14,7 +14,10 @@ var makeRequest = function(method, options, params, callback) {
1414

1515
if (!options.workstation) options.workstation = '';
1616
if (!options.ntlm_domain) options.ntlm_domain = '';
17-
if (!options.headers) options.headers = {};
17+
if (!options.headers ) options.headers = {};
18+
19+
options.ntlm = options.ntlm || {};
20+
options.ntlm.strict = Boolean(options.ntlm.strict);
1821

1922
function startAuth($) {
2023
var type1msg = ntlm.createType1Message(options);
@@ -28,8 +31,11 @@ var makeRequest = function(method, options, params, callback) {
2831
}
2932

3033
function requestComplete(res, body, $) {
31-
if (!res.headers['www-authenticate'])
32-
return $(new Error('www-authenticate not found on response of second request'));
34+
if (!res.headers['www-authenticate']) {
35+
return options.ntlm.strict
36+
? $(new Error('www-authenticate not found on response of second request'))
37+
: $(null, res, body);
38+
}
3339

3440
var type2msg = ntlm.parseType2Message(res.headers['www-authenticate']);
3541
var type3msg = ntlm.createType3Message(type2msg, options);
@@ -52,7 +58,7 @@ var makeRequest = function(method, options, params, callback) {
5258
async.waterfall([startAuth, requestComplete], callback);
5359
};
5460

55-
exports.get = _.partial(makeRequest, 'get');
56-
exports.post = _.partial(makeRequest, 'post');
57-
exports.put = _.partial(makeRequest, 'put');
58-
exports.delete = _.partial(makeRequest, 'delete');
61+
exports.get = _.partial(makeRequest, 'get');
62+
exports.post = _.partial(makeRequest, 'post');
63+
exports.put = _.partial(makeRequest, 'put');
64+
exports.delete= _.partial(makeRequest, 'delete');

package.json

+41-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
11
{
22
"name": "request-ntlm-continued",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "NTLM auth for NodeJS - continued from Colyn Brown's project",
55
"main": "index.js",
66
"repository": "https://github.com/FrankyBoy/request-ntlm",
7-
"contributors": [{
8-
"name": "Colyn Brown",
9-
"email": "clbrown@godaddy.com"
10-
},{
11-
"name": "Immanuel Hayden",
12-
"email": "immanuel.hayden@gmail.com"
13-
},{
14-
"name": "Brett Profitt",
15-
"email": "brett.profitt@gmail.com"
16-
}],
7+
"scripts": {
8+
"test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
9+
},
10+
"contributors": [
11+
{
12+
"name": "Colyn Brown",
13+
"email": "clbrown@godaddy.com"
14+
},
15+
{
16+
"name": "Immanuel Hayden",
17+
"email": "immanuel.hayden@gmail.com"
18+
},
19+
{
20+
"name": "Brett Profitt",
21+
"email": "brett.profitt@gmail.com"
22+
},
23+
{
24+
"name": "Andreas Pizsa",
25+
"url": "https://github.com/AndreasPizsa"
26+
}
27+
],
1728
"license": "ISC",
1829
"dependencies": {
1930
"agentkeepalive": "^2.0.3",
2031
"async": "^0.7.0",
2132
"lodash": "^2.4.1",
2233
"request": "^2.34.0"
23-
}
34+
},
35+
"devDependencies": {
36+
"coveralls": "*",
37+
"istanbul": "*",
38+
"mocha": "*",
39+
"mocha-lcov-reporter": "*",
40+
"should": "*"
41+
},
42+
"keywords" : [
43+
"ntlm",
44+
"authentication",
45+
"auth",
46+
"login",
47+
"windows",
48+
"microsoft",
49+
"request",
50+
"http",
51+
"https"
52+
]
2453
}

test/test.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
var should = require('should');
3+
var request = require('../index.js');
4+
5+
describe('request-ntlm-continued', function(){
6+
7+
function simpleGetRequest(options) {
8+
return function(callback) {
9+
request.get(options, undefined, function (err, res, body) {
10+
should.not.exist(err);
11+
should.exist(res);
12+
should.exist(body);
13+
callback();
14+
});
15+
}
16+
}
17+
18+
this.timeout(10000);
19+
20+
var executeRequestAgainsNtlmServer = undefined;
21+
try {
22+
var options = require(__dirname + '/ntlm-options');
23+
executeRequestAgainsNtlmServer = simpleGetRequest(options);
24+
}
25+
catch(err) {
26+
}
27+
28+
it('successfully execute a request against an NTLM server', executeRequestAgainsNtlmServer);
29+
30+
describe("non-NTLM requests", function(){
31+
32+
var options;
33+
beforeEach(function(){
34+
options = {
35+
username : 'username',
36+
password : 'password',
37+
ntlm_domain : 'yourdomain',
38+
workstation : 'workstation',
39+
url : 'https://www.google.com/search?q=ntlm',
40+
strictSSL : false
41+
};
42+
});
43+
44+
it('successfully execute a request against a non-NTLM server', function(done){
45+
simpleGetRequest(options)(done);
46+
});
47+
48+
it('fails when requesting non-NTLM resource with `options.ntlm.strict`', function(done){
49+
options.ntlm = { strict:true };
50+
request.get(options, undefined, function (err) {
51+
should.exist(err);
52+
done();
53+
});
54+
});
55+
});
56+
});

0 commit comments

Comments
 (0)