aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/spomky-labs/otphp/src/ParameterTrait.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/spomky-labs/otphp/src/ParameterTrait.php')
-rw-r--r--vendor/spomky-labs/otphp/src/ParameterTrait.php32
1 files changed, 22 insertions, 10 deletions
diff --git a/vendor/spomky-labs/otphp/src/ParameterTrait.php b/vendor/spomky-labs/otphp/src/ParameterTrait.php
index b05092351..3b2641e0c 100644
--- a/vendor/spomky-labs/otphp/src/ParameterTrait.php
+++ b/vendor/spomky-labs/otphp/src/ParameterTrait.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace OTPHP;
use function array_key_exists;
+use function assert;
use function in_array;
use InvalidArgumentException;
use function is_int;
@@ -13,18 +14,24 @@ use function is_string;
trait ParameterTrait
{
/**
- * @var array<string, mixed>
+ * @var array<non-empty-string, mixed>
*/
private array $parameters = [];
+ /**
+ * @var non-empty-string|null
+ */
private null|string $issuer = null;
+ /**
+ * @var non-empty-string|null
+ */
private null|string $label = null;
private bool $issuer_included_as_parameter = true;
/**
- * @return array<string, mixed>
+ * @return array<non-empty-string, mixed>
*/
public function getParameters(): array
{
@@ -40,7 +47,7 @@ trait ParameterTrait
public function getSecret(): string
{
$value = $this->getParameter('secret');
- is_string($value) || throw new InvalidArgumentException('Invalid "secret" parameter.');
+ (is_string($value) && $value !== '') || throw new InvalidArgumentException('Invalid "secret" parameter.');
return $value;
}
@@ -78,7 +85,7 @@ trait ParameterTrait
public function getDigits(): int
{
$value = $this->getParameter('digits');
- is_int($value) || throw new InvalidArgumentException('Invalid "digits" parameter.');
+ (is_int($value) && $value > 0) || throw new InvalidArgumentException('Invalid "digits" parameter.');
return $value;
}
@@ -86,7 +93,7 @@ trait ParameterTrait
public function getDigest(): string
{
$value = $this->getParameter('algorithm');
- is_string($value) || throw new InvalidArgumentException('Invalid "algorithm" parameter.');
+ (is_string($value) && $value !== '') || throw new InvalidArgumentException('Invalid "algorithm" parameter.');
return $value;
}
@@ -137,20 +144,21 @@ trait ParameterTrait
}
/**
- * @return array<string, callable>
+ * @return array<non-empty-string, callable>
*/
protected function getParameterMap(): array
{
return [
- 'label' => function ($value) {
+ 'label' => function (string $value): string {
+ assert($value !== '');
$this->hasColon($value) === false || throw new InvalidArgumentException(
'Label must not contain a colon.'
);
return $value;
},
- 'secret' => static fn ($value): string => mb_strtoupper(trim((string) $value, '=')),
- 'algorithm' => static function ($value): string {
+ 'secret' => static fn (string $value): string => mb_strtoupper(trim($value, '=')),
+ 'algorithm' => static function (string $value): string {
$value = mb_strtolower($value);
in_array($value, hash_algos(), true) || throw new InvalidArgumentException(sprintf(
'The "%s" digest is not supported.',
@@ -164,7 +172,8 @@ trait ParameterTrait
return (int) $value;
},
- 'issuer' => function ($value) {
+ 'issuer' => function (string $value): string {
+ assert($value !== '');
$this->hasColon($value) === false || throw new InvalidArgumentException(
'Issuer must not contain a colon.'
);
@@ -174,6 +183,9 @@ trait ParameterTrait
];
}
+ /**
+ * @param non-empty-string $value
+ */
private function hasColon(string $value): bool
{
$colons = [':', '%3A', '%3a'];