add base of ldap authentication

This commit is contained in:
Mateo Körber Belmonte
2025-03-02 20:31:13 +01:00
parent 5608f1914e
commit 9d5ba9d7bb
4 changed files with 119 additions and 0 deletions
+1
View File
@@ -1,3 +1,4 @@
*.ttf *.ttf
/backend/node_modules /backend/node_modules
/frontend/public/data /frontend/public/data
.env
+2
View File
@@ -1,7 +1,9 @@
{ {
"dependencies": { "dependencies": {
"better-sqlite3": "^11.8.1", "better-sqlite3": "^11.8.1",
"dotenv": "^16.4.7",
"express": "^4.21.2", "express": "^4.21.2",
"ldap-authentication": "^3.3.4",
"ws": "^8.18.0" "ws": "^8.18.0"
} }
} }
+34
View File
@@ -5,6 +5,8 @@ const bodyParser = require('body-parser');
const path = require('path'); const path = require('path');
const Database = require('better-sqlite3'); const Database = require('better-sqlite3');
const fs = require('fs'); const fs = require('fs');
const ldap = require('ldap-authentication'); // Importiere die Bibliothek
require('dotenv').config(); // Lade die Umgebungsvariablen aus der .env-Datei
const app = express(); const app = express();
const server = http.createServer(app); const server = http.createServer(app);
@@ -60,6 +62,34 @@ db.exec(`
) )
`); `);
// LDAP-Authentifizierung Route
app.post('/login', async (req, res) => {
const { username, password } = req.body;
try {
// Für Active Directory verwenden wir eine andere Methode
const user = await ldap.authenticate({
ldapOpts: {
url: process.env.LDAP_URL,
reconnect: true
},
userDn: process.env.LDAP_USER_DN, // Serviceaccount aus Umgebungsvariable
userPassword: process.env.LDAP_USER_PASSWORD, // Serviceaccount Passwort aus Umgebungsvariable
userSearchBase: process.env.LDAP_SEARCH_BASE, // Basis-Suchpfad aus Umgebungsvariable
usernameAttribute: 'sAMAccountName', // AD verwendet sAMAccountName
username: username, // Eingegebener Nutzername
validatePassword: true, // Validiere Passwort des Users
passwordAttribute: 'userPassword' // Passwort-Attribut
});
console.log('Successfully authenticated:', user.cn);
res.json({ success: true, user: { name: user.cn } });
} catch (error) {
console.error('LDAP authentication error:', error);
res.status(401).json({ success: false, message: 'Authentication failed' });
}
});
// Routes // Routes
app.get('/edit/menu', (req, res) => { app.get('/edit/menu', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend/public/edit', 'menu.html')); res.sendFile(path.join(__dirname, '../frontend/public/edit', 'menu.html'));
@@ -77,6 +107,10 @@ app.get('/digitalSignage/weeklyFeed', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend/public/digitalSignage', 'weeklyFeed.html')); res.sendFile(path.join(__dirname, '../frontend/public/digitalSignage', 'weeklyFeed.html'));
}); });
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend/public', 'login.html'));
});
app.post('/save-week', (req, res) => { app.post('/save-week', (req, res) => {
const { week, entries } = req.body; const { week, entries } = req.body;
+82
View File
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Fira Sans', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.login-container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.login-container h1 {
margin-bottom: 20px;
}
.login-container input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
.login-container button {
width: 100%;
padding: 10px;
background-color: #a81411;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.login-container button:hover {
background-color: #8e1210;
}
</style>
</head>
<body>
<div class="login-container">
<h1>Login</h1>
<input type="text" id="username" placeholder="Benutzername">
<input type="password" id="password" placeholder="Passwort">
<button onclick="login()">Login</button>
<p id="error-message" style="color: red; display: none;">Login fehlgeschlagen. Bitte versuchen Sie es erneut.</p>
</div>
<script>
function login() {
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
fetch('/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, password })
})
.then(response => response.json())
.then(data => {
if (data.success) {
window.location.href = '/edit/menu';
} else {
document.getElementById('error-message').style.display = 'block';
}
})
.catch(error => {
console.error('Error:', error);
document.getElementById('error-message').style.display = 'block';
});
}
</script>
</body>
</html>