Loheide-eu-mitarbeiterplatt.../app/Providers/FortifyServiceProvider.php
Friederich Loheide d91570f118
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 6m46s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 7m52s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 8m5s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 10m33s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m53s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m1s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 8m49s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 54s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m4s
Dateien nur mit Anmeldung, nur Microsoft-Login, mehrere Titelbilder (0.47.0)
- /uploads nur über das Portal: UploadController prüft Sitzung bzw. App-Token;
  frei nur die Dateien der Anmeldeseite (Logos, Symbole, Anmeldehintergrund,
  Stylesheet, Schrift); public/uploads/.htaccess leitet alles an index.php
  (Migration schreibt sie, da der Ordner vor dem Updater geschützt ist);
  Systemzustand prüft die Regel; nginx-Hinweis in docs/SECURITY.md
- Passwort-Anmeldung abschaltbar (PasswordLogin): Login-Formular, Passwort
  vergessen, App-Token und /api/v1/config; nur mit verknüpftem Konto
  abschaltbar, automatische Rückkehr ohne nutzbaren Dienst, Notausgang
  INHAUS_PASSWORD_LOGIN
- Titelbilder: profile_covers (bis 12), zufällig je Seitenaufruf
- ImageCompressor: Uploads im Backend (SafeUpload) und WordPress-Import auf
  ≤ 2400 px komprimiert, Format bleibt

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 17:26:15 +00:00

99 lines
4.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Providers;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Http\Responses\NeutralPasswordResetLinkResponse;
use App\Models\IdentityProvider;
use App\Models\User;
use App\Support\Audit;
use App\Support\PasswordLogin;
use Illuminate\Auth\Events\Failed;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
use Laravel\Fortify\Contracts\FailedPasswordResetLinkRequestResponse;
use Laravel\Fortify\Contracts\SuccessfulPasswordResetLinkRequestResponse;
use Laravel\Fortify\Fortify;
class FortifyServiceProvider extends ServiceProvider
{
public function register(): void
{
// „Passwort vergessen“ verrät nicht, ob es ein Konto gibt
$this->app->bind(FailedPasswordResetLinkRequestResponse::class, NeutralPasswordResetLinkResponse::class);
$this->app->bind(SuccessfulPasswordResetLinkRequestResponse::class, NeutralPasswordResetLinkResponse::class);
}
public function boot(): void
{
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::loginView(fn (Request $request) => Inertia::render('Auth/Login', [
'canResetPassword' => app(PasswordLogin::class)->enabled(),
'passwordLogin' => app(PasswordLogin::class)->enabled(),
'providers' => IdentityProvider::forLogin()->map->toLoginButton()->values(),
'identityError' => $request->session()->get('identity_error'),
'status' => $request->boolean('eingerichtet')
? 'Die Einrichtung ist abgeschlossen. Sie können sich jetzt mit Ihrem Administratorkonto anmelden.'
: $request->session()->get('status'),
]));
Fortify::requestPasswordResetLinkView(fn (Request $request) => Inertia::render('Auth/ForgotPassword', [
'status' => $request->session()->get('status'),
]));
Fortify::resetPasswordView(fn (Request $request) => Inertia::render('Auth/ResetPassword', [
'email' => $request->input('email'),
'token' => $request->route('token'),
]));
// Deaktivierte Konten und Konten ohne lokales Passwort (später: nur IdP) können
// sich nicht per Passwort anmelden.
Fortify::authenticateUsing(function (Request $request): ?User {
if (! app(PasswordLogin::class)->enabled()) {
throw ValidationException::withMessages([Fortify::username() => 'Die Anmeldung mit Passwort ist abgeschaltet – bitte über den Anmeldedienst oben anmelden.']);
}
$user = User::query()->where('email', Str::lower($request->input(Fortify::username())))->first();
// Immer ein Passwort prüfen – gleiche Antwortzeit, ob es das Konto gibt oder nicht
$valid = Hash::check((string) $request->input('password'), $user?->password ?? self::dummyHash());
return $user && $user->is_active && $user->password !== null && $valid ? $user : null;
});
// Anmeldungen protokollieren: fehlgeschlagene Passwort-Anmeldungen und Passwort-Resets
Event::listen(Failed::class, fn ($event) => Audit::log('auth.login_failed', ['email' => Str::lower((string) ($event->credentials['email'] ?? ''))], $event->user?->id));
Event::listen(PasswordReset::class, fn ($event) => Audit::log('auth.password_reset', ['user' => $event->user->id], $event->user->id));
Event::listen(Login::class, function (Login $event): void {
if ($event->user instanceof User) {
$event->user->forceFill(['last_login_at' => now()])->saveQuietly();
}
});
RateLimiter::for('login', function (Request $request) {
$throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip());
return Limit::perMinute(5)->by($throttleKey);
});
}
public static function dummyHash(): string
{
static $hash;
return $hash ??= Hash::make(Str::random(32));
}
}