node.js - nodejs HTTP 摘要身份验证不起作用

标签 node.js https digest digest-authentication

我一直在深入研究堆栈溢出,但未能解决我的问题。我正在尝试访问使用摘要的 API,但没有成功,而且我的同事也无法确定问题。我碰壁了,来到 Stack Overflow 来问我的问题。

这是我的身份验证代码:

var https = require("https"),
    crypto = require('crypto'),
    _ = require('underscore');

var options = {
    host: 'api.example.com',
    port: 80,
    path: '/path/to/uri/',
    method: 'GET',
    accept: 'application/json',
    acceptEncoding: 'gzip, deflate',
    connection: 'keep-alive',
    rejectUnauthorized: false,
    requestCert: true,
    agent: false
};

var username = 'username',
    password = 'httppassword';

var req = https.get(options, function(res) {

    res.setEncoding('utf-8');

    console.log(res.url);
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));

    var data = "";

    res.on('data', function (chunk) {
        data = data + chunk;
    });

    res.on('end', function(){

        console.log(data);
        var challengeParams = parseDigest(res.headers['www-authenticate']);
        console.log(challengeParams);
        var ha1 = crypto.createHash('md5').update(username + ':' + challengeParams.realm + ':' + password).digest('hex');
        var ha2 = crypto.createHash('md5').update('GET:' + options.path).digest('hex');
        var response = crypto.createHash('md5').update(ha1 + ':' + challengeParams.nonce + ':1::auth:' + ha2).digest('hex');
        var authRequestParams = {
            username : username,
            realm : challengeParams.realm,
            nonce : challengeParams.nonce,
            uri : options.path, 
            qop : challengeParams.qop,
            response : response,
            nc : 1,
            cnonce : ''
        };
        options.headers = { 'Authorization' : renderDigest(authRequestParams) };
        console.log(options);
        https.get(options, function(res) {

            console.log("STATUS: " + res.statusCode);
            console.log("HEADERS: "  + JSON.stringify(res.headers));

            res.setEncoding('utf-8');
            var content = '';
            res.on('data', function(chunk) {
                content += chunk;
            }).on('end', function() {
                console.log(content);
            });
        })
    });

});

req.on('error' ,function(err){
    console.log("request");
    console.log(err);
});

req.write('data\n');
req.write('data\n');
req.end();

这是 API 发回的质询 header

{ realm: 'API realm',
  domain: 'https:/api.example.com/',
  qop: 'auth',
  nonce: 'UZ43b0FWC9591pMjy1i6H2okVwgMbDVO6fcgcQ' }

编辑:

我认为,对于那些希望为我回答这个问题的人提供我实际发送回 API 的内容会很有帮助,所以就在这里。

{ host: 'api.example.com',
  port: 80,
  path: '/path/to/uri/',
  method: 'GET',
  accept: 'application/json',
  acceptEncoding: 'gzip, deflate',
  connection: 'keep-alive',
  rejectUnauthorized: false,
  requestCert: true,
  agent: false,
  headers: { Authorization: 'Digest username="uname", realm="API realm", nonce="UZ43b0FWC9591pMjy1i6H2okVwgMbDVO6fcgcQ", uri="/path/to/uri", qop="auth", response="09c536e22bca031cdbcb289e4065064a", nc="1", cnonce=""' } }

最佳答案

您可以使用http-auth支持摘要认证的模块

// HTTP module
var http = require('http');

// Authentication module.
var auth = require('http-auth');
var digest = auth.digest({
    realm: "Simon Area.",
    file: __dirname + "/../data/users.htdigest" // vivi:anna, sona:testpass
});

// Creating new HTTP server.
http.createServer(digest, function(req, res) {
    res.end("Welcome to private area - " + req.user + "!");
}).listen(1337);

关于node.js - nodejs HTTP 摘要身份验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25246601/

相关文章:

node.js - Google Calendar API v3 服务帐户在创建事件时不发送通知

node.js - 推送到 Heroku (node.js) 时出错

node.js - 跳过 Gulp 的本地安装

javascript - Nodejs 将服务器 HTTP 更改为 HTTPS

PHP 摘要 HTTP 身份验证示例不起作用,为什么?

javascript - 表达式的 Angular 观察者

node.js - 加载模块时的nodejs异常处理

node.js - HTTPS 请求,指定主机名和具体 IP 地址

file-upload - 从 HTTPS 托管站点直接上传到 CORS 上的 S3(无需 Flash)

security - 摘要式身份验证如何防止重放攻击?