219 lines
9.5 KiB
PHP
219 lines
9.5 KiB
PHP
<?php
|
|
/**
|
|
* Fociz Veilige Login met Tweestapsverificatie (2FA)
|
|
*/
|
|
|
|
require_once __DIR__ . '/../includes/auth.php';
|
|
start_secure_session();
|
|
|
|
// Als er nog geen admin is, direct naar setup
|
|
if (!has_admin_user()) {
|
|
header('Location: /auth/setup.php');
|
|
exit;
|
|
}
|
|
|
|
// Als al ingelogd (via actieve sessie of persistente cookie), ga direct naar planner
|
|
$currentUser = get_logged_in_user();
|
|
if ($currentUser) {
|
|
header('Location: /planner.php');
|
|
exit;
|
|
}
|
|
|
|
$error = null;
|
|
$step = isset($_SESSION['pending_2fa_user_id']) ? 2 : 1;
|
|
|
|
// Terug naar stap 1
|
|
if (isset($_GET['action']) && $_GET['action'] === 'restart') {
|
|
unset($_SESSION['pending_2fa_user_id'], $_SESSION['pending_2fa_user_email']);
|
|
header('Location: /auth/login.php');
|
|
exit;
|
|
}
|
|
|
|
// POST Verwerking
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$db = get_db_connection();
|
|
|
|
// STAP 1: Wachtwoord verificatie
|
|
if ($step === 1) {
|
|
$email = strtolower(trim($_POST['email'] ?? ''));
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($email) || empty($password)) {
|
|
$error = "Vul e-mailadres en wachtwoord in.";
|
|
} else {
|
|
$stmt = $db->prepare("SELECT id, name, email, password_hash, two_factor_secret, two_factor_enabled FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password_hash'])) {
|
|
// Succesvol stap 1: zet tijdelijke pending sessie voor 2FA
|
|
$_SESSION['pending_2fa_user_id'] = $user['id'];
|
|
$_SESSION['pending_2fa_user_email'] = $user['email'];
|
|
$_SESSION['pending_2fa_user_name'] = $user['name'];
|
|
$step = 2;
|
|
} else {
|
|
$error = "Onjuist e-mailadres of wachtwoord.";
|
|
}
|
|
}
|
|
}
|
|
// STAP 2: 2FA TOTP Code verificatie
|
|
elseif ($step === 2) {
|
|
$userId = $_SESSION['pending_2fa_user_id'] ?? null;
|
|
$totpCode = trim(str_replace(' ', '', $_POST['totp_code'] ?? ''));
|
|
|
|
if (!$userId || empty($totpCode)) {
|
|
$error = "Vul de 6-cijferige 2FA verificatiecode in.";
|
|
} else {
|
|
$stmt = $db->prepare("SELECT id, name, email, role, two_factor_secret, backup_codes FROM users WHERE id = ?");
|
|
$stmt->execute([$userId]);
|
|
$user = $stmt->fetch();
|
|
|
|
$isValid = false;
|
|
|
|
// 1. Controleer TOTP code
|
|
if (!empty($user['two_factor_secret']) && TOTP::verifyCode($user['two_factor_secret'], $totpCode)) {
|
|
$isValid = true;
|
|
}
|
|
|
|
// 2. Controleer optioneel backup codes
|
|
if (!$isValid && !empty($user['backup_codes'])) {
|
|
$backupCodes = json_decode($user['backup_codes'], true) ?: [];
|
|
$codeUpper = strtoupper($totpCode);
|
|
$key = array_search($codeUpper, $backupCodes);
|
|
if ($key !== false) {
|
|
$isValid = true;
|
|
// Verbruik de backup code
|
|
unset($backupCodes[$key]);
|
|
$stmtUpdate = $db->prepare("UPDATE users SET backup_codes = ? WHERE id = ?");
|
|
$stmtUpdate->execute([json_encode(array_values($backupCodes)), $userId]);
|
|
}
|
|
}
|
|
|
|
if ($isValid) {
|
|
// Volledige login succesvol
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['user_name'] = $user['name'];
|
|
$_SESSION['user_email'] = $user['email'];
|
|
$_SESSION['user_role'] = $user['role'];
|
|
$_SESSION['is_2fa_verified'] = true;
|
|
|
|
// Activeer persistente remember token (30 dagen blijf ingelogd)
|
|
issue_remember_token((int)$user['id']);
|
|
|
|
unset($_SESSION['pending_2fa_user_id'], $_SESSION['pending_2fa_user_email'], $_SESSION['pending_2fa_user_name']);
|
|
|
|
header('Location: /planner.php');
|
|
exit;
|
|
} else {
|
|
$error = "Onjuiste 2FA code. Controleer uw authenticator app en probeer opnieuw.";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$pageTitle = "Fociz — Inloggen";
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="nl" class="dark">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?= htmlspecialchars($pageTitle) ?></title>
|
|
<script src="https://cdn.tailwindcss.com"></script>
|
|
<script src="https://unpkg.com/lucide@latest"></script>
|
|
<style>body { background-color: #090d16; color: #f1f5f9; }</style>
|
|
</head>
|
|
<body class="min-h-screen flex items-center justify-center p-4 bg-gradient-to-br from-[#090d16] via-[#0f172a] to-[#090d16]">
|
|
|
|
<?php require_once __DIR__ . '/../includes/env_badge.php'; ?>
|
|
|
|
<div class="w-full max-w-md bg-slate-900/90 border border-slate-800 rounded-2xl shadow-2xl backdrop-blur-xl p-8">
|
|
<!-- Header -->
|
|
<div class="text-center mb-8">
|
|
<div class="inline-flex items-center justify-center w-14 h-14 rounded-2xl bg-emerald-500/10 border border-emerald-500/30 text-emerald-400 mb-3 shadow-lg shadow-emerald-500/10">
|
|
<i data-lucide="shield-check" class="w-8 h-8"></i>
|
|
</div>
|
|
<h1 class="text-3xl font-extrabold tracking-tight text-white">Fociz</h1>
|
|
<p class="text-slate-400 text-sm mt-1">
|
|
Veilig inloggen met tweestapsverificatie (2FA)
|
|
</p>
|
|
</div>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="mb-6 p-4 rounded-xl bg-red-500/10 border border-red-500/30 flex items-start space-x-3 text-red-400 text-sm">
|
|
<i data-lucide="alert-triangle" class="w-5 h-5 flex-shrink-0 mt-0.5"></i>
|
|
<span><?= htmlspecialchars($error) ?></span>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($step === 1): ?>
|
|
<!-- STAP 1: E-mail & Wachtwoord -->
|
|
<form method="POST" action="/auth/login.php" class="space-y-4">
|
|
<div>
|
|
<label class="block text-xs font-medium text-slate-300 mb-1">E-mailadres</label>
|
|
<div class="relative">
|
|
<input type="email" name="email" required autofocus placeholder="uw.naam@bedrijf.nl" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>" class="w-full bg-slate-800/80 border border-slate-700 rounded-xl px-4 py-2.5 text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-emerald-500/50 text-sm transition">
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="block text-xs font-medium text-slate-300 mb-1">Wachtwoord</label>
|
|
<div class="relative">
|
|
<input type="password" name="password" required placeholder="••••••••" class="w-full bg-slate-800/80 border border-slate-700 rounded-xl px-4 py-2.5 text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-emerald-500/50 text-sm transition">
|
|
</div>
|
|
</div>
|
|
|
|
<button type="submit" class="w-full mt-4 bg-emerald-500 hover:bg-emerald-600 text-slate-950 font-bold py-3 px-4 rounded-xl transition shadow-lg shadow-emerald-500/20 flex items-center justify-center space-x-2">
|
|
<span>Verder naar 2FA</span>
|
|
<i data-lucide="arrow-right" class="w-4 h-4"></i>
|
|
</button>
|
|
</form>
|
|
|
|
<?php else: ?>
|
|
|
|
<!-- STAP 2: 2FA TOTP Code -->
|
|
<form method="POST" action="/auth/login.php" class="space-y-5">
|
|
<div class="text-center">
|
|
<div class="inline-flex items-center justify-center w-12 h-12 rounded-xl bg-emerald-500/10 text-emerald-400 mb-2">
|
|
<i data-lucide="key" class="w-6 h-6"></i>
|
|
</div>
|
|
<p className="text-sm text-slate-300 font-medium">
|
|
Voer uw 6-cijferige verificatiecode in
|
|
</p>
|
|
<p class="text-xs text-slate-400 mt-1">
|
|
Gekoppeld aan <strong class="text-slate-200"><?= htmlspecialchars($_SESSION['pending_2fa_user_email'] ?? '') ?></strong>
|
|
</p>
|
|
</div>
|
|
|
|
<div>
|
|
<input type="text" name="totp_code" required autofocus maxlength="9" placeholder="000 000" class="w-full text-center tracking-[0.4em] font-mono text-2xl bg-slate-800/80 border border-slate-700 rounded-xl py-3 text-white placeholder-slate-600 focus:outline-none focus:ring-2 focus:ring-emerald-500/50 transition">
|
|
<span class="text-[11px] text-slate-500 text-center block mt-1">
|
|
Tip: U kunt ook een 8-cijferige herstelcode invoeren.
|
|
</span>
|
|
</div>
|
|
|
|
<div class="flex space-x-3">
|
|
<a href="/auth/login.php?action=restart" class="w-1/3 text-center bg-slate-800 hover:bg-slate-700 text-slate-300 font-medium py-2.5 px-3 rounded-xl transition text-sm flex items-center justify-center">
|
|
Terug
|
|
</a>
|
|
<button type="submit" class="w-2/3 bg-emerald-500 hover:bg-emerald-600 text-slate-950 font-bold py-2.5 px-4 rounded-xl transition shadow-lg shadow-emerald-500/20 flex items-center justify-center space-x-2">
|
|
<span>Inloggen</span>
|
|
<i data-lucide="check-circle-2" class="w-4 h-4"></i>
|
|
</button>
|
|
</div>
|
|
</form>
|
|
|
|
<?php endif; ?>
|
|
|
|
<div class="mt-8 pt-6 border-t border-slate-800/80 text-center text-xs text-slate-500">
|
|
Toegang tot Fociz is strikt op uitnodiging. Vraag uw beheerder om een uitnodigingslink.
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
if (typeof lucide !== 'undefined') lucide.createIcons();
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|