Loheide-eu-mitarbeiterplatt.../app/Providers/FortifyServiceProvider.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

92 lines
3.9 KiB
PHP
Raw Permalink 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 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 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' => true,
'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 {
$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));
}
}