Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs passport ldapauth "Cannot read 'on' property of undefined"

I'm trying to authenticate to an LDAP server using passport-ldapauth and express.

The authentication using an ldap url (ldap://myserver...) works OK, but with ldaps I get:

TypeError: Cannot read property 'on' of undefined
    at setupSocket (...\ldapauth-fork\node_modules\ldapjs\lib\client\client.js:111:14)
    at Client._connect (...\ldapauth-fork\node_modules\ldapjs\lib\client\client.js:742:3)
    at new Client (...\ldapauth-fork\node_modules\ldapjs\lib\client\client.js:247:22)
    at Object.createClient (...\ldapauth-fork\node_modules\ldapjs\lib\client\index.js:60:12)
    at new LdapAuth (...\ldapauth-fork\lib\ldapauth.js:129:28)
    at handleAuthentication (...\passport-ldapauth\lib\passport-ldapauth\strategy.js:140:10)
    at Strategy.authenticate (...\passport-ldapauth\lib\passport-ldapauth\strategy.js:175:33)
    at attempt (...\passport\lib\middleware\authenticate.js:341:16)
    at authenticate (...\passport\lib\middleware\authenticate.js:342:7)
    at Layer.handle [as handle_request] (...\express\lib\router\layer.js:82:5)

My code is, basically, this:

  ...
  passport.use(new LdapStrategy({
  server: {
    url: 'ldaps://myserver:636',
    searchBase: '...',
    searchFilter: '(uid={{username}})',
    tlsOptions: {
        ca: [
            fs.readFileSync('myCAcert.pem')
    ]
   }
    },
    session: false,
    usernameField:'u',
    passwordField:'p'
  }, 
 function(user,  done) {
   console.log("Interna: \nOK");
    console.log("u:");
    console.log(user.cn);
   return done(null, user);
 }));

 app.use('/login',passport.authenticate('ldapauth', 
                 { session:false,                                        
                   successRedirect:'/accessed',
                   failureRedirect: '/accessfail' 
                  }
                 )); 

app.use('/accessed',function (req,res,next){
     res.send("User OK");
});

app.use('/accessfail',function (req,res,next){
       res.send("User MAL !!!!!!!");
 });

app.listen(3336);

My library versions are:

 [email protected], [email protected],[email protected]

Someone can help me?

Thanks.

like image 727
Fernando Carpani Avatar asked Feb 27 '15 20:02

Fernando Carpani


2 Answers

Finally, I could solve the issue.

The ldapjs version used in ldapauth-fork, can't work with an ldaps url.

The solution was:

  1. Delete the ldapjs from ldapauth-fork using in the node_modules directory under ldapauth-fork (under other node_modules directory under passport-ldap in the node_modules for my user !!!!!!) the following command:

    npm rm ldapjs

  2. Install the new version from github using:

    npm install git://github.com/mcavage/node-ldapjs.git

FDO.

like image 105
Fernando Carpani Avatar answered Jan 27 '23 19:01

Fernando Carpani


This happens only with Node 0.12 and io.js when using SSL, see the ldapjs issue. You can add dependency to the unreleased version in your projects package.json:

{
  "dependencies": {
    "ldapjs": "mcavage/node-ldapjs",
    "ldapauth-fork": "2.3.1"
  }
}
like image 35
vesse Avatar answered Jan 27 '23 20:01

vesse