UNPKG

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.

45 lines 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createCnonce = exports.createHeader = exports.DIGEST = void 0; const crypto_1 = require("crypto"); const url_1 = require("url"); const uuid_1 = require("uuid"); exports.DIGEST = 'Digest'; const createHeader = (method, url, username, password, challenge, cnonce) => { if (challenge.algorithm !== undefined && challenge.algorithm !== 'MD5') { throw new Error(`Unsupported digest algorithm: ${challenge.algorithm}`); } if (challenge.qop !== undefined && challenge.qop !== 'auth') { throw new Error(`Unsupported digest qop: ${challenge.qop}`); } if (challenge.qop === 'auth' && cnonce === undefined) { throw new Error('Digest argument error: cnonce must be specified when qop="auth"'); } const path = (0, url_1.parse)(url).path; const ha1 = md5(`${username}:${challenge.realm}:${password}`); const ha2 = md5(`${method}:${path}`); const response = challenge.qop === 'auth' ? md5(`${ha1}:${challenge.nonce}:00000001:${cnonce}:auth:${ha2}`) : md5(`${ha1}:${challenge.nonce}:${ha2}`); const params = [`username="${username}"`, `realm="${challenge.realm}"`, `nonce="${challenge.nonce}"`, `uri="${path}"`]; if (cnonce !== undefined) { params.push(`cnonce="${cnonce}"`); params.push('nc=00000001'); } if (challenge.qop !== undefined) { params.push(`qop=${challenge.qop}`); } params.push(`response="${response}"`); if (challenge.algorithm !== undefined) { params.push(`algorithm=${challenge.algorithm}`); } return `${challenge.type} ${params.join(', ')}`; }; exports.createHeader = createHeader; const createCnonce = () => { return (0, uuid_1.v4)().replace(/-/g, ''); }; exports.createCnonce = createCnonce; const md5 = (value) => { const hash = (0, crypto_1.createHash)('md5'); return hash.update(value).digest('hex'); }; //# sourceMappingURL=digest.js.map