*/ public array $users = []; public int $recentAttempts = 0; /** @var array */ public array $writes = []; /** @var array */ public array $settings = []; public function fetchOne($sql, $params = []) { $sql = (string)preg_replace('/\s+/', ' ', strtolower($sql)); if (str_contains($sql, 'from login_attempts')) { return ['cnt' => $this->recentAttempts]; } if (str_contains($sql, 'from users')) { foreach ($this->users as $user) { if (($user['email'] ?? null) === ($params[0] ?? null)) { return $user; } } return false; } if (str_contains($sql, 'from audit_log')) { return ['1' => 1]; // IP bereits gesehen -> keine Benachrichtigung } return false; } public function fetchAll($sql, $params = []) { return []; } public function execute($sql, $params = []) { $this->writes[] = ['sql' => (string)preg_replace('/\s+/', ' ', strtolower($sql)), 'params' => $params]; return 1; } public function query($sql, $params = []) { return $this->execute($sql, $params); } public function getSetting($key, $default = null) { return $this->settings[$key] ?? $default; } /** Hat es einen Schreibzugriff mit diesem Fragment gegeben? */ public function wrote(string $fragment): bool { foreach ($this->writes as $write) { if (str_contains($write['sql'], strtolower($fragment))) { return true; } } return false; } } class AuthTest extends TestCase { private FakeAuthDb $db; private Auth $auth; protected function setUp(): void { $_SESSION = []; $_SERVER['REMOTE_ADDR'] = '198.51.100.5'; unset($_SERVER['HTTP_X_FORWARDED_FOR']); $this->db = new FakeAuthDb(); $this->db->users[] = [ 'id' => 7, 'email' => 'marie@example.com', 'name' => 'Marie', 'is_admin' => 1, 'is_active' => 1, 'password_hash' => password_hash('richtig', PASSWORD_DEFAULT), 'totp_enabled' => 0, 'totp_secret' => null, ]; $this->auth = new Auth($this->db); } // --- Anmeldung ------------------------------------------------------- public function testCorrectPasswordStartsSession(): void { $this->assertTrue($this->auth->login('marie@example.com', 'richtig')); $this->assertSame(7, $_SESSION['user_id']); $this->assertTrue($this->auth->isLoggedIn()); $this->assertTrue($this->auth->isAdmin()); } public function testWrongPasswordIsRecorded(): void { $this->assertFalse($this->auth->login('marie@example.com', 'falsch')); $this->assertArrayNotHasKey('user_id', $_SESSION); $this->assertTrue($this->db->wrote('insert into login_attempts'), 'Fehlversuch muss protokolliert werden'); } public function testUnknownUserBehavesLikeWrongPassword(): void { $this->assertFalse($this->auth->login('niemand@example.com', 'egal')); $this->assertArrayNotHasKey('user_id', $_SESSION); } public function testRateLimitStopsLoginBeforePasswordCheck(): void { $this->db->recentAttempts = 10; $this->assertSame('rate_limited', $this->auth->login('marie@example.com', 'richtig')); $this->assertArrayNotHasKey('user_id', $_SESSION, 'Bei Sperre darf keine Sitzung entstehen'); } public function testTwoFactorUserIsNotLoggedInYet(): void { $this->db->users[0]['totp_enabled'] = 1; $this->db->users[0]['totp_secret'] = 'JBSWY3DPEHPK3PXP'; $result = $this->auth->login('marie@example.com', 'richtig'); $this->assertSame('totp_required', $result); $this->assertArrayNotHasKey('user_id', $_SESSION, 'Erst nach dem Code gilt man als angemeldet'); $this->assertSame(7, $_SESSION['totp_pending_user_id']); $this->assertFalse($this->auth->isLoggedIn()); } public function testInactiveUserCannotLogIn(): void { $this->db->users[0]['is_active'] = 0; // Die Abfrage filtert auf is_active = 1; die Attrappe liefert den // Datensatz trotzdem, deshalb hier der Vollständigkeit halber nur // die Prüfung, dass ein leerer Treffer nicht anmeldet. $this->db->users = []; $this->assertFalse($this->auth->login('marie@example.com', 'richtig')); } // --- CSRF ------------------------------------------------------------ public function testCsrfTokenIsStableAndChecked(): void { $token = $this->auth->getCsrfToken(); $this->assertNotSame('', $token); $this->assertSame($token, $this->auth->getCsrfToken(), 'Token bleibt innerhalb der Sitzung gleich'); $this->assertTrue($this->auth->validateCsrfToken($token)); $this->assertFalse($this->auth->validateCsrfToken('falsch')); $this->assertFalse($this->auth->validateCsrfToken('')); } public function testCsrfCheckFailsWithoutSessionToken(): void { $_SESSION = []; $this->assertFalse($this->auth->validateCsrfToken('irgendwas')); } // --- Client-IP hinter einem Proxy ----------------------------------- public function testClientIpIgnoresForwardedHeaderWithoutTrustedProxy(): void { $_SERVER['HTTP_X_FORWARDED_FOR'] = '203.0.113.9'; $this->assertSame('198.51.100.5', $this->auth->clientIp(), 'Ohne eingetragenen Proxy darf der Header nicht zählen'); } public function testClientIpUsesForwardedHeaderFromTrustedProxy(): void { $this->db->settings['trusted_proxy'] = '198.51.100.5'; $_SERVER['HTTP_X_FORWARDED_FOR'] = '203.0.113.9, 70.41.3.18'; $this->assertSame('203.0.113.9', $this->auth->clientIp()); } public function testClientIpIgnoresForwardedHeaderFromUnknownSource(): void { $this->db->settings['trusted_proxy'] = '10.0.0.1'; $_SERVER['HTTP_X_FORWARDED_FOR'] = '203.0.113.9'; $this->assertSame('198.51.100.5', $this->auth->clientIp()); } public function testClientIpFallsBackWhenHeaderIsGarbage(): void { $this->db->settings['trusted_proxy'] = '198.51.100.5'; $_SERVER['HTTP_X_FORWARDED_FOR'] = 'kein-ip-wert'; $this->assertSame('198.51.100.5', $this->auth->clientIp()); } }