Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/cam.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const http = require('http'),
events = require('events'),
url = require('url'),
util = require('util'),
splitargs = require('splitargs'),
linerase = require('./utils').linerase,
parseSOAPString = require('./utils').parseSOAPString,
splitArgs = require('./utils').splitArgs,
parseString = require('xml2js').parseString,
stripPrefix = require('xml2js').processors.stripPrefix,
emptyFn = function() {};
Expand Down Expand Up @@ -384,8 +384,9 @@ Cam.prototype._requestPart2 = function(options, callback) {
Cam.prototype._parseChallenge = function(digest) {
const prefix = 'Digest ';
const challenge = digest.substring(digest.indexOf(prefix) + prefix.length);
// use splitargs to handle things like qop="auth,auth-int"
let parts = splitargs(challenge,',', true); // get a list of Key=Value items. Whitespace will be consumed in the RegEx with \s before the RexEx Groups
// use splitArgs to handle things like qop="auth,auth-int"
// Digest algorithm=MD5,realm="happytimesoft",qop="auth,auth-int",nonce="36160F746AFA5913"
let parts = splitArgs(challenge);
// split into Key-Value pairs. We can split on '=' as this cannot appear in the Key name, replacing String with an Array in 'parts'
parts = parts.map(part => part.match(/^\s*?([a-zA-Z0-9]+)="?([^"]*)"?\s*?$/).slice(1));
return Object.fromEntries(parts);
Expand Down
40 changes: 36 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,40 @@ const guid = function() {
return (s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4());
};

/**
* Split Digest authentication string
* @param {string} args
* @returns {[string]}
*/
const splitArgs = function(args) {
let buffer = '';
const result = [];
let quoteOpen = false;
for (let i of args) {
if (quoteOpen) {
if (i === '"') {
quoteOpen = false;
}
buffer += i;
continue;
}
if (i === ',') {
result.push(buffer.trim());
buffer = '';
} else {
if (i === '"') {
quoteOpen = true;
}
buffer += i;
}
}
result.push(buffer.trim());
return result;
};

module.exports = {
linerase: linerase,
parseSOAPString: parseSOAPString,
guid: guid
};
linerase,
parseSOAPString,
guid,
splitArgs,
};
Loading