Skip to content

Instantly share code, notes, and snippets.

@musasoftlabx
Created August 26, 2024 12:54
Show Gist options
  • Save musasoftlabx/b9b8e0faf1431ee3cf16e8dba9257e18 to your computer and use it in GitHub Desktop.
Save musasoftlabx/b9b8e0faf1431ee3cf16e8dba9257e18 to your computer and use it in GitHub Desktop.
import ldap from "ldapjs";
const query = 'name-to-search'; // ? query contains name to search
const client = ldap.createClient({ url: ["ldap://172.29.200.99:389"] });
client.bind(`Your-windows-username`, "Your-windows-password", (error: any) => {
if (error)
console.log(error)
else
new Promise((resolve, reject) => {
client.search(
"ou=safaricom departments,dc=safaricom,dc=net",
{
filter: `(cn=*${query}*)`,
scope: "sub",
attributes: [
"sAMAccountName",
"cn",
"mail",
"title",
"employeeNumber",
"manager",
"telephoneNumber",
"department",
],
},
(err: any, result: any) => {
let results: any = [];
result.on("searchEntry", (entry: any) => {
let user: any = {};
entry.pojo.attributes.map((attribute: any) => {
user[
attribute.type === "cn"
? "name"
: attribute.type === "sAMAccountName"
? "username"
: attribute.type === "title"
? "jobRole"
: attribute.type === "mail"
? "emailAddress"
: attribute.type === "telephoneNumber"
? "phoneNumber"
: attribute.type
] = attribute.values[0];
});
results.push(user);
});
setTimeout(() => resolve(results), 2000);
}
);
})
.then((value: any) => console.log(value))
.catch((err) =>
console.log({ subject: "LDAP Error!", body: err?.message })
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment