aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/spomky-labs/otphp/src/TOTP.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/spomky-labs/otphp/src/TOTP.php')
-rw-r--r--vendor/spomky-labs/otphp/src/TOTP.php19
1 files changed, 14 insertions, 5 deletions
diff --git a/vendor/spomky-labs/otphp/src/TOTP.php b/vendor/spomky-labs/otphp/src/TOTP.php
index e9bce9e14..3a7d72870 100644
--- a/vendor/spomky-labs/otphp/src/TOTP.php
+++ b/vendor/spomky-labs/otphp/src/TOTP.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace OTPHP;
+use function assert;
use InvalidArgumentException;
use function is_int;
@@ -50,7 +51,7 @@ final class TOTP extends OTP implements TOTPInterface
public function getPeriod(): int
{
$value = $this->getParameter('period');
- is_int($value) || throw new InvalidArgumentException('Invalid "period" parameter.');
+ (is_int($value) && $value > 0) || throw new InvalidArgumentException('Invalid "period" parameter.');
return $value;
}
@@ -58,7 +59,7 @@ final class TOTP extends OTP implements TOTPInterface
public function getEpoch(): int
{
$value = $this->getParameter('epoch');
- is_int($value) || throw new InvalidArgumentException('Invalid "epoch" parameter.');
+ (is_int($value) && $value >= 0) || throw new InvalidArgumentException('Invalid "epoch" parameter.');
return $value;
}
@@ -128,7 +129,7 @@ final class TOTP extends OTP implements TOTPInterface
}
/**
- * @return array<string, callable>
+ * @return array<non-empty-string, callable>
*/
protected function getParameterMap(): array
{
@@ -152,7 +153,7 @@ final class TOTP extends OTP implements TOTPInterface
}
/**
- * @param array<string, mixed> $options
+ * @param array<non-empty-string, mixed> $options
*/
protected function filterOptions(array &$options): void
{
@@ -165,8 +166,16 @@ final class TOTP extends OTP implements TOTPInterface
ksort($options);
}
+ /**
+ * @param 0|positive-int $timestamp
+ *
+ * @return 0|positive-int
+ */
private function timecode(int $timestamp): int
{
- return (int) floor(($timestamp - $this->getEpoch()) / $this->getPeriod());
+ $timecode = (int) floor(($timestamp - $this->getEpoch()) / $this->getPeriod());
+ assert($timecode >= 0);
+
+ return $timecode;
}
}