New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@bjowes/express-ntlm

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bjowes/express-ntlm

An express middleware to have simple NTLM-authentication. Temporary scoped release for NTLMv2

  • 2.4.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

NPM version

express-ntlm

An express middleware to have basic NTLM-authentication in node.js.

Upgrading from 1.0: The fields for username, domain and workstation have different names now: UserName, DomainName, Workstation.

Active Directory support is heavily inspired by PyAuthenNTLM2.

important notes on (reverse) proxies and NTLM

NTLM is designed for corporate networks without a proxy between the client and the application. It does authorise the TCP connection instead of the HTTP session and with a proxy between, it'll authorise the connection between the proxy and the application and therefore mixing up users if the proxy shares the same connection or "forgetting" users if the proxy suddenly uses a different connection for the same user.

In an early state of this module express-ntlm tried to create a session during the negotiation, which failed (see 50d9ac4) even though RFC6265 makes it clear it MUST be possible: "User agents [...] MUST process Set-Cookie headers contained in other responses (including responses with 400- and 500-level status codes)."

A possible solution to this problem might be to set the keep-alive property in nginx as mentioned in an answer from StackOverflow regarding this issue but it could end in the "multiple-users same-connection"-problem mentioned from another user.

Another option would be to abandon the proxy completely and connect directly to the application on port 80 or build a custom reverse proxy that authenticates the user, creates a session and keeps the session data on a shared store, that is accessible by all applications behind the proxy (e.g. expressjs/session in combination with visionmedia/connect-redis).

install

$ npm install express-ntlm

example usage

var express = require('express'),
    ntlm = require('express-ntlm');

var app = express();

app.use(ntlm({
    debug: function() {
        var args = Array.prototype.slice.apply(arguments);
        console.log.apply(null, args);
    },
    domain: 'MYDOMAIN',
    domaincontroller: 'ldap://myad.example',

    // use different port (default: 389)
    // domaincontroller: 'ldap://myad.example:3899',
}));

app.all('*', function(request, response) {
    response.end(JSON.stringify(request.ntlm)); // {"DomainName":"MYDOMAIN","UserName":"MYUSER","Workstation":"MYWORKSTATION"}
});

app.listen(80);

example with ldaps

var express = require('express'),
    ntlm = require('express-ntlm'),
    fs = require('fs');

var app = express();

app.use(ntlm({
    debug: function() {
        var args = Array.prototype.slice.apply(arguments);
        console.log.apply(null, args);
    },
    domain: 'MYDOMAIN',
    domaincontroller: 'ldaps://myad.example',
    tlsOptions: {
        //trusted certificate authorities (can be extracted from the server with openssh)
        ca: fs.readFileSync('./ca.pem'),
        //tells the tls module not to check the server's certificate (do not use in production)
        //rejectUnauthorized: false,
    }
}));

//same as above
app.all('*', function(request, response) {
    response.end(JSON.stringify(request.ntlm)); // {"DomainName":"MYDOMAIN","UserName":"MYUSER","Workstation":"MYWORKSTATION"}
});

app.listen(80);

without validation

It's not recommended, but it's possible to add NTLM-Authentication without validation. This means you can authenticate without providing valid credentials.

app.use(ntlm());

options

Nametypedefaultdescription
badrequestfunctionfunction(request, response, next) { response.sendStatus(400); }Function to handle HTTP 400 Bad Request.
internalservererrorfunctionfunction(request, response, next) { response.sendStatus(500); }Function to handle HTTP 500 Internal Server Error.
forbiddenfunctionfunction(request, response, next) { response.sendStatus(403); }Function to handle HTTP 403 Forbidden.
unauthorizedfunctionfunction(request, response, next) { response.statusCode = 401; response.setHeader('WWW-Authenticate', 'NTLM'); response.end(); }Function to handle HTTP 401 Unauthorized.
prefixstring[express-ntlm]The prefix is the first argument passed to the debug-function.
debugfunctionfunction() {}Function to log the debug messages. See logging for more details.
domainstringundefinedDefault domain if the DomainName-field cannot be parsed.
domaincontrollernull / string / arraynullOne or more domaincontroller(s) to handle the authentication. If null is specified the user is not validated. Active Directory is supported.
tlsOptionsobjectundefinedAn options object that will be passed to tls.connect and tls.createSecureContext. Only required when using ldaps and the server's certificate is signed by a certificate authority not in Node's default list of CAs. (or use NODE_EXTRA_CA_CERTS environment variable)
tlsOptions.castring / array / BufferundefinedOverride the trusted CA certificates provided by Node. Refer to tls.createSecureContext
getConnectionIdfunctionfunction(request, response) { return utils.uuidv4(); }Function to generate custom connection IDs, based optionally on the request and response objects.

logging (examples)

simple debugging to the console

function() {
    var args = Array.prototype.slice.apply(arguments);
    console.log.apply(null, args);
}

logging to debug (or similiar logging-utilities)

function() {
    var args = Array.prototype.slice.apply(arguments);
    debug.apply(null, args.slice(1)); // slice the prefix away, since debug is already prefixed
}

notes

All NTLM-fields (UserName, DomainName, Workstation) are also available within response.locals.ntlm, which means you can access it through your template engine (e.g. jade or ejs) while rendering (e.g. <%= ntlm.UserName %>).

Keywords

FAQs

Package last updated on 10 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc