在node.js中,Http Digest身份验证失败

时间:2014-05-22 10:59:29

标签: node.js authentication digest

我正在尝试使用摘要式身份验证执行http get请求。我在下面发布我的代码

var digestOption    =   {
                                        'host' : hostName,
                                        'port'  : port,
                                        'path'  : path
                                    }
            http.get(digestOption, function(res){
                var challengeParams = parseDigest(res.headers['www-authenticate'])
                var ha1 = crypto.createHash('md5');
                ha1.update([username,challengeParams.realm,password].join(':'));
                console.log('h1 >>> '+ha1);
                var ha2 = crypto.createHash('md5');
                ha2.update(['GET',digestOption.path].join(':'));
                var response = crypto.createHash('md5');
                response.update([ha1,challengeParams.nonce,'1::auth',ha2].join(':'));
                var authRequestParams = {
                  username : username,
                  realm : challengeParams.realm,
                  nonce : challengeParams.nonce,
                  uri : digestOption.path, 
                  qop : challengeParams.qop,
                  response : response,
                  nc : '1',
                  cnonce : '',
                }
                digestOption.headers = { 'Authorization' : renderDigest(authRequestParams) }
                http.get(digestOption, function(res) {
                  res.setEncoding('utf-8')
                  var content = ''
                  res.on('data', function(chunk) {
                    content += chunk
                  }).on('end', function() {
                    console.log(content)
                  })
                });
            });

` 但是我得到了

  

400错误请求错误

我已经测试了从浏览器连接并获得了身份验证对话框。因此我的代码出了问题。

1 个答案:

答案 0 :(得分:0)

您可以使用URL本身使用URL技巧,如RFC 1738中所指定的那样。只需在主机前面传递用户/传递@符号。

var request = require('request'),
username = "john",
password = "1234",
url = "http://" + username + ":" + password + "@www.example.com";

request({
    url : url
},
function (error, response, body) {
    // Do more stuff with 'body' here
});
相关问题