bhoriuchi / passport-activedirectory

Active Directory strategy for passport.js
29 stars 16 forks source link

Authenticate with username and password from the request body #24

Open Vrielmann-EE opened 1 year ago

Vrielmann-EE commented 1 year ago

I am trying to authenticate to the ActiveDirectory with Username and Password from the body om my request:

curl --location 'https://company.com:8443/login' \
--header 'Content-Type: application/json' \
--data-raw '{
    "username": "name@company.com",
    "password": "name@company.com"
}

If I provide username and password in the ldap from the ActiveDirectoryStrategy, a connection is established. But if I try to connect through the username and password from the request body, I am getting the following error:

{
    "lde_message":"000004DC: LdapErr: DSID-0C090AC9, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v4f7c",
    "lde_dn": null
}

Here is my ActiveDirectoryStrategy

passport.use(new ActiveDirectoryStrategy({
  integrated: false,
  passReqToCallback: true,
  ldap: {
    url: 'ldap://dc1.ad.company.com',
    baseDN: 'OU=Benutzer,OU=Company,DC=ad,DC=company,DC=com',
    // username: 'name@company.com',
    // password: 'name@company.com',
  },
  usernameField: 'username',
  passwordField: 'password',
}, ((profile, ad, done) => {
  // ad.authenticate(username, password, (err, auth) => {
  //   if (err) {
  //     console.log(`ERROR: ${JSON.stringify(err)}`);
  //     return;
  //   }
  //   if (auth) {
  //     console.log('Authenticated!');
  //   } else {
  //     console.log('Authentication failed!');
  //   }
  // });
  return done(null, profile);
})));

And here is my passport.authenticate:

router.post('/login', async (req, res, next) => {
  passport.authenticate('ActiveDirectory', null, async (err, user) => {
    if (err || !user) {
      return res.json(err);
    }
    return res.json({ m: 'super' });
  })(req, res, next);
});

Unfortunately I don't see the error and how do I get username and password after a established connection for my ad.authenticate?

Vrielmann-EE commented 1 year ago

I have created a user in ActiveDirectory for login and search profiles, I can use this user in the ldap options with username and password to establishing a connection.

The profile of my random user in the request with username as usernameField and password as passwordField will be transferred automatically, thats all - works well, thank you 👍