const http = require('http');
const url = require('url');
const ldap = require('ldapjs');
const bodyParser = require('body-parser');
const mysql = require('mysql2');
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const options = {
hostname: 'localhost',
port: 80,
path: req.url,
method: req.method,
headers: req.headers
};
const ldapUrl = 'ldap://208.80.162.112:389';
const bindDn = 'saltov.jorani@aspose.com';
const bindCredentials = 'pFK1V4$EB8r9';
const searchBase = 'ou=aspose,dc=aspose,dc=local';
if (req.method === 'POST' && parsedUrl.pathname === '/session/login') {
let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
});
// Get user login from request
login = ''
let userExistsAD = false;
let userExistsDB = false;
req.on('end', () => {
const postParams = new URLSearchParams(body);
login = postParams.get('login');
console.log(`Login: ${login}`);
});
// Create LDAP client
const ldapClient = ldap.createClient({ url: ldapUrl });
// Bind to LDAP server
ldapClient.bind(bindDn, bindCredentials, (bindErr) => {
if (bindErr) {
console.error('LDAP bind error:', bindErr);
}
// Search for user in LDAP
const searchOptions = {
scope: 'sub',
filter: `(sAMAccountName=${login})`,
attributes: ['dn', 'sn', 'givenName', 'mail'],
};
ldapClient.search(searchBase, searchOptions, (searchErr, searchRes) => {
if (searchErr) {
console.error('LDAP search error:', searchErr);
}
// Check if user exists
searchRes.on('searchEntry', (entry) => {
userExistsAD = true;
const { dn, lastname, firstname, email } = entry.object;
console.log(`User ${login} found in AD with DN ${dn}, lastname: ${lastname}, firstname: ${firstname}, email: ${email}`);
});
searchRes.on('end', () => {
ldapClient.unbind();
if (!userExistsAD) {
console.log(`${login} does not exist in LDAP`);
}
});
});
});
// Check if user exists in Jorani
const connection = mysql.createConnection({
host: 'localhost',
user: 'jorani',
password: 'jorani',
database: 'jorani'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected to MySQL database');
connection.query('SELECT COUNT(*) as count FROM users WHERE login = ?', login, (err, results) => {
if (err) {
console.error('MySQL query error:', err);
}
const count = results[0].count;
if (count > 0) {
userExistsDB = true;
console.log(`User with login ${login} exists in database`);
}
});
// Point
// If user in AD, but not in Jorani, create the user
if (userExistsAD && !userExistsDB) {
today = new Date();
const user = {
firstname: firstname,
lastname: lastname,
login: login,
email: email,
password: 'password',
role: 2,
manager: -1,
datehired: today.toISOString().slice(0, 10),
identifier: '',
language: 'en-GB',
timezone: 'Europe/Kyiv'
};
const query = 'INSERT INTO users SET ?';
connection.query(query, user, (err, result) => {
if (err) {
console.error('MySQL insert error:', err);
}
console.log(`User with login ${login} inserted successfully into database`);
console.log(result);
});
}
// If user not in AD, but in Jorani, remove the user
if (!userExistsAD && userExistsDB) {
connection.query('DELETE FROM users where login = ?', login, (err, result) => {
if (err) {
console.error('MySQL delete error:', err);
}
console.log(`User with login ${login} removed successfully from database`);
console.log(result);
});
}
});
}
// Forward the response from the destination server to the original requester
const proxyReq = http.request(options, proxyRes => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res, { end: true });
});
req.pipe(proxyReq, { end: true });
});
const port = 3000;
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});