death-notifier/src/main/php/com/fwdekker/deathnotifier/validation/EqualsCliPasswordRule.php

48 lines
1.8 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier\validation;
use com\fwdekker\deathnotifier\Config;
use com\fwdekker\deathnotifier\IllegalStateError;
/**
* Validates that the input equals the CLI password.
*/
class EqualsCliPasswordRule extends Rule
{
/**
* The key in the configuration at which the hash of the CLI password is stored.
*/
public const CONFIG_KEY = "admin.cli_password";
/**
* The default value of the CLI password.
*/
public const DEFAULT = "REPLACE THIS WITH A SECRET VALUE";
/**
* Validates that the input equals the CLI password.
*
* @param array<int|string, mixed> $inputs the list of inputs in which the value at {@see $key} should be checked
* @param string $key the key in {@see $inputs} of the input to check
* @return void if the checked input equals the CLI password
* @throws InvalidTypeException if the CLI password is a blank string, if the CLI password is at its default
* value, or if the checked input is not set
* @throws InvalidValueException if the checked input does not equal the CLI password
*/
public function check(array $inputs, string $key): void
{
if (!Config::has(self::CONFIG_KEY) || trim(Config::get(self::CONFIG_KEY)) === "")
throw new IllegalStateError("The CLI is disabled because the CLI password is not set.");
if (Config::get(self::CONFIG_KEY) === self::DEFAULT)
throw new IllegalStateError("The CLI is disabled because the CLI password is set to the default.");
if (!isset($inputs[$key]))
throw new InvalidTypeException("This operation requires the CLI password.");
if (!password_verify($inputs[$key], Config::get(self::CONFIG_KEY)))
throw new InvalidValueException("Incorrect CLI password.", $key);
}
}