vesse / node-ldapauth-fork

Simple node.js module to authenticate against an LDAP server
Other
127 stars 79 forks source link

connect.basicAuth is missing - please provide working example for basic authentication #38

Closed godmar closed 8 years ago

godmar commented 8 years ago

connect no longer has a basicAuth method, because I believe they moved all middleware outside.

This mean that the example is broken.

Could it be fixed?

I'm asking because it took me a long time to figure out how to fix it, I ended up with the following middleware to support basic authentication via LDAP:

const basicAuth = require('basic-auth');
const basicAuthenticator = (req, res, next) => {
    function reject() {
        res.statusCode = 401
        res.setHeader('WWW-Authenticate', 'Basic realm="please use your SLO"')
        res.end('Access denied')
    }

    var credentials = basicAuth(req)
    if (!credentials) return reject();

    ldap.authenticate(credentials.name, credentials.pass, function (err, user) {
        if (err) return reject();
        req.user = credentials.name;
        next();
    });
}

which I am not certain of, quite frankly, whether it handles all errors correctly.

Thank you!