vesse / passport-ldapauth

LDAP authentication strategy for Passport
MIT License
312 stars 100 forks source link

How to use common auth and basic auth in one application? #85

Closed victornikitin closed 5 years ago

victornikitin commented 5 years ago

Is it possible to use common auth (with usernameField and passwordField options) and basic auth (with credentialsLookup options) in the same application?

As far as I understood, i should call passport.use(...) twice with different options. If I call passport.authenticate("ldapauth") in 2 different middlewares how passport can understand which authenticate method should be called in each one?

ddolcimascolo commented 5 years ago

Hi @victornikitin,

You're almost there :) You indeed should register two different passport strategies using passport.use, but the key is to use two different strategy names. For instance:

function myUserCallback(user, done) {
  // Do something cool with user, then call done()
}

passport.use('ldapauth', new LdapStrategy(options, myUserCallback));
passport.use('basic-ldapauth', new LdapStrategy(options, myUserCallback));

Then later in an express middleware (assuming you're using https://github.com/expressjs/express):

passport.authenticate('ldapauth', ...); // To use usernameField + passwordField authentication
passport.authenticate('basic-ldapauth', ...); // To use basic authentication
passport.authenticate(['ldapauth', 'basic-ldapauth'], ...); // To support both (passport will try all given strategies serially until one succeeds, or fail the authentication)

Cheers, David

vesse commented 5 years ago

Closing as @ddolcimascolo already answered, and this is really once again not an issue in this library but generic passport usage question.