Git Product home page Git Product logo

passport-adauth's Introduction

passport-adauth

Passport authentication strategy against an Active Directory server. This module is a Passport strategy wrapper for ldapauth-fork

Install

npm install passport-adauth

Usage

Configure strategy

var ActiveDirectoryStrategy  = require('passport-adauth');

passport.use(new ActiveDirectoryStrategy({
    server: {
      host: 'ad.company.com',
      principal: 'company.com'
    }
  }));
  • server: LDAP settings. These are passed directly to ldapauth-fork. See its documentation for all available options.

    • url: e.g. ldap://localhost:389
    • bindDn: e.g. cn='root'
    • bindCredentials: Password for bindDn
    • searchBase: e.g. o=users,o=example.com
    • searchFilter: LDAP search filter, e.g. (uid={{username}}). Use literal {{username}} to have the given username used in the search.
    • searchAttributes: Optional array of attributes to fetch from LDAP server, e.g. ['displayName', 'mail']. Defaults to undefined, i.e. fetch all attributes
    • tlsOptions: Optional object with options accepted by Node.js tls module.
  • usernameField: Field name where the username is found, defaults to username

  • passwordField: Field name where the password is found, defaults to password

  • passReqToCallback: When true, req is the first argument to the verify callback (default: false):

      passport.use(new ActiveDirectoryStrategy(..., function(req, user, done) {
          ...
          done(null, user);
        }
      ));
    

Note: you can pass a function instead of an object as options, see the example below

Authenticate requests

Use passport.authenticate(), specifying the 'adauth' strategy, to authenticate requests.

authenticate() options

In addition to default authentication options the following options are available for passport.authenticate():

  • badRequestMessage flash message for missing username/password (default: 'Missing credentials')
  • invalidCredentials flash message for InvalidCredentialsError, NoSuchObjectError, and /no such user/i LDAP errors (default: 'Invalid username/password')
  • userNotFound flash message when LDAP returns no error but also no user (default: 'Invalid username/password')
  • constraintViolation flash message when user account is locked (default: 'Exceeded password retry limit, account locked')

Express example

var express      = require('express'),
    passport     = require('passport'),
    bodyParser   = require('body-parser'),
    ActiveDirectoryStrategy = require('passport-adauth');

var OPTS = {
  server: {
    host: 'ad.company.com',
    principal: 'company.com'
  }
};

var app = express();

passport.use(new ActiveDirectoryStrategy(OPTS));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(passport.initialize());

app.post('/login', passport.authenticate('adauth', {session: false}), function(req, res) {
  res.send({status: 'ok'});
});

app.listen(8080);

Active Directory over SSL example

Simple example config for connecting over ldaps:// to a server requiring some internal CA certificate (often the case in corporations using Windows AD).

var fs = require('fs');

var opts = {
  server: {
    url: 'ldaps://ad.corporate.com:636',
    bindDn: 'cn=non-person,ou=system,dc=corp,dc=corporate,dc=com',
    bindCredentials: 'secret',
    searchBase: 'dc=corp,dc=corporate,dc=com',
    searchFilter: '(&(objectcategory=person)(objectclass=user)(|(samaccountname={{username}})(mail={{username}})))',
    searchAttributes: ['displayName', 'mail'],
    tlsOptions: {
      ca: [
        fs.readFileSync('/path/to/root_ca_cert.crt')
      ]
    }
  }
};
...

Asynchronous configuration retrieval

Instead of providing a static configuration object, you can pass a function as options that will take care of fetching the configuration. It will be called with the req object and a callback function having the standard (err, result) signature. Notice that the provided function will be called on every authenticate request.

var getLDAPConfiguration = function(req, callback) {
  // Fetching things from database or whatever
  process.nextTick(function() {
    var opts = {
      server: {
        url: 'ldap://localhost:389',
        bindDn: 'cn=root',
        bindCredentials: 'secret',
        searchBase: 'ou=passport-adauth',
        searchFilter: '(uid={{username}})'
      }
    };

    callback(null, opts);
  });
};

var LdapStrategy = require('passport-adauth');

passport.use(new LdapStrategy(getLDAPConfiguration,
  function(user, done) {
    ...
    return done(null, user);
  }
));

License

MIT

passport-adauth's People

Contributors

vesse avatar fusionrsrch avatar michaelbailly avatar simong avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.