HTTP NTLM Authentication for Node.js
httpntlm is a Node.js library designed to facilitate HTTP NTLM authentication, a protocol used in Windows environments. It is a direct port of the Python `python-ntlm` library and notably includes support for NTLMv2, which handles extended security and target information negotiations. The current stable version is 1.8.13, and while it's a mature library with over a decade of history, its release cadence appears infrequent, with the last significant README update in March 2023. Key differentiators include its focused implementation of NTLM for Node.js, offering both a high-level API for common use cases (GET, POST, etc.) and granular access to NTLM message creation and parsing for advanced scenarios. It supports both HTTP and HTTPS connections, and allows for pre-encrypting passwords for enhanced security. The library relies on other modules like `httpreq`, `async`, and `agentkeepalive` for its underlying HTTP requests and flow control.
Common errors
-
www-authenticate not found on response of second request
cause During the NTLM handshake, the server must respond with a `WWW-Authenticate` header containing the Type 2 challenge message. This error indicates the server did not provide the expected challenge, likely due to an invalid initial Type 1 message or incorrect server configuration.fixVerify the URL, username, password, workstation, and domain are correct. Ensure the server endpoint is indeed NTLM-protected and configured correctly. Debug the initial Type 1 message sent to confirm it's well-formed. -
NTLM authentication failed: Invalid credentials
cause This is a generic authentication failure, usually stemming from incorrect `username`, `password`, `workstation`, or `domain` parameters. It can also occur if the NTLM hashes generated are incorrect.fixDouble-check all authentication parameters for typos or incorrect values. Ensure the user account has access to the resource. If pre-encrypting passwords, verify the `lm_password` and `nt_password` buffers are generated correctly from the plaintext password. Consider setting `domain` to an empty string if unsure. -
TypeError: require is not a function
cause This error occurs when attempting to use `require()` syntax in an ECMAScript Module (ESM) context. The `httpntlm` library is designed for CommonJS.fixIf your project is ESM, either switch to dynamic `await import('httpntlm')` (if supported and appropriate for your use case) or configure your build system to handle CommonJS modules. If possible, consider setting `"type": "commonjs"` in your `package.json` or changing file extensions to `.cjs` for files that use `require()`.
Warnings
- gotcha The library explicitly states that it assumes the server supports NTLMv2 and creates responses accordingly. If the server only supports NTLMv1 and does not negotiate NTLMv2 extended security, this assumption might lead to authentication failures or unexpected behavior.
- deprecated The package's specified Node.js engine requirement is `>=10.4.0`, a very old version of Node.js. While the library might function on newer Node.js versions, official support and compatibility testing beyond Node.js 10 may be limited, potentially leading to unforeseen issues.
- breaking The package is primarily CommonJS (CJS). Attempting to `import` it directly in a pure ECMAScript Module (ESM) Node.js project (e.g., with `"type": "module"` in `package.json`) will result in a `TypeError: require is not a function` or similar module resolution errors.
- gotcha The Snyk security scan badge in the README indicates 'Known Vulnerabilities'. While the Snyk Vulnerability Database currently shows no *direct* vulnerabilities for `httpntlm` itself, it's crucial to check its *dependencies* for vulnerabilities which may be indirectly introduced.
Install
-
npm install httpntlm -
yarn add httpntlm -
pnpm add httpntlm
Imports
- httpntlm
import httpntlm from 'httpntlm';
const httpntlm = require('httpntlm'); - httpntlm.get
const httpntlm = require('httpntlm'); httpntlm.get({ /* options */ }, callback); - httpntlm.ntlm
import { ntlm } from 'httpntlm';const { ntlm } = require('httpntlm'); // or const ntlm = require('httpntlm').ntlm;
Quickstart
const httpntlm = require('httpntlm');
httpntlm.get({
url: "https://someurl.com", // Replace with your NTLM-protected URL
username: process.env.NTLM_USERNAME ?? '',
password: process.env.NTLM_PASSWORD ?? '',
workstation: process.env.NTLM_WORKSTATION ?? 'local_workstation',
domain: process.env.NTLM_DOMAIN ?? ''
}, function (err, res){
if(err) {
console.error("NTLM GET request failed:", err.message); // Log the error message
return;
}
console.log("Status Code:", res.statusCode);
console.log("Response Headers:", res.headers);
console.log("Response Body (truncated):");
console.log(res.body ? res.body.substring(0, 500) + '...' : '[No Body]'); // Truncate body for readability
});