UNPKG

@node-ntlm/httpreq

Version:

NTLM httpreq node utility function

108 lines 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.options = exports.del = exports.post = exports.patch = exports.put = exports.get = exports.method = void 0; const tslib_1 = require("tslib"); const http = tslib_1.__importStar(require("node:http")); const https = tslib_1.__importStar(require("node:https")); const node_url_1 = require("node:url"); const node_util_1 = require("node:util"); const core_1 = require("@node-ntlm/core"); const httpreq_1 = tslib_1.__importDefault(require("httpreq")); async function method(stringMethod, options) { // extract non-ntlm-options: const { url, username, password, workstation, domain, lm_password, nt_password, ...httpreqOptions } = { workstation: '', domain: '', ...options, }; // is https? const reqUrl = (0, node_url_1.parse)(options.url); const isHttps = reqUrl.protocol == 'https:'; // set keepaliveAgent (http or https): let keepaliveAgent; if (isHttps) { keepaliveAgent = new https.Agent({ keepAlive: true }); } else { keepaliveAgent = new http.Agent({ keepAlive: true }); } // build type1 request: async function sendType1Message() { const type1msg = (0, core_1.createType1Message)({ domain, workstation }); const { headers, body, ...safeOptions } = httpreqOptions; const type1options = { ...safeOptions, headers: { Connection: 'keep-alive', Authorization: type1msg, }, timeout: options.timeout || 0, agent: keepaliveAgent, allowRedirects: false, // don't redirect in httpreq, because http could change to https which means we need to change the keepaliveAgent }; // send type1 message to server: return (await (0, node_util_1.promisify)(httpreq_1.default[stringMethod])(options.url, type1options)); } async function sendType3Message(res) { // catch redirect here: if (res.headers.location) { return method(stringMethod, { ...options, url: res.headers.location }); } const ntlmMessage = (0, core_1.extractNtlmMessageFromAuthenticateHeader)(res.headers['www-authenticate']); if (!ntlmMessage) { throw new Error('www-authenticate not found on response of second request'); } // parse type2 message from server: const type2msg = (0, core_1.parseType2Message)(ntlmMessage); // create type3 message: const type3msg = (0, core_1.createType3Message)(type2msg, { domain, workstation, username, password, nt_password, lm_password, }); // build type3 request: const type3options = { allowRedirects: false, agent: keepaliveAgent, ...httpreqOptions, headers: { Connection: 'Close', Authorization: type3msg, ...httpreqOptions.headers, }, }; // send type3 message to server: return (await (0, node_util_1.promisify)(httpreq_1.default[stringMethod])(options.url, type3options)); } const res = await sendType1Message(); return sendType3Message(res); } exports.method = method; function get(options) { return method('get', options); } exports.get = get; function put(options) { return method('put', options); } exports.put = put; function patch(options) { return method('patch', options); } exports.patch = patch; function post(options) { return method('post', options); } exports.post = post; function del(options) { return method('delete', options); } exports.del = del; function options(options) { return method('options', options); } exports.options = options; //# sourceMappingURL=httpreq.js.map