简体   繁体   English

Http摘要验证在node.js中失败

[英]Http Digest Authentication fails in node.js

I am trying to do a http get request with Digest authentication. 我正在尝试使用摘要身份验证进行http get请求。 i am posting my code below` 我在下面发布我的代码`

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)
                  })
                });
            });

` However i am getting `但是我越来越

400 Bad request error 400错误的请求错误

I have tested connecting from my browser and got the authentication dialog. 我已经测试了从浏览器进行的连接并获得了身份验证对话框。 Hence there is something wrong with my code. 因此,我的代码有问题。

you can use a trick using the URL itself, as specified in RFC 1738. Simply pass the user/pass before the host with an @ sign. 您可以使用RFC 1738中指定的URL本身的技巧。只需在用户之前将用户名/ pass传递给@符号即可。

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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM