nodejs app - download file from sharepoint with NTLM AUTH

I must confess I'm stuck. I need a nodejs app to download a file from a SharePoint library. Simple, huh? No. Not the simple OOTB SharePoint. The only-ssl allowed, with specific mandatory header added and surely only domain-based NTLM authentication method.

I've tried httpntlm (https://www.npmjs.org/package/httpntlm) that seemed to just might work in advance, but no. The SP responses with something went wrong message.

I've tried node-sharepoint, but it doesn't support NTLM yet. The app gets ETIMEDOUT response.

Any ideas, please welcome.

I am able to download the file using httpntlm module.you need to change the few lines of code.Replace the waterfall logic with below code in httpntlm.js.

async.waterfall([
        function ($){
            var type1msg = ntlm.createType1Message(options);

            httpreq.get(options.url, {
                headers:{
                    'Connection' : 'keep-alive',
                    'Authorization': type1msg
                },
                agent: keepaliveAgent
            }, $);
        },

        function (res, $){
            if(!res.headers['www-authenticate'])
                return $(new Error('www-authenticate not found on response of second request'));

            var type2msg = ntlm.parseType2Message(res.headers['www-authenticate']);
            var type3msg = ntlm.createType3Message(type2msg, options);
            if(method!=='download'){
                httpreq[method](options.url, {
                    headers:{
                        'Connection' : 'Close',
                        'Authorization': type3msg
                    },
                    allowRedirects: false,
                    agent: keepaliveAgent
                }, $);
            }else{

                    //By Dheeraj for file download
                    httpreq.download(
                        url,
                    {
                        headers:{
                            'Connection' : 'Close',
                            'Authorization': type3msg
                        },
                        //allowRedirects: false,
                        agent: keepaliveAgent
                    },
                    __dirname + 'your_filename',                        
                     function (err, progress){
                        if (err) return console.log(err);
                        console.log(progress);
                    }, function (err, res){
                        if (err) return console.log(err);
                        console.log(res);
                    });

            }
        }
    ], callback);
};

['get', 'put', 'post', 'delete', 'head','download'].forEach(function(method){
  exports[method] = exports.method.bind(exports, method);
});

and replace download method of httpreq.js(httpntm_module/node_modules/httpreq_module/httpreq.js) You can find it at Line number 79 approx.

exports.download = function (url,options, downloadlocation, progressCallback, callback) {
    //var options = {};
    options.url = url;
    options.method = 'GET';
    options.downloadlocation = downloadlocation;
    options.allowRedirects = true;
    // if only 3 args are provided, so no progressCallback
    if(callback === undefined && progressCallback && typeof(progressCallback)==="function")
        callback = progressCallback;
    else
        options.progressCallback = progressCallback;

    doRequest(options, callback);
}

Please let me know if you are still getting issues.