0
votes
  1. I am trying to authenticate username and password using passport-ldapauth npm. while executing the below code I am always getting error as Bad Request. Kindly help me what is wrong with my code.

    var express      = require('express'),
        passport     = require('passport'),
        bodyParser   = require('body-parser'),
        LdapStrategy = require('passport-ldapauth');
    
    // Credentials from the free LDAP test server by LDAP Search
    
    var OPTS = {
      server: {
    
        url: 'ldap://54.227.207.201:389',
        bindDn: 'CN=simple One,CN=Users,DC=test,DC=local',
        bindCredentials: 'password',
        searchBase: 'ou=passport-ldapauth',
        searchFilter: '(uid={{username}})'
      },
      usernameField: "CN=simple One,CN=Users,DC=test,DC=local",
      passwordField: "password"
    };
    
    var app = express();
    
    passport.use(new LdapStrategy(OPTS));
    
    app.use(bodyParser.json());
    
    app.use(bodyParser.urlencoded({extended: false}));
    
    app.use(passport.initialize());
    
    
    app.use(passport.session());    
    
    app.post('/login', passport.authenticate('ldapauth', {session: false}), function(req, res)  {  
    
    // you can refer code from here:https://github.com/vesse/passport-ldapauth
    
      res.send({status: 'ok'});//will generate error 'Bad Request'
    
    });
       /*For more info:https://github.com/vesse/passport-ldapauth/issues/45*/
    
1

1 Answers

1
votes

passport-ldapauth returns bad request when either username or password value is missing. In your case this is probably because you have defined usernameField: "CN=simple One,CN=Users,DC=test,DC=local", ie. your login form should have the username to be used when logging in field with that name as mentioned in readme.

If your login form (or XHR post) sends the username in a field with name weirdUsername, eg. <input type="text" name="weirdUsername"/>, you would define usernameField : weirdUsername. The defaults, as again mentioned in the readme, are username for the username and password for the password.