【问题标题】:Is Digest authentication possible with jQuery?使用 jQuery 可以进行摘要身份验证吗?
【发布时间】:2011-07-14 08:59:33
【问题描述】:

我正在尝试发送需要 HTTP Digest 身份验证的请求。

在 jQuery 中可以使用 Digest 吗?

如果是这样,这是否接近正确的方法?它目前不工作。

<script type="text/javascript">
    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'json',
        success: function() { alert('hello!'); },
        error: function() { alert('error')},
        beforeSend: setHeader

    });

    function setHeader(xhr){
        xhr.setRequestHeader("Authorization", "Digest username:password");
        xhr.setRequestHeader("Accept", "application/json");
    }
</script>

【问题讨论】:

    标签: jquery digest-authentication


    【解决方案1】:

    使用香草 javascript 是可能的。试试 digestAuthRequest.js:

    https://github.com/inorganik/digest-auth-request

    【讨论】:

      【解决方案2】:

      不,Digest Access Authentication Scheme 有点复杂,因为它实现了需要以下步骤的 challenge-response authentication mechanism

      1. 客户端发送访问受保护资源的请求,但未发送可接受的授权头字段
      2. 服务器以“401 Unauthorized”状态码和 WWW-Authenticate 标头字段(digest-challenge)响应
      3. 客户端针对同一资源发送另一个请求,但包含 Authorization 标头字段以响应挑战(digest-response
      4. 如果授权不成功,转步骤2;否则服务器正常运行。

      这意味着至少有两个请求/响应对。

      每个WWW-Authenticate response header field 的语法如下:

      challenge        =  "Digest" digest-challenge
      digest-challenge  = 1#( realm | [ domain ] | nonce |
                          [ opaque ] |[ stale ] | [ algorithm ] |
                          [ qop-options ] | [auth-param] )
      

      因此,您需要解析 digest-challenge 以获取能够为 Authorization request header field 生成 digest-reponse 的参数,语法如下:

      credentials      = "Digest" digest-response
      digest-response  = 1#( username | realm | nonce | digest-uri
                      | response | [ algorithm ] | [cnonce] |
                      [opaque] | [message-qop] |
                          [nonce-count]  | [auth-param] )
      

      该部分还描述了如何计算 digest-response 参数。特别是,您可能需要一个 MD5 实现,因为这是此身份验证方案最常用的算法

      这是一个简单的标记化,您可以从这里开始:

      var ws = '(?:(?:\\r\\n)?[ \\t])+',
          token = '(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2E\\x30-\\x39\\x3F\\x41-\\x5A\\x5E-\\x7A\\x7C\\x7E]+)',
          quotedString = '"(?:[\\x00-\\x0B\\x0D-\\x21\\x23-\\x5B\\\\x5D-\\x7F]|'+ws+'|\\\\[\\x00-\\x7F])*"',
          tokenizer = RegExp(token+'(?:=(?:'+quotedString+'|'+token+'))?', 'g');
      var tokens = xhr.getResponseHeader("WWW-Authentication").match(tokenizer);
      

      这将变成一个 WWW-Authenticate 标头字段,例如:

      WWW-Authenticate: Digest
              realm="testrealm@host.com",
              qop="auth,auth-int",
              nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
              opaque="5ccc069c403ebaf9f0171e9517f40e41"
      

      进入:

      ['Digest', 'realm="testrealm@host.com"', 'qop="auth,auth-int"', 'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093"', 'opaque="5ccc069c403ebaf9f0171e9517f40e41"']
      

      然后您需要解析参数(检查存在和有效性)并提取值。请注意,quoted-string 值可以折叠,因此您需要展开它们(另请参阅 RFC 中使用 unquote 函数 unq):

      function unq(quotedString) {
          return quotedString.substr(1, quotedString.length-2).replace(/(?:(?:\r\n)?[ \t])+/g, " ");
      }
      

      有了这个,你应该能够自己实现它。

      【讨论】:

      • 嗯,我同意你的回答,除了尽量不要使用 MD5 来生成你的身份验证令牌。
      • @pachanka 那么你为什么不同意 MD5 作为校验和的算法呢?
      • 作为校验和,我同意使用 MD5 或 SHA1,只要校验本身的寿命很短和/或保护密码本身以外的东西(可能是某种会话 ID)。 MD5 是加密存储密码的最常见函数这一事实本身就是一种危险。
      • 你读过MD5的实际使用方法吗? “一个有效的响应包含用户名的校验和(默认为 MD5 校验和)、密码、给定的 nonce 值、HTTP 方法和请求的 URI。”
      【解决方案3】:

      你应该试试 digestj jquery 插件。

      http://code.google.com/p/digestj/

      这是一个部分实现,但足以帮助您完成。

      【讨论】:

      • 如果我理解正确,这也需要在服务器端进行更改,对吧? (因为它使用DigestJ 作为身份验证方案名称而不是Digest
      猜你喜欢
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 2022-01-18
      相关资源
      最近更新 更多