| 1 |
Node.js handler |
| 2 |
=============== |
| 3 |
|
| 4 |
Since version 2.0, a beta Node.js handler is available on |
| 5 |
`GitHub <https://github.com/LemonLDAPNG/node-lemonldap-ng-handler>`__ |
| 6 |
and `NPMJS <https://www.npmjs.com/package/lemonldap-ng-handler>`__. |
| 7 |
|
| 8 |
Up-to-date documentation is available on GitHub. |
| 9 |
|
| 10 |
Examples |
| 11 |
-------- |
| 12 |
|
| 13 |
**Important things**: |
| 14 |
|
| 15 |
- The above examples below are written for version 0.6.0 in CommonJS |
| 16 |
but Node.js handler can be used in ES7 and/or Typescript code |
| 17 |
- Rules and headers must be written in javascript for these hosts |
| 18 |
(example ``$uid eq "dwho"`` becomes ``$uid === "dwho"``) |
| 19 |
- Virtualhosts handled by node-lemonldap-ng-handler must be explicitly |
| 20 |
declared in your ``lemonldap-ng.ini`` file in ``[node-handler]`` |
| 21 |
section |
| 22 |
|
| 23 |
.. code-block:: ini |
| 24 |
|
| 25 |
[node-handler] |
| 26 |
|
| 27 |
nodeVhosts = test.example.com, test2.example.com |
| 28 |
|
| 29 |
Use it as FastCGI server (application protection only) |
| 30 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 31 |
|
| 32 |
FastCGI server |
| 33 |
^^^^^^^^^^^^^^ |
| 34 |
|
| 35 |
.. code-block:: javascript |
| 36 |
|
| 37 |
var handler = require('lemonldap-ng-handler'); |
| 38 |
|
| 39 |
handler.init({ |
| 40 |
configStorage: { |
| 41 |
"confFile": "/path/to/lemonldap-ng.ini" |
| 42 |
} |
| 43 |
}).then( () => { |
| 44 |
|
| 45 |
handler.nginxServer({ |
| 46 |
"mode": "fcgi", // or "http", default: fcgi |
| 47 |
"port": 9090, // default value |
| 48 |
"ip": 'localhost' // default value |
| 49 |
}); |
| 50 |
|
| 51 |
}); |
| 52 |
|
| 53 |
Nginx configuration |
| 54 |
^^^^^^^^^^^^^^^^^^^ |
| 55 |
|
| 56 |
.. code-block:: nginx |
| 57 |
|
| 58 |
server { |
| 59 |
#... |
| 60 |
# Internal authentication request |
| 61 |
location = /lmauth { |
| 62 |
internal; |
| 63 |
include /etc/nginx/fastcgi_params; |
| 64 |
fastcgi_pass localhost:9090; |
| 65 |
|
| 66 |
# Drop post data |
| 67 |
fastcgi_pass_request_body off; |
| 68 |
fastcgi_param CONTENT_LENGTH ""; |
| 69 |
|
| 70 |
# Prevent attacks on older Nginx versions (<1.29.5) |
| 71 |
fastcgi_param HTTP_HOST $host; |
| 72 |
|
| 73 |
# Keep original request (LLNG server will receive /lmauth) |
| 74 |
fastcgi_param X_ORIGINAL_URI $original_uri; |
| 75 |
} |
| 76 |
|
| 77 |
# Client requests |
| 78 |
location / { |
| 79 |
auth_request /lmauth; |
| 80 |
set $original_uri $uri$is_args$args; |
| 81 |
auth_request_set $lmremote_user $upstream_http_lm_remote_user; |
| 82 |
auth_request_set $lmlocation $upstream_http_location; |
| 83 |
error_page 401 $lmlocation; |
| 84 |
include conf/nginx-lua-headers.conf; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
Use it to protect an express app |
| 89 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 90 |
|
| 91 |
.. code-block:: javascript |
| 92 |
|
| 93 |
// Variables |
| 94 |
var express = require('express'); |
| 95 |
var app = express(); |
| 96 |
var handler = require('lemonldap-ng-handler'); |
| 97 |
|
| 98 |
// initialize handler (optional args) |
| 99 |
handler.init({ |
| 100 |
configStorage: { |
| 101 |
"confFile":"test/lemonldap-ng.ini" |
| 102 |
} |
| 103 |
}).then( () => { |
| 104 |
|
| 105 |
// and load it |
| 106 |
app.use(handler.run); |
| 107 |
|
| 108 |
// Then simply use your express app |
| 109 |
app.get('/', function(req, res) { |
| 110 |
return res.send('Hello ' + req.headers['Auth-User'] + ' !'); |
| 111 |
}); |
| 112 |
app.listen(3000, function() { |
| 113 |
return console.log('Example app listening on port 3000!'); |
| 114 |
}); |
| 115 |
}); |
| 116 |
|