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]>
80 lines
2.4 KiB
PHP
80 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Users\Application\UseCases;
|
|
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
use App\Modules\Users\Application\Contracts\SecretHasher;
|
|
use App\Modules\Users\Domain\Contracts\LoginSecrets;
|
|
use App\Modules\Users\Domain\Exceptions\InvalidLoginPin;
|
|
use App\Modules\Users\Domain\Exceptions\WrongPassword;
|
|
use App\Modules\Users\Domain\ValueObjects\LoginPin;
|
|
|
|
/**
|
|
* Users' own PIN codes for switching user on a machine console (spec
|
|
* 19.6): a user sets one with their password, and the console signs them
|
|
* in with it while their account may sign in. Only the hash is stored.
|
|
*/
|
|
final class LoginPins
|
|
{
|
|
public function __construct(
|
|
private LoginSecrets $secrets,
|
|
private SecretHasher $hasher,
|
|
private CheckLoginAllowed $loginAllowed,
|
|
private TransactionManager $transactions,
|
|
) {}
|
|
|
|
/**
|
|
* @throws InvalidLoginPin
|
|
* @throws WrongPassword
|
|
*/
|
|
public function set(int $userId, string $currentPassword, string $pin): void
|
|
{
|
|
$pin = new LoginPin($pin);
|
|
$this->checkPassword($userId, $currentPassword);
|
|
|
|
$this->transactions->run(fn () => $this->secrets->storePinHash($userId, $this->hasher->hash($pin->value)));
|
|
}
|
|
|
|
/**
|
|
* @throws WrongPassword
|
|
*/
|
|
public function remove(int $userId, string $currentPassword): void
|
|
{
|
|
$this->checkPassword($userId, $currentPassword);
|
|
|
|
$this->transactions->run(fn () => $this->secrets->storePinHash($userId, null));
|
|
}
|
|
|
|
public function has(int $userId): bool
|
|
{
|
|
return $this->secrets->pinHashOf($userId) !== null;
|
|
}
|
|
|
|
/**
|
|
* Whether the PIN code is the user's and the user may sign in today.
|
|
*/
|
|
public function verify(int $userId, string $pin): bool
|
|
{
|
|
$hash = $this->secrets->pinHashOf($userId);
|
|
|
|
return $hash !== null
|
|
&& preg_match('/^\d{'.LoginPin::MIN_LENGTH.','.LoginPin::MAX_LENGTH.'}$/', trim($pin)) === 1
|
|
&& $this->hasher->check(trim($pin), $hash)
|
|
&& $this->loginAllowed->execute($userId);
|
|
}
|
|
|
|
/**
|
|
* @throws WrongPassword
|
|
*/
|
|
private function checkPassword(int $userId, string $password): void
|
|
{
|
|
$hash = $this->secrets->passwordHashOf($userId);
|
|
|
|
if ($hash === null || ! $this->hasher->check($password, $hash)) {
|
|
throw new WrongPassword;
|
|
}
|
|
}
|
|
}
|