Debian Codegraph @

vendor/spomky-labs/otphp/src/HOTP.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace OTPHP;
6
7 use function is_int;
8 use OTPHP\Exception\InvalidParameterException;
9
10 /**
11 * Note: This class is not marked as readonly because the verify() method
12 * updates the counter state on successful verification, which is an intentional
13 * side-effect to prevent OTP reuse.
14 *
15 * @see \OTPHP\Test\HOTPTest
16 */
17 final class HOTP extends OTP implements HOTPInterface
18 {
19 private const DEFAULT_WINDOW = 0;
20
21 public static function create(
22 null|string $secret = null,
23 int $counter = self::DEFAULT_COUNTER,
24 string $digest = self::DEFAULT_DIGEST,
25 int $digits = self::DEFAULT_DIGITS,
26 ?int $secretSize = null
27 ): self {
28 $htop = $secret !== null
29 ? self::createFromSecret($secret)
30 : self::generate($secretSize)
31 ;
32 $htop->setCounter($counter);
33 $htop->setDigest($digest);
34 $htop->setDigits($digits);
35
36 return $htop;
37 }
38
39 public static function createFromSecret(string $secret): self
40 {
41 $htop = new self($secret);
42 $htop->setCounter(self::DEFAULT_COUNTER);
43 $htop->setDigest(self::DEFAULT_DIGEST);
44 $htop->setDigits(self::DEFAULT_DIGITS);
45
46 return $htop;
47 }
48
49 /**
50 * @param positive-int|null $secretSize
51 */
52 public static function generate(?int $secretSize = null): self
53 {
54 return self::createFromSecret(self::generateSecret($secretSize));
55 }
56
57 /**
58 * @return 0|positive-int
59 */
60 public function getCounter(): int
61 {
62 $value = $this->getParameter('counter');
63 (is_int($value) && $value >= 0) || throw new InvalidParameterException(
64 'Invalid "counter" parameter.',
65 'counter',
66 $value
67 );
68
69 return $value;
70 }
71
72 public function getProvisioningUri(): string
73 {
74 return $this->generateURI('hotp', [
75 'counter' => $this->getCounter(),
76 ]);
77 }
78
79 /**
80 * If the counter is not provided, the OTP is verified at the actual counter.
81 *
82 * @param null|0|positive-int $counter
83 */
84 public function verify(string $otp, null|int $counter = null, null|int $window = null): bool
85 {
86 $counter >= 0 || throw new InvalidParameterException(
87 'The counter must be at least 0.',
88 'counter',
89 $counter
90 );
91
92 if ($counter === null) {
93 $counter = $this->getCounter();
94 } elseif ($counter < $this->getCounter()) {
95 return false;
96 }
97
98 return $this->verifyOtpWithWindow($otp, $counter, $window);
99 }
100
101 public function setCounter(int $counter): void
102 {
103 $this->setParameter('counter', $counter);
104 }
105
106 public function withCounter(int $counter): self
107 {
108 $otp = clone $this;
109 $otp->setParameter('counter', $counter);
110
111 return $otp;
112 }
113
114 /**
115 * @return array<non-empty-string, callable>
116 */
117 protected function getParameterMap(): array
118 {
119 return [...parent::getParameterMap(), ...[
120 'counter' => static function (mixed $value): int {
121 $value = (int) $value;
122 $value >= 0 || throw new InvalidParameterException(
123 'Counter must be at least 0.',
124 'counter',
125 $value
126 );
127
128 return $value;
129 },
130 ]];
131 }
132
133 private function updateCounter(int $counter): void
134 {
135 $this->setCounter($counter);
136 }
137
138 /**
139 * @param null|0|positive-int $window
140 */
141 private function getWindow(null|int $window): int
142 {
143 return abs($window ?? self::DEFAULT_WINDOW);
144 }
145
146 /**
147 * @param non-empty-string $otp
148 * @param 0|positive-int $counter
149 * @param null|0|positive-int $window
150 */
151 private function verifyOtpWithWindow(string $otp, int $counter, null|int $window): bool
152 {
153 $window = $this->getWindow($window);
154
155 for ($i = $counter; $i <= $counter + $window; ++$i) {
156 if ($this->compareOTP($this->at($i), $otp)) {
157 $this->updateCounter($i + 1);
158
159 return true;
160 }
161 }
162
163 return false;
164 }
165 }