Home

Awesome

express-passport-ldap-mongoose

Build Status Known Vulnerabilities

A turn key library that uses ldap-authentication with Passport and local database (MongoDB) to authenticate and save users

When an application needs to authenticate a user against an LDAP server, it normally also needs to save the user into local MongoDB for further references. express-passport-ldap-mongoose is designed to handle this requirement with a simple wrapper layer on top of expressjs, passportjs, ldap-authentication, and MongoDB.

Requirements

Installation

Using npm: npm install --save express-passport-ldap-mongoose

or using yarn: yarn add express-passport-ldap-mongoose

Usage

express-passport-ldap-mongoose configures passportjs and adds the login and logout route to your express app or router. All you need to do is call the initialize function of the library and everything else is taken care of.

const LdapAuth = require('express-passport-ldap-mongoose')
app.use(express.json())
app.use(sessionMiddleWare)
LdapAuth.initialize(options, app, findUserFunc, upsertUserFunc, loginPath, logoutPath)

Since version 3.1.0, you can still use init() but it is deprecated. Use initialize() instead which is simpler.

MongoDB model

When search for a user by its username in LDAP, a usernameAttribute is needed. The User model in local MongoDB must have the same key as the value of usernameAttribute that maps to the LDAP attribute. In some cases, and in the example we are using uid. it is used to uniquely identify a user and equals to the user's login username.

Parameters

Example

Complete example is in the example folder.

Another example on how to use Passport and ldap-authentication can be found in passport-ldap-example.

const mongoose = require('mongoose')
mongoose.Promise = Promise
mongoose.connect('mongodb://localhost/ldaptest')
const session = require('express-session')
const MongoStore = require('connect-mongo')(session)

const express = require('express')
const app = express()

const User = require('./model').User

const LdapAuth = require('express-passport-ldap-mongoose')

var sessionMiddleWare = session({
  secret: 'top session secret',
  store: new MongoStore({ mongooseConnection: mongoose.connection }),
  resave: true,
  saveUninitialized: true,
  unset: 'destroy',
  cookie: {
    httpOnly: false,
    maxAge: 1000 * 3600 * 24,
    secure: false, // this need to be false if https is not used. Otherwise, cookie will not be sent.
  }
})

// The order of the following middleware is very important!!
app.use(express.json())
app.use(sessionMiddleWare)
// use the library express-passport-ldap-mongoose
let usernameAttributeName = 'uid'
LdapAuth.initialize({
    ldapOpts: {
      url: 'ldap://localhost'
    },
    // note in this example it only use the user to directly
    // bind to the LDAP server. You can also use an admin
    // here. See the document of ldap-authentication.
    userDn: `uid={{username}},${ldapBaseDn}`,
    userSearchBase: ldapBaseDn,
    usernameAttribute: usernameAttributeName
  }, 
  app, 
  async (id) => {
    let user = await User.findOne({ usernameAttributeName: id }).exec()
    return user
  }, 
  async (user) => {
    let foundUser = await User.findOneAndUpdate({ username: user[usernameAttributeName] }, user, { upsert: true, new: true }).exec()
    return foundUser
  })

// serve static pages (where login.html resides)
app.use(express.static('public'))

// Start server
app.listen(4000, '127.0.0.1')