UNPKG

4.46 kBMarkdownView Raw
1# httpntlm
2
3__httpntlm__ is a Node.js library to do HTTP NTLM authentication
4
5It's a port from the Python libary [python-ntml](https://code.google.com/p/python-ntlm/)
6
7## Install
8
9You can install __httpntlm__ using the Node Package Manager (npm):
10
11 npm install httpntlm
12
13## How to use
14
15```js
16var httpntlm = require('httpntlm');
17
18httpntlm.get({
19 url: "https://someurl.com",
20 username: 'm$',
21 password: 'stinks',
22 workstation: 'choose.something',
23 domain: ''
24}, function (err, res){
25 if(err) return err;
26
27 console.log(res.headers);
28 console.log(res.body);
29});
30```
31
32It supports __http__ and __https__.
33
34## Options
35
36- `url:` _{String}_ URL to connect. (Required)
37- `username:` _{String}_ Username. (Required)
38- `password:` _{String}_ Password. (Required)
39- `workstation:` _{String}_ Name of workstation or `''`.
40- `domain:` _{String}_ Name of domain or `''`.
41
42You can also pass along all other options of [httpreq](https://github.com/SamDecrock/node-httpreq), including custom headers, cookies, body data, ... and use POST, PUT or DELETE instead of GET.
43
44## Advanced
45
46If you want to use the NTLM-functions yourself, you can access the ntlm-library like this (https example):
47
48```js
49var ntlm = require('httpntlm').ntlm;
50var async = require('async');
51var httpreq = require('httpreq');
52var HttpsAgent = require('agentkeepalive').HttpsAgent;
53var keepaliveAgent = new HttpsAgent();
54
55var options = {
56 url: "https://someurl.com",
57 username: 'm$',
58 password: 'stinks',
59 workstation: 'choose.something',
60 domain: ''
61};
62
63async.waterfall([
64 function (callback){
65 var type1msg = ntlm.createType1Message(options);
66
67 httpreq.get(options.url, {
68 headers:{
69 'Connection' : 'keep-alive',
70 'Authorization': type1msg
71 },
72 agent: keepaliveAgent
73 }, callback);
74 },
75
76 function (res, callback){
77 if(!res.headers['www-authenticate'])
78 return callback(new Error('www-authenticate not found on response of second request'));
79
80 var type2msg = ntlm.parseType2Message(res.headers['www-authenticate']);
81 var type3msg = ntlm.createType3Message(type2msg, options);
82
83 setImmediate(function() {
84 httpreq.get(options.url, {
85 headers:{
86 'Connection' : 'Close',
87 'Authorization': type3msg
88 },
89 allowRedirects: false,
90 agent: keepaliveAgent
91 }, callback);
92 });
93 }
94], function (err, res) {
95 if(err) return console.log(err);
96
97 console.log(res.headers);
98 console.log(res.body);
99});
100```
101
102## Download binary files
103
104```javascript
105httpntlm.get({
106 url: "https://someurl.com/file.xls",
107 username: 'm$',
108 password: 'stinks',
109 workstation: 'choose.something',
110 domain: '',
111 binary: true
112}, function (err, response) {
113 if(err) return console.log(err);
114 fs.writeFile("file.xls", response.body, function (err) {
115 if(err) return console.log("error writing file");
116 console.log("file.xls saved!");
117 });
118});
119```
120
121## More information
122
123* [python-ntlm](https://code.google.com/p/python-ntlm/)
124* [NTLM Authentication Scheme for HTTP](http://www.innovation.ch/personal/ronald/ntlm.html)
125* [LM hash on Wikipedia](http://en.wikipedia.org/wiki/LM_hash)
126
127
128## License (MIT)
129
130Copyright (c) Sam Decrock <https://github.com/SamDecrock/>
131
132Permission is hereby granted, free of charge, to any person obtaining a copy
133of this software and associated documentation files (the "Software"), to deal
134in the Software without restriction, including without limitation the rights
135to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
136copies of the Software, and to permit persons to whom the Software is
137furnished to do so, subject to the following conditions:
138
139The above copyright notice and this permission notice shall be included in
140all copies or substantial portions of the Software.
141
142THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
143IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
144FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
145AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
146LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
147OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
148THE SOFTWARE.