1
  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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I am trying to test passport-ldapauth in NodeJS using postman and I keep getting Bad request all the time. What am I doing wrong? I have followed the instructions mentioned in the Read me.
PS: I am a newbie with ldap authentication.
I'm using ldaps and my usernameField and passwordField are correctly filled in.
Make sure the request content type is "application/json".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.