Note that there are some explanatory texts on larger screens.

plurals
  1. POCant get digest auth to work with node.js
    text
    copied!<p>I'm trying to get a simple (!) digest authentication working with node js using an an API from gathercontent.com.</p> <p>Everything seems to be working except I still get a "Wrong credentials" response that looks like this:</p> <pre><code>{ success: false, error: 'Wrong Credentials!' } </code></pre> <p>The code looks like this:</p> <pre><code>var https = require('https'), qs = require('querystring'); apikey = "[my api key goes in here]", pwd = "[my password goes in here]", crypto = require('crypto'); module.exports.apiCall = function () { var options = { host:'abcdefg.gathercontent.com', port:443, path:'/api/0.1/get_pages_by_project/get_me', method:'POST', headers:{ "Accept":"application/json", "Content-Type":"application/x-www-form-urlencoded" } }; var req = https.request(options, function (res) { res.on('data', function (d) { var creds = JSON.parse(d); var parsedDigest = parseDigest(res.headers['www-authenticate']); console.log(parsedDigest); var authopts = { host:'furthercreative.gathercontent.com', port:443, path:'/api/0.1/get_pages_by_project/get_me', method:'POST', headers:{ "Accept":"application/json", "Content-Type":"application/x-www-form-urlencoded", "Authorization" : getAuthHeader(parsedDigest, apikey, parsedDigest['Digest realm'], pwd) } }; console.log(authopts); console.log('\n\n\n'); var req2 = https.request(authopts, function (res2) { console.log("statusCode: ", res2.statusCode); console.log("headers: ", res2.headers); res2.on('data', function (d2) { var result = JSON.parse(d2); }); }); req2.end(); }); }); req.write('id=1234'); req.end(); req.on('error', function (e) { console.error(e); }); }; function parseDigest(s){ var parts = s.split(','); var obj = {}; var nvp = ''; for(var i = 0; i &lt; parts.length; i++){ nvp = parts[i].split('='); obj[nvp[0]] = nvp[1].replace(/"/gi, ''); } return obj; } function getAuthHeader(digest, apikey, realm, pwd){ var md5 = crypto.createHash('md5'); var s = ''; var nc = '00000001'; var cn = '0a4f113b'; var HA1in = apikey+':'+realm+':'+pwd; md5 = crypto.createHash('md5'); md5.update(HA1in); var HA1out = md5.digest('hex'); var HA2in = 'POST:/api/0.1/get_pages_by_project/get_me'; md5 = crypto.createHash('md5'); md5.update(HA2in); var HA2out = md5.digest('hex'); md5 = crypto.createHash('md5'); var respIn = HA1out + ':' + digest.nonce + ':'+nc+':'+cn+':'+digest.qop+':'+ HA2out; md5.update(respIn); var resp = md5.digest('hex'); s = [ 'Digest username="',apikey,'", ', 'realm="',digest['Digest realm'],'", ', 'nonce="',digest.nonce,'", ', 'uri="/api/0.1/get_pages_by_project/get_me", ', 'cnonce="',cn,'", ', 'nc="',nc,'", ', 'qop="',digest.qop,'", ', 'response="',resp,'", ', 'opaque="',digest.opaque,'"'].join('') return s; } </code></pre> <p>I'd try and Curl to it but I'm not sure how!</p> <p>Any help appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload