Skip to content

Instantly share code, notes, and snippets.

@tippexs
Last active March 4, 2021 10:05
Show Gist options
  • Save tippexs/ac18f8989cc0e8b6ea210f38bc2c7547 to your computer and use it in GitHub Desktop.
Save tippexs/ac18f8989cc0e8b6ea210f38bc2c7547 to your computer and use it in GitHub Desktop.
function auth(r) {
r.subrequest("/auth2", {}, reply => {
var header = reply.headersOut['WWW-Authenticate'];
r.log(header);
var nonce = header.match(/(nonce=")(.*?)(?=")/i);
r.log("************nonce: " + nonce[2]);
r.variables.digest_header = digest(r, nonce[2]);
r.log("********** New Header ************");
r.log(r.variables.digest_header);
r.subrequest("/auth", {}, reply => {
r.headersOut["Content-Type"] = "text/html";
r.return(reply.status, reply.responseBody);
return;
});
});
}
function digest(r, nonce) {
var crypto = require('crypto');
var A1 = null;
var A2 = null;
var response = null;
var nc = "00000001";
var cnonce = crypto.createHash('sha1').update("sdsdfadaf").update('Bdfadfadf').digest('hex');
var realm = "test";
var username = "dummy";
var uri = "/auth";
var qop = "auth";
A1 = crypto.createHash("md5").update(username+":"+realm+":dummy123").digest('hex');
A2 = crypto.createHash("md5").update("GET:"+uri).digest('hex');
response = crypto.createHash("md5").update(A1+":"+nonce+":"+nc+":"+cnonce+":"+qop+":"+A2).digest('hex');
r.log("************** resonse MD5: "+response);
var header = null;
header = 'Digest username="'+username+'", realm="'+realm+'", nonce="'+nonce+'", uri="'+uri+'", cnonce="'+cnonce+'", nc='+nc+', qop='+qop+', response="'+response+'", algorithm="MD5"';
return header;
}
export default {auth, digest};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment