Files
Toiminnanohjaus/app/Modules/Users/Presentation/Livewire/⚡login.blade.php
T
petriandClaude Opus 5.5 1acdaa5b19 Harden the app: block project files, add rate limits and security headers
- Root .htaccess refuses everything outside public/, because local Apache
  serves the whole project folder (.env, logs with set-password links, .git).
- Login allows 20 failed attempts a minute per IP across accounts, besides
  5 per email and IP; the forgot-password page 5 requests a minute per IP.
- A global middleware sends X-Frame-Options, frame-ancestors, nosniff,
  Referrer-Policy, Permissions-Policy and HSTS over HTTPS, and drops
  X-Powered-By.
- The weekly time sheet locks the shown week and selected day, so the
  browser cannot set them.
- Users who may not see personal identity codes can save a new code at
  most 10 times an hour, so the duplicate check cannot be used to test codes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 06:25:26 +02:00

125 lines
4.1 KiB
PHP

<?php
use App\Modules\Users\Application\UseCases\CheckLoginAllowed;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Livewire\Component;
new class extends Component
{
public const int MAX_ATTEMPTS_PER_MINUTE = 5;
/**
* Failed attempts from one address across all accounts, so one common
* password cannot be tried on every account (password spraying).
*/
public const int MAX_ATTEMPTS_PER_IP_PER_MINUTE = 20;
public string $email = '';
public string $password = '';
public bool $remember = false;
/**
* Set when EnsureAccountIsUsable ended the session. Passed in the URL,
* not flashed: a Livewire request that gets redirected here would
* consume a flash message before the browser shows the page.
*/
public bool $accessEnded = false;
public function mount(): void
{
$this->accessEnded = request()->query('access') === 'ended';
}
public function login(CheckLoginAllowed $checkLoginAllowed): void
{
$this->email = trim($this->email);
$this->validate(
['email' => ['required', 'string', 'email'], 'password' => ['required', 'string']],
[
'email.required' => __('auth.email_required'),
'email.email' => __('auth.email_invalid'),
'password.required' => __('auth.password_required'),
],
);
$email = Str::lower(trim($this->email));
$throttleKey = Str::transliterate($email.'|'.request()->ip());
$ipThrottleKey = 'login-ip|'.request()->ip();
foreach ([$throttleKey => self::MAX_ATTEMPTS_PER_MINUTE, $ipThrottleKey => self::MAX_ATTEMPTS_PER_IP_PER_MINUTE] as $key => $maxAttempts) {
if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
throw ValidationException::withMessages([
'email' => __('auth.throttle', ['seconds' => RateLimiter::availableIn($key)]),
]);
}
}
$signedIn = Auth::attemptWhen(
['email' => $email, 'password' => $this->password],
fn ($user): bool => $checkLoginAllowed->execute($user->getAuthIdentifier()),
$this->remember,
);
if (! $signedIn) {
RateLimiter::hit($throttleKey);
RateLimiter::hit($ipThrottleKey);
$this->reset('password');
// One message for a wrong password and a disabled account, so the
// form never confirms that a disabled account's password is right.
throw ValidationException::withMessages(['email' => __('auth.failed')]);
}
RateLimiter::clear($throttleKey);
session()->regenerate();
$this->redirectIntended(route('home'));
}
public function render(): mixed
{
return $this->view()->title(__('auth.login_title'));
}
};
?>
<div class="mx-auto max-w-md space-y-6 p-4 sm:p-6 sm:pt-16">
<h1 class="text-2xl font-semibold">{{ __('auth.login_title') }}</h1>
@if ($accessEnded)
<x-alert role="status" color="yellow" light icon="exclamation-triangle" :text="__('auth.account_disabled')" />
@elseif (session('status'))
<x-alert role="status" color="green" light icon="information-circle" :text="session('status')" />
@endif
<form wire:submit="login" class="space-y-6" novalidate>
<x-input
wire:model="email"
type="email"
:label="__('auth.email')"
autocomplete="username"
required
/>
<x-password-field
wire:model="password"
:label="__('auth.password_label')"
autocomplete="current-password"
required
/>
<x-checkbox wire:model="remember" :label="__('auth.remember')" />
<div class="flex flex-wrap items-center justify-between gap-4">
<x-button submit :text="__('auth.login')" />
<x-link :href="route('password.request')" navigate :text="__('auth.forgot_password')" />
</div>
</form>
</div>