使用nodejs进行摘要式身份验证不起作用

时间:2019-07-10 12:32:17

标签: node.js authentication digest-authentication

我正在尝试连接到使用摘要身份验证的monero-rpc服务器

但是,我总是收到401 - Unauthorized access

我测试了calculateResponse函数-它返回与Postman相同的哈希值。

所以看来Authorization标头和请求本身存在问题,但是我看不到问题。

这是我的代码:

        const http = require("http");
        const crypto = require("crypto");

        // function from class - uses username/pass to calculate response
          calculateResponse(nonce, realm) {
            const MD5 = data =>
              crypto
                .createHash("MD5")
                .update(data)
                .digest("hex");
            const HA1 = MD5(`${this.auth.username}:${realm}:${this.auth.password}`);
            const HA2 = MD5(`${this.options.method}:${this.options.path}`);
            const response = MD5(`${HA1}:${nonce}:00000001:0a4f113b:auth:${HA2}`);
            return response;
          }

        (async function() {
          // this just makes 1 request and parse nonce from www-auth
          const nonce = await getNonce()
          const  response = await calculateResponse(nonce, 'monero-rpc');

          const options = {
            method: "GET",
            url: "http://127.0.0.1:18082/json_rpc",
            headers: {
              "cache-control": "no-cache",
              Authorization: `Digest username="${this.auth.username}", realm="monero-rpc", nonce="${nonce}", uri="/json_rpc", algorithm="MD5", qop=auth, nc=00000002, cnonce="0a4f113b", response="${response}"`,
              "Content-Type": "application/json"
            }
          };

          request(options, (error, response, body) {

            console.log(body);
          });
        })();

0 个答案:

没有答案