All files index.js

96.55% Statements 28/29
75% Branches 12/16
100% Functions 3/3
96.55% Lines 28/29
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x 1x 1x   1x 1x                                 1x         4x 4x 3x 3x   3x 1x       2x 2x 2x 2x 2x     2x   2x 2x   2x         2x 2x 2x   2x           1x          
import Debug from 'debug';
import merge from 'lodash.merge';
import omit from 'lodash.omit';
import DefaultVerifier from './verifier';
import { Strategy as LdapStrategy } from 'passport-ldapauth';
 
const debug = Debug('feathers-authentication-ldap');
const defaults = {
  name: 'ldap',
  server: {
    url: 'ldap://localhost:389',
    bindDn: 'cn=anonymous',
    bindCredentials: '',
    searchBase: 'dc=de',
    searchFilter: '(uid={{username}})',
    searchAttributes: null
  },
  passReqToCallback: true
};
 
class JWTVerifier {
  // JWT Verifier replacement
  // to disable population from user service
  verify (req, payload, done) {
    done(null, { payload });
  }
}
 
// Export ldap-auth init function
export default function init (options = {}) {
  return function ldapAuth () {
    const app = this;
    const _super = app.setup;
 
    if (!app.passport) {
      throw new Error(`Can not find app.passport. Did you initialize feathers-authentication before feathers-authentication-ldap?`);
    }
 
    // Construct ldapSettings for passport ldap strategy
    let name = options.name || defaults.name;
    let authOptions = app.get('auth') || {};
    let ldapOptions = authOptions[name] || {};
    const ldapSettings = merge({}, defaults, ldapOptions, omit(options, ['Verifier']));
    const Verifier = options.Verifier || DefaultVerifier;
 
    // plugin setup: register strategy in feathers passport
    app.setup = function () {
      // be sure feathers setup was called
      let result = _super.apply(this, arguments);
      let verifier = new Verifier(app, ldapSettings);
 
      Iif (!verifier.verify) {
        throw new Error(`Your verifier must implement a 'verify' function. It should have the same signature as function(request, user, done)`);
      }
 
      // Register 'ldap' strategy with passport
      debug('Registering ldap authentication strategy with options:', ldapSettings);
      app.passport.use(ldapSettings.name, new LdapStrategy(ldapSettings, verifier.verify.bind(verifier)));
      app.passport.options(ldapSettings.name, ldapSettings); // do we need this ??
 
      return result;
    };
  };
}
 
// Exposed Modules
Object.assign(init, {
  defaults,
  Verifier: DefaultVerifier,
  JWTVerifier
});