Files
Toiminnanohjaus/app/Modules/Users/Presentation/Livewire/Concerns/SignsInWithPassword.php
T
petriandClaude Sonnet 5.5 32c33e2a64 Give the machine console its own session with PIN sign-in, make counters cumulative
The console under console/ has its own session cookie and console guard
(UseConsoleSession), so signing out or changing user there leaves the
normal interface alone. Links open it in a new window through a one-time
handoff key; a signed-out console asks for a PIN code or password on its
own login page. Users set their 4-6 digit PIN on the profile's PIN page,
and the console's change user dialog takes it on a keypad; five wrong
PINs lock PIN sign-in for 15 minutes.

Machine counters are cumulative: users record how much a job used and it
is added to the latest reading. A maintenance record restarts the whole
counter interval from the reading at the record.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-29 09:58:11 +02:00

75 lines
2.6 KiB
PHP

<?php
namespace App\Modules\Users\Presentation\Livewire\Concerns;
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;
/**
* Signing in with email and password, on the login page and on the
* machine console's sign-in page: 5 failed attempts a minute per email and
* address, 20 per address across all accounts (password spraying), and one
* message for a wrong password and a disabled account. Signs in to the
* request's guard (the console's own on console pages).
*
* @property string $email
* @property string $password
*/
trait SignsInWithPassword
{
public string $email = '';
public string $password = '';
/**
* @throws ValidationException
*/
protected function signInWithPassword(CheckLoginAllowed $checkLoginAllowed, bool $remember = false): 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 => 5, $ipThrottleKey => 20] 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()),
$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();
}
}