🌟 Synchronization of the user with LDAP (#185)
* 🌟Synchronization of the user with LDAP
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
LDAP_URL=ldap://localhost:389
|
||||
LDAP_BIND_DN=
|
||||
LDAP_BIND_CREDENTIALS=
|
||||
LDAP_SEARCH_BASE=dc=example,dc=com
|
||||
LDAP_SEARCH_FILTER=(objectClass=inetorgperson)
|
||||
API_URL=http://tdrive:4000/api/sync
|
||||
@@ -0,0 +1 @@
|
||||
18
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "ldap_project",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "npm run build:clean && npm run build:ts",
|
||||
"build:ts": "tsc",
|
||||
"build:clean": "rimraf ./dist",
|
||||
"sync": "node dist/index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^1.4.0",
|
||||
"dotenv": "^16.0.3",
|
||||
"ldapjs": "^3.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/ldapjs": "^2.2.5",
|
||||
"typescript": "^5.0.4",
|
||||
"rimraf": "^3.0.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import ldap from "ldapjs";
|
||||
import axios from "axios";
|
||||
import dotenv from "dotenv";
|
||||
|
||||
interface UserAttributes {
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
dotenv.config();
|
||||
|
||||
console.log("Run script with the following env:");
|
||||
console.log(process.env);
|
||||
|
||||
// LDAP server configuration
|
||||
const ldapConfig = {
|
||||
url: process.env.LDAP_URL|| "localhost",
|
||||
bindDN: process.env.LDAP_BIND_DN || "",
|
||||
bindCredentials: process.env.LDAP_BIND_CREDENTIALS || "",
|
||||
searchBase: process.env.LDAP_SEARCH_BASE || "dc=example,dc=com",
|
||||
searchFilter: process.env.LDAP_SEARCH_FILTER || "(objectClass=inetorgperson)",
|
||||
timeout: 120,
|
||||
version: 3,
|
||||
};
|
||||
|
||||
// Create LDAP client
|
||||
const client = ldap.createClient({
|
||||
url: ldapConfig.url,
|
||||
});
|
||||
|
||||
// Bind to LDAP server
|
||||
client.bind(ldapConfig.bindDN, ldapConfig.bindCredentials, (err) => {
|
||||
if (err) {
|
||||
console.error("LDAP bind error:", err);
|
||||
return;
|
||||
}
|
||||
|
||||
// Perform search
|
||||
client.search(
|
||||
ldapConfig.searchBase,
|
||||
{
|
||||
filter: ldapConfig.searchFilter,
|
||||
attributes: ["uid", "mail", "cn", "sn", "mobile"],
|
||||
scope: "sub",
|
||||
derefAliases: 2,
|
||||
},
|
||||
(searchErr, searchRes) => {
|
||||
if (searchErr) {
|
||||
console.error("LDAP search error:", searchErr);
|
||||
return;
|
||||
}
|
||||
|
||||
const apiRequests: Promise<any>[] = [];
|
||||
|
||||
searchRes.on("searchEntry", (entry: any) => {
|
||||
// Handle each search result entry
|
||||
const userAttributes: UserAttributes = {
|
||||
first_name: entry.attributes[1]?.values[0],
|
||||
last_name: entry.attributes[2]?.values[0],
|
||||
email: entry.attributes[3]?.values[0],
|
||||
};
|
||||
|
||||
// Make API call to tdrive backend with the userAttributes
|
||||
apiRequests.push(axios.post(process.env.API_URL || "", userAttributes));
|
||||
});
|
||||
|
||||
searchRes.on("error", (err) => {
|
||||
console.error("LDAP search result error:", err);
|
||||
});
|
||||
|
||||
searchRes.on("end", () => {
|
||||
// Unbind from LDAP server after search is complete
|
||||
client.unbind((unbindErr) => {
|
||||
if (unbindErr) {
|
||||
console.error("LDAP unbind error:", unbindErr);
|
||||
} else {
|
||||
Promise.all(apiRequests)
|
||||
.then((responses) => {
|
||||
console.log(
|
||||
"API responses:",
|
||||
responses.map((r) => r.data)
|
||||
);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("API error:", error);
|
||||
})
|
||||
.finally(() => {
|
||||
console.log("LDAP search completed successfully.");
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"module": "commonjs",
|
||||
"outDir": "dist",
|
||||
"strict": true,
|
||||
"esModuleInterop": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user