2

I am try to get all the project names from a tfs api url. For that I am using the node.js ntlm package. Following is my code.

function getTFSProjectNames(credentials) {
        return new Promise(function (resolve, reject) {
            var ntlm = require('ntlm');
            var ntlmrequest = require('request').defaults({
                agentClass: require('agentkeepalive').HttpsAgent
            });
            var projectNames = [];
            var url = "https://{instance}/tfs/DefaultCollection/_apis/projects?api-version=1.0"
                , domain = 'domain'
                , hostname = 'hostname';

           var testOption = {
                             url:url,
                             headers: {
                              'Authorization': ntlm.challengeHeader(hostname, domain)
                            },
                            proxy: 'server proxy',
                            timeout: 10000,
                            followRedirect: true,
                            maxRedirects: 10        
                          }

            ntlmrequest(testOption , function (err, ntlmres) {
                if (err && ntlmres == undefined)
                    resolve({ 'Projects': projectNames });
                else {
                    var options = {
                        method: 'get',
                        json: true,
                        url: url,
                        headers: {
                            'Authorization': ntlm.responseHeader(ntlmres, url, domain, credentials.username, credentials.password),
                            'Content-Type': 'application/json'
                        },
                       proxy: 'server proxy',
                       timeout: 10000,
                       followRedirect: true,
                       maxRedirects: 10 
                    }
                    ntlmrequest(options, function (err, ntlmres, body) {
                        if (err)
                            return next(err)

                        if (ntlmres != undefined && ntlmres.body != undefined && ntlmres.body.value != undefined) {
                            var projectNames = ntlmres.body.value.map(function (item) {
                                return item.name;
                            })
                        }
                        resolve({ 'Projects': projectNames });
                    });
                }
            });
        });
    }

Even if the correct credential, It's response is always as 401 - Unauthorized: Access is denied due to invalid credentials. It's a server error.I am not able to understand, why it's happening every time. Please help me to solve this issue.

Libin C Jacob
  • 1,108
  • 12
  • 34
  • 1
    NTLM authenticates a connection and not a request, and for this reason all the steps of the authentication must operate on the same socket (which seems to be the case in your code). Can we check the network connections ? Come back with a network capture please. – Eugène Adell Feb 01 '18 at 10:04
  • @Eugène Adell - you are right, it's working in my local machine. The problem is happening only in UAT server. That's why I am included the proxy along with the option field. But still I am getting the 401 error. – Libin C Jacob Feb 01 '18 at 11:38
  • make a capture on the proxy with the client and the final server as filters, to check if anyone is ending the connection too early. – Eugène Adell Feb 01 '18 at 12:06
  • @Eugène Adell - But its not able to identify. – Libin C Jacob Feb 02 '18 at 08:31
  • We don't know if your proxy is NTLM compatible. Or even, if it's not the proxy itself which needs an authentication. That's why I asked for a capture. One solution is to ask your proxy administrator to bypass traffic for your server. – Eugène Adell Feb 02 '18 at 08:43
  • @Eugène Adell - I am getting the result when run the api directly on the address bar of any browser. That's why I am confused. – Libin C Jacob Feb 02 '18 at 09:01
  • 1
    I expect the below answer helps you https://stackoverflow.com/questions/1222506/avoiding-401-response-for-each-request-using-ntlm – Arun Bertil Feb 12 '18 at 10:31

0 Answers0