I'm very new to ldapjs, and am trying to write an authenticate(username, password) script.
The steps I'm eventually trying to write are:
- bind to the admin, and I want to check for errors to see if the bind succeeded.
- search for the user in question (to see if they exist)
- bind to that user with their password to see if the password is correct
I'm currently stuck at step 1.
I am intentionally using a bad password when binding to the the admin to see if I can display an error message. My question is, why is my code not outputting an error message when it binds?
ldap3.js
const ldap = require('ldapjs');
const { promisify } = require('util');
// Configure the LDAP client
const ldapClient = ldap.createClient({
url: 'ldap://my.domain.com:7001',
tlsOptions: {
rejectUnauthorized: false,
},
timeout: 10000, //ms
connectTimeout: 10000,
idleTimeout: 10000
});
ldapClient.on('error', (err) => {
console.error('LDAP error:', err);
});
function bindToAdmin() {
console.log("inside BindToAdmin()");
ldapClient.bind('cn=admin', 'password!qqq', err => {
if(err){
ldapClient.unbind();
console.log("Error in binding (codeID: 5456464727):" + err);
throw (err);
}
});
console.log("after binding (codeID: 878789684654654)");
}
export default async function authenticateUser(username, password) {
var assert = require('assert');
console.log('START BINDING***********************************************************************');
try {
console.log("Binding to admin");
bindToAdmin();
} catch (err) {
console.error(err.message);
}
console.log('FINISHED BINDING*******************************************************************');
//todo: search for the username
//todo bind to the username using the password. (if it binds, then we know the password is correct.)
console.log('START UNBINDING************************************************************************');
ldapClient.unbind();
console.log('FINISHED UNBINDING*********************************************************************');
}
to call the code:
import ldapAuthenticate from '../../../library/ldap/ldap3';
...
try {
ldapAuthenticate(userIDInternal, password);
} catch (error) {
console.log('Failed to authenticate user:', error.message);
}
Output looks like this:
START BINDING***********************************************************************
Binding to admin
inside BindToAdmin()
after binding (codeID: 878789684654654)
FINISHED BINDING*******************************************************************
Edit 2023-03-09 (Additional Testing Info. Hopefully, this gives a clue.)
I waited 20 minutes, and still no additional output. Shouldn't it timeout after a few seconds?
Also, this is ldapjs 3.0.0, which I just realized was released just about 2 weeks ago (2/2023).
Also, I wanted to note, that I added code to Search for a user after the code that binds the admin. I'd say, 99% of the time, it finds nothing. However, ONCE, it actually found the user, and returned the values (phone, email...).
Also, I was able to connect using jXplorer, connecting from the same server. After connecting, I am able to navigate, and find all of the users, as well as edit the phone numbers and emails.
These are the settings I used with jXplorer:
Host: Same host as my code.
Port: 7001
Protocol: LDAP V3
BaseDN: BLANK
SecurityLevel: User+password
UserDN: cn=admin
password: same as my code