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; } } }