|
1 | 1 | # express-ntlm |
2 | 2 |
|
3 | | -an express middleware to have basic NTLM-authentication in node.js. |
| 3 | +An express middleware to have basic NTLM-authentication in node.js. |
| 4 | + |
| 5 | +> **Upgrading from 1.0:** As of v2.0.0 `express-ntlm` requires [`express-session`](https://github.com/expressjs/session). Also the fields for username, domain and workstation have different names now. |
| 6 | +
|
| 7 | +*Active Directory support is heavily inspired by [PyAuthenNTLM2](https://github.com/Legrandin/PyAuthenNTLM2/).* |
4 | 8 |
|
5 | 9 | ## install |
6 | 10 |
|
7 | 11 | $ npm install express-ntlm |
8 | 12 |
|
9 | | -## usage |
| 13 | +## example usage |
| 14 | + |
| 15 | + var express = require('express'), |
| 16 | + ntlm = require('./lib/express-ntlm'), |
| 17 | + session = require('express-session'); |
10 | 18 |
|
11 | | - var express = require('express'); |
12 | 19 | var app = express(); |
13 | | - var ntlm = require('express-ntlm'); |
14 | | - |
15 | | - app.all('*', ntlm()); // authenticate for all paths |
16 | | - |
17 | | - app.get('/', function(request, response) { |
18 | | - response.send(request.ntlm); // { target: 'MYDOMAIN', userid: 'MYUSERID', workstation: 'MYWORKSTATION' } |
| 20 | + |
| 21 | + // express-ntlm requires a session to store its connection |
| 22 | + app.use(session({ |
| 23 | + secret: 'ntlm-secret', |
| 24 | + resave: true, |
| 25 | + saveUninitialized: true |
| 26 | + })); |
| 27 | + |
| 28 | + app.use(ntlm({ |
| 29 | + debug: function() { |
| 30 | + var args = Array.prototype.slice.apply(arguments); |
| 31 | + console.log.apply(null, args); |
| 32 | + }, |
| 33 | + domain: 'MYDOMAIN', |
| 34 | + domaincontroller: 'ldap://myad.example', |
| 35 | + })); |
| 36 | + |
| 37 | + app.all('*', function(request, response) { |
| 38 | + response.end(JSON.stringify(request.ntlm)); // {"DomainName":"MYDOMAIN","UserName":"MYUSER","Workstation":"MYWORKSTATION"} |
19 | 39 | }); |
| 40 | + |
| 41 | + app.listen(80); |
| 42 | + |
| 43 | +## options |
| 44 | + |
| 45 | +| Name | type | default | description | |
| 46 | +|------|------|---------|-------------| |
| 47 | +| `badrequest` | `function` | `function(request, response, next) { response.sendStatus(400); }` | Function to handle HTTP 400 Bad Request. | |
| 48 | +| `internalservererror` | `function` | `function(request, response, next) { response.sendStatus(500); }` | Function to handle 500 Internal Server Error. | |
| 49 | +| `forbidden` | `function` | `function(request, response, next) { response.sendStatus(403); }` | Function to handle HTTP 403 Forbidden. | |
| 50 | +| `prefix` | `string` | `[express-ntlm]` | The prefix is the first argument passed to the `debug`-function. | |
| 51 | +| `debug` | `function` | `function() {}` | Function to log the debug messages. See [logging](#logging) for more details. | |
| 52 | +| `domain` | `string` | `undefined` | Default domain if the DomainName-field cannot be parsed. | |
| 53 | +| `domaincontroller` | `null` / `string` / `array` | `null` | One or more domaincontroller(s) to handle the authentication. If `null` is specified the user is not validated. | |
| 54 | + |
| 55 | +<a name="logging" /> |
| 56 | +## logging (examples) |
| 57 | + |
| 58 | +### simple debugging to the console |
| 59 | + |
| 60 | + function() { |
| 61 | + var args = Array.prototype.slice.apply(arguments); |
| 62 | + console.log.apply(null, args); |
| 63 | + } |
20 | 64 |
|
21 | | - app.listen(3000); |
22 | | - |
| 65 | +### logging to [debug](https://github.com/visionmedia/debug) |
| 66 | + |
| 67 | + function() { |
| 68 | + var args = Array.prototype.slice.apply(arguments); |
| 69 | + debug.apply(null, args.slice(1)); // slice the prefix away, since debug is already prefixed |
| 70 | + } |
| 71 | + |
23 | 72 | ### notes |
24 | 73 |
|
25 | 74 | ntlm is also available within `response.locals` which means you can access it through your template engine (e.g. jade or ejs) using `ntlm`. |
0 commit comments