I’m trying to send a request that requires HTTP Digest authentication.
Is Digest possible in jQuery?
If so, is this close to the correct way to do it? It’s not currently working.
<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>
No, the Digest Access Authentication Scheme is a little more complex as it implements a challenge-response authentication mechanism that requires the following steps:
This means there are at least two request/response pairs.
Each WWW-Authenticate response header field has the syntax:
So you need to parse the digest-challenge to get the parameters to be able to generate a digest-reponse for the Authorization request header field with the following syntax:
That section does also describe how the digest-response parameters are calculated. In particular, you will probably need an MD5 implementation as that’s the most commonly used algorithm for this authentication scheme.
Here is a simple tokenization that you can start with:
This will turn a WWW-Authenticate header field like:
into:
Then you need to parse the parameters (check existence and validity) and extract the values. Note that quoted-string values can be folded, so you need to unfold them (see also the use of the unquote function
unqin the RFC):With this you should be able to implement that on your own.