-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from perry-mitchell/digest
Digest auth (final)
- Loading branch information
Showing
7 changed files
with
208 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,64 @@ | ||
const { toBase64 } = require("./encode.js"); | ||
const { md5, ha1Compute } = require("./crypto.js"); | ||
|
||
function generateBasicAuthHeader(username, password) { | ||
const encoded = toBase64(`${username}:${password}`); | ||
return `Basic ${encoded}`; | ||
} | ||
|
||
function generateDigestAuthHeader(options, digest) { | ||
const url = options.url.replace("//", ""); | ||
const uri = url.indexOf("/") == -1 ? "/" : url.slice(url.indexOf("/")); | ||
const method = options.method ? options.method.toUpperCase() : "GET"; | ||
const qop = /(^|,)\s*auth\s*($|,)/.test(digest.qop) ? "auth" : false; | ||
const ncString = `00000000${digest.nc}`.slice(-8); | ||
const cnonce = digest.cnonce; | ||
const ha1 = ha1Compute( | ||
digest.algorithm, | ||
digest.username, | ||
digest.realm, | ||
digest.password, | ||
digest.nonce, | ||
digest.cnonce | ||
); | ||
const ha2 = md5(`${method}:${uri}`); | ||
const digestResponse = qop | ||
? md5(`${ha1}:${digest.nonce}:${ncString}:${digest.cnonce}:${qop}:${ha2}`) | ||
: md5(`${ha1}:${digest.nonce}:${ha2}`); | ||
|
||
const authValues = { | ||
username: digest.username, | ||
realm: digest.realm, | ||
nonce: digest.nonce, | ||
uri, | ||
qop, | ||
response: digestResponse, | ||
nc: ncString, | ||
cnonce: digest.cnonce, | ||
algorithm: digest.algorithm, | ||
opaque: digest.opaque | ||
}; | ||
|
||
const authHeader = []; | ||
for (var k in authValues) { | ||
if (authValues[k]) { | ||
if (k === "qop" || k === "nc" || k === "algorithm") { | ||
authHeader.push(`${k}=${authValues[k]}`); | ||
} else { | ||
authHeader.push(`${k}="${authValues[k]}"`); | ||
} | ||
} | ||
} | ||
|
||
return `Digest ${authHeader.join(", ")}`; | ||
} | ||
|
||
function generateTokenAuthHeader(tokenInfo) { | ||
return `${tokenInfo.token_type} ${tokenInfo.access_token}`; | ||
} | ||
|
||
module.exports = { | ||
generateBasicAuthHeader, | ||
generateTokenAuthHeader | ||
generateTokenAuthHeader, | ||
generateDigestAuthHeader | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { createHash } = require("crypto"); | ||
|
||
function md5(data) { | ||
return createHash("md5").update(data).digest("hex"); | ||
} | ||
|
||
function ha1Compute(algorithm, user, realm, pass, nonce, cnonce) { | ||
const ha1 = md5(`${user}:${realm}:${pass}`); | ||
if (algorithm && algorithm.toLowerCase() === "md5-sess") { | ||
return md5(`${ha1}:${nonce}:${cnonce}`); | ||
} else { | ||
return ha1; | ||
} | ||
} | ||
|
||
module.exports = { | ||
md5, | ||
ha1Compute | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const axios = require("axios"); | ||
const { merge } = require("./merge.js"); | ||
const { getPatcher } = require("./patcher.js"); | ||
const { generateDigestAuthHeader } = require("./auth.js"); | ||
|
||
function makeNonce() { | ||
const cnonceSize = 32; | ||
const nonceRaw = "abcdef0123456789"; | ||
|
||
let uid = ""; | ||
for (let i = 0; i < cnonceSize; ++i) { | ||
uid += nonceRaw[Math.floor(Math.random() * nonceRaw.length)]; | ||
} | ||
return uid; | ||
} | ||
|
||
function parseAuth(response, _digest) { | ||
const authHeader = response.headers["www-authenticate"] || ""; | ||
|
||
if (authHeader.split(/\s/)[0].toLowerCase() !== "digest") { | ||
return false; | ||
} | ||
|
||
const re = /([a-z0-9_-]+)=(?:"([^"]+)"|([a-z0-9_-]+))/gi; | ||
for (;;) { | ||
var match = re.exec(authHeader); | ||
if (!match) { | ||
break; | ||
} | ||
_digest[match[1]] = match[2] || match[3]; | ||
} | ||
|
||
_digest.nc++; | ||
_digest.cnonce = makeNonce(); | ||
|
||
return true; | ||
} | ||
|
||
function request(requestOptions) { | ||
return getPatcher().patchInline("request", options => axios(options), requestOptions); | ||
} | ||
|
||
function fetch(requestOptions) { | ||
// Client not configured for digest authentication | ||
if (!requestOptions._digest) { | ||
return request(requestOptions); | ||
} | ||
|
||
// Remove client's digest authentication object from request options | ||
const _digest = requestOptions._digest; | ||
delete requestOptions._digest; | ||
|
||
// If client is already using digest authentication, include the digest authorization header | ||
if (_digest.hasDigestAuth) { | ||
requestOptions = merge(requestOptions, { | ||
headers: { | ||
Authorization: generateDigestAuthHeader(requestOptions, _digest) | ||
} | ||
}); | ||
} | ||
|
||
// Perform the request and handle digest authentication | ||
return request(requestOptions).then(function(response) { | ||
if (response.status == 401) { | ||
_digest.hasDigestAuth = parseAuth(response, _digest); | ||
|
||
if (_digest.hasDigestAuth) { | ||
requestOptions = merge(requestOptions, { | ||
headers: { | ||
Authorization: generateDigestAuthHeader(requestOptions, _digest) | ||
} | ||
}); | ||
|
||
return request(requestOptions).then(function(response2) { | ||
if (response2.status == 401) { | ||
_digest.hasDigestAuth = false; | ||
} else { | ||
_digest.nc++; | ||
} | ||
return response2; | ||
}); | ||
} | ||
} else { | ||
_digest.nc++; | ||
} | ||
return response; | ||
}); | ||
} | ||
|
||
module.exports = fetch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters