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]>
This commit is contained in:
2026-11-29 09:58:11 +02:00
co-authored by Claude Sonnet 5.5
parent 87e38260dc
commit 32c33e2a64
55 changed files with 1660 additions and 194 deletions
@@ -0,0 +1,74 @@
<?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();
}
}
@@ -0,0 +1,84 @@
<?php
namespace App\Modules\Users\Presentation\Livewire\Concerns;
use App\Modules\Users\Application\Contracts\UserAccountQueries;
use App\Modules\Users\Application\DTOs\UserOption;
use App\Modules\Users\Application\UseCases\LoginPins;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Attributes\Computed;
/**
* Signing in with a user's own console PIN code (spec 19.6): the user picks
* their name and types the PIN. Five wrong PIN codes lock the user's PIN
* sign-in for 15 minutes, and one address may try 20 times a minute. Ends
* the request's guard's earlier sign-in (the console's own on console
* pages) and starts a new session for the user.
*/
trait SignsInWithPin
{
public string $userId = '';
public string $pin = '';
/**
* @return list<UserOption>
*/
#[Computed]
public function pinUsers(): array
{
return app(UserAccountQueries::class)->pinLoginOptions();
}
/**
* @return bool Whether the user is now signed in.
*/
protected function signInWithPin(LoginPins $pins): bool
{
$this->resetErrorBag();
$user = collect($this->pinUsers)->firstWhere('id', (int) $this->userId);
if ($user === null) {
$this->addError('userId', __('users.pin_switch.errors.user'));
return false;
}
$userKey = 'pin-login|user|'.$user->id;
$ipKey = 'pin-login|ip|'.request()->ip();
foreach ([[$userKey, 5], [$ipKey, 20]] as [$key, $max]) {
if (RateLimiter::tooManyAttempts($key, $max)) {
$this->addError('pin', __('users.pin_switch.errors.locked', ['minutes' => (int) ceil(RateLimiter::availableIn($key) / 60)]));
$this->pin = '';
return false;
}
}
RateLimiter::hit($ipKey, 60);
if (! $pins->verify($user->id, $this->pin)) {
RateLimiter::hit($userKey, 900);
$this->addError('pin', __('users.pin_switch.errors.pin'));
$this->pin = '';
return false;
}
RateLimiter::clear($userKey);
if (Auth::check()) {
Auth::logout();
session()->invalidate();
session()->regenerateToken();
}
Auth::loginUsingId($user->id);
session()->regenerate();
return true;
}
}