我正在尝试使用一个使用Digest认证的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拒绝。服务器摘要认证已被测试。
错误在于指定了 username
冒号的参数,它应该是一个等号(username:"login"
与 username="login"
):
Authorization: Digest username="login", realm="Guard", nonce="7d0c753c2fb4cdc9480403547952f1", uri="/", algorithm=MD5, response="e9d8ad8f04e42672f2c21d70257c1072", qop=auth, nc=00000001, cnonce="bd5fd9b093dccaa1"