@node-ntlm/axios
Version:
NTLM axios node utility function
69 lines • 3.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NtlmClient = exports.AxiosError = void 0;
const tslib_1 = require("tslib");
const http = tslib_1.__importStar(require("node:http"));
const https = tslib_1.__importStar(require("node:https"));
const core_1 = require("@node-ntlm/core");
const axios_1 = tslib_1.__importStar(require("axios"));
Object.defineProperty(exports, "AxiosError", { enumerable: true, get: function () { return axios_1.AxiosError; } });
const dev_null_1 = tslib_1.__importDefault(require("dev-null"));
/**
* @param credentials An NtlmCredentials object containing the username and password
* @param AxiosConfig The Axios config for the instance you wish to create
*
* @returns This function returns an axios instance configured to use the provided credentials
*/
function NtlmClient(credentials, AxiosConfig) {
const config = AxiosConfig ?? {};
if (!config.httpAgent) {
config.httpAgent = new http.Agent({ keepAlive: true });
}
if (!config.httpsAgent) {
config.httpsAgent = new https.Agent({ keepAlive: true });
}
const client = axios_1.default.create(config);
const safeCredentials = {
workstation: '',
...credentials,
};
client.interceptors.response.use(response => {
return response;
}, async (err) => {
const error = err.response;
if (error && error.status === 401) {
const ntlmMessage = (0, core_1.extractNtlmMessageFromAuthenticateHeader)(error.headers['www-authenticate']);
if (ntlmMessage) {
// This length check is a hack because SharePoint is awkward and will
// include the Negotiate option when responding with the T2 message
// There is nore we could do to ensure we are processing correctly,
// but this is the easiest option for now
if (ntlmMessage.length < 50) {
const t1Msg = (0, core_1.createType1Message)(safeCredentials);
error.config.headers['Authorization'] = t1Msg;
}
else {
const t3Msg = (0, core_1.createType3Message)((0, core_1.parseType2Message)(ntlmMessage), safeCredentials);
error.config.headers['X-retry'] = 'false';
error.config.headers['Authorization'] = t3Msg;
}
if (error.config.responseType === 'stream') {
const stream = err.response?.data;
// Read Stream is holding HTTP connection open in our
// TCP socket. Close stream to recycle back to the Agent.
if (stream && !stream.readableEnded) {
await new Promise(resolve => {
stream.pipe((0, dev_null_1.default)());
stream.once('close', resolve);
});
}
}
return client(error.config);
}
}
throw err;
});
return client;
}
exports.NtlmClient = NtlmClient;
//# sourceMappingURL=axios.js.map