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:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user