LDAP authentication with Node


Authentication with Node is fairly well established. Its components
are well documented. There are quite a few pieces of the puzzle
which, individually, are well understood. But its is a bit difficult
to find to find on the web a simple, integrated example on how to do
it. Here we will study a LDAP authentication example using Koa.
The solution is easily adaptable to other web frameworks (arguably
easier with Express) and authentication backends.

(#) Our components

We will be using the following:

- **Koa** is a web framework developed by the team behind Express as,
  more or less, a modern replacement. As Koa is more minimalistic than
  Express, this solution would actually be slightly simpler in express.
  In any case, its is very easy to adapt to Express
- **Passport** is the *de facto* standard framework for authentication
  in Node
- **Passport-ldapauth** is the Passport LDAP backend. Or as Passport
  calls it, 'Strategy'
  
A few other Koa packages will be needed:

- **Koa-passport** The bridge between Koa and Passport
- **Koa-router** is a routing middleware for Koa. Essential to map URLs to
  functions. Express doesn't need this as the functionality is built-in
- **Koa-session** Session management needed to keep user information
after login
- **Koa-bodyparser** to parse form responses

(#) The code

The complete code for this solution can be found on
[github](passport-ldapauth-example).

You will need to configure a JSON file called `dat.json`. See
`dat.json.example`

!!!
    You need to get this right or the code will not work

(#) Step-by-step guide

(#) Further reading

A great discussion on Passport workflow is provided by [Toon
Ketels](http://toon.io/understanding-passportjs-authentication-flow/). Highly
recommended