axis-core
Version:
A Node.js library written in TypeScript containing shared behavior for the other packages, e.g. code handling communication and authentication with Axis Communication cameras.
61 lines • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = void 0;
const basic = require("./basic");
const digest = require("./digest");
const parse = (wwwAuthenticateHeader) => {
const challenge = toChallenge(wwwAuthenticateHeader);
if (challenge.type === basic.BASIC) {
return {
type: challenge.type,
realm: challenge.mustGet('realm'),
};
}
if (challenge.type === digest.DIGEST) {
return {
type: challenge.type,
realm: challenge.mustGet('realm'),
nonce: challenge.mustGet('nonce'),
qop: challenge.get('qop'),
opaque: challenge.get('opaque'),
algorithm: challenge.get('algorithm'),
};
}
throw new Error(`Auth protocol ${challenge.type} is not supported`);
};
exports.parse = parse;
const toChallenge = (wwwAuthenticateHeader) => {
const typeStopIndex = wwwAuthenticateHeader.indexOf(' ');
const type = wwwAuthenticateHeader.substring(0, typeStopIndex);
const params = wwwAuthenticateHeader
.substring(typeStopIndex)
.split(',')
.reduce((map, param) => {
const separatorIndex = param.indexOf('=');
const name = param.substring(0, separatorIndex).trim();
const value = param
.substring(separatorIndex + 1)
.trim()
.replace(/"/g, '');
map.set(name, value);
return map;
}, new Map());
return new Challenge(type, params);
};
class Challenge {
constructor(type, params) {
this.type = type;
this.params = params;
this.get = (name) => {
return this.params.get(name);
};
this.mustGet = (name) => {
const value = this.params.get(name);
if (value === undefined) {
throw new Error(`Challenge does not have required parameter "${name}"`);
}
return value;
};
}
}
//# sourceMappingURL=challenge.js.map