Please ignore my stupidity, I had a typo.  Fixed and now working.  But in 
case anyone wants to know, 2nd call didn't have port, host, path..

var NTLMCall = function(options, callback){
  var host = options.host;
  var port = options.port;
  var user = options.user;
  var pass = options.pass;
  var domain = options.domain;
  var path = options.path||'/';
  var hostname = options.hostname||require('os').hostname();
  
  var head = function(options, callback){
    options.method = 'HEAD';
    var req = http.request(options, function(res){
        callback(null, res.headers);
      });
    req.end();
  };
  
  // Generate Type 1 to send to server in HTTP Request:
  var buf = ntlm.encodeType1(hostname, domain);
  // Should be capturing the connection here for re-use
  head({headers: {Authorization: 'NTLM ' + buf.toString('base64')}, host: 
host, port: port, path: '/'}, function(err, headers){
    var hdr = headers['www-authenticate'];
    var m = /^NTLM (.*)$/.exec(hdr);
    var inbuf = new Buffer(m[1], 'base64');
    // Extract Type 2 from HTTP Response header, and use it here:
    var serverNonce = ntlm.decodeType2(inbuf);
    // Generate Type 3 to send as authentication to server:
    var buf = ntlm.encodeType3(user, hostname, domain, serverNonce, pass);
    // Should be re-using the captured connection for this, it fails as is
    var req = http.request({headers: {Authorization: 'NTLM ' + 
buf.toString('base64')}, host: host, port: port, path: path}, function(res){
      var data = '';
      res.setEncoding('utf8');
      res.on('data', function (chunk){
        data += chunk;
      });
      res.on('end', function(){
        callback(null, data);
      });
      res.on('error', function(err){
        callback(err);
      });
    }).on('error', function(err){
      callback(err);
    });
    req.end();
  });
};

On Friday, February 8, 2013 11:58:41 AM UTC-6, Jeremy wrote:
>
> I've been working on using SMBHash (
> https://github.com/jclulow/node-smbhash) to add NTLM authentication to a 
> client request as part of a project I am working on.  Problem is that for 
> NTLM to work properly you have to make a head request to get the Type2 
> header then use the same connection with data you get back and make your 
> Type3 request to get the final response.
>
> The code shown on the GitHub page is suggestive, but doesn't work since 
> you can't do HTTP that way any more.
>
> So, I've tried what I can find but it seems I can't find any details on 
> how to re-use the connection object for all of my requests until done.
>
> The code I'm using requires you to download SMBHash has the author hasn't 
> published the current version to NPM, the current version is on GitHub.  In 
> case someone wants to try running this code.
>
> Here is the code I'm using today, I've noted the two places I know I need 
> to make changes, but don't know what changes to make:
>
> var ntlm = require('smbhash').ntlm;
>
> var NTLMCall = function(options, callback){
>   var host = options.host;
>   var port = options.port;
>   var user = options.user;
>   var pass = options.pass;
>   var domain = options.domain;
>   var hostname = options.hostname||require('os').hostname();
>   
>   var head = function(options, callback){
>     options.method = 'HEAD';
>     var req = http.request(options, function(res){
>         callback(null, res.headers);
>       });
>     req.end();
>   };
>   
>   // Generate Type 1 to send to server in HTTP Request:
>   var buf = ntlm.encodeType1(hostname, domain);
>   // Should be capturing the connection here for re-use
>   head({headers: {Authorization: 'NTLM ' + buf.toString('base64')}, host: 
> host, port: port, path: '/'}, function(err, headers){
>     var hdr = headers['www-authenticate'];
>     var m = /^NTLM (.*)$/.exec(hdr);
>     var inbuf = new Buffer(m[1], 'base64');
>     // Extract Type 2 from HTTP Response header, and use it here:
>     var serverNonce = ntlm.decodeType2(inbuf);
>     // Generate Type 3 to send as authentication to server:
>     var buf = ntlm.encodeType3(user, hostname, domain, serverNonce, pass);
>     // Should be re-using the captured connection for this, it fails as is
>     var req = http.request({headers: {Authorization: 'NTLM ' + 
> buf.toString('base64')}}, function(res){
>       var data = '';
>       res.setEncoding('utf8');
>       res.on('data', function (chunk){
>         data += chunk;
>       });
>       res.on('end', function(){
>         callback(null, data);
>       });
>       res.on('error', function(err){
>         callback(err);
>       });
>     }).on('error', function(err){
>       callback(err);
>     });
>     req.end();
>   });
> };
>
> To use:
>   NTLMCall({
>     host  : servername,
>     port  : serverport,
>     user  : user,
>     pass  : pass,
>     domain: domain
>   }, callback);
>
> Thanks,
>  - Jeremy
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to