我尝试使用摘要认证与一款API进行身份验证。
我向服务器发送了一个POST请求,但返回的响应是HTTP 401 Denied
。以下是服务器返回的WWW-Authenticate
挑战头:
(斜杠用于格式化,不在响应头中)
WWW-Authenticate: Digest realm="Guard", domain="/", \
nonce="MTMzOTA5Mjk1NTE2NDo0NzY2NjJiOTgyMjE1ZDc0OWU3NzM5MTkzMWNjNGQzNw==", \
algorithm=MD5, qop="auth"
使用此标头中的参数,我应用摘要认证算法并构建挑战回复标头:
const HA1 = MD5("login:Guard:mypassword");
const HA2 = MD5("POST:/");
const authHash = MD5(
HA1 + ':' + unquotes(tokensObj["nonce"]) + ':' +
tokensObj["nc"] + ':' + tokensObj["cnonce"] + ':' +
unquotes(tokensObj["qop"]) + ':' + HA2
);
const challengeReply = 'Digest username:"login"' +
', realm=' + tokensObj["realm"] + ', nonce=' + tokensObj["nonce"] +
', uri=' + tokensObj["domain"] + ', algorithm=' + tokensObj["algorithm"] +
', response="' + authHash + '"' + ', qop=' + unquotes(tokensObj["qop"]) +
', nc=' + tokensObj["nc"] + ', cnonce="' + tokensObj["cnonce"] + '"';
xhr.setRequestHeader("Authorization", challengeReply);
发送到服务器的标头:
Authorization: Digest username:"login", realm="Guard", \
nonce="7d0c753c2fb4cdc9480403547952f1", uri="/", algorithm=MD5, \
response="e9d8ad8f04e42672f2c21d70257c1072", qop=auth, nc=00000001, \
cnonce="bd5fd9b093dccaa1"
但这并不起作用,我仍然收到 HTTP 401 Denied。服务器的摘要认证已经被测试过了。