1 | <?php |
||
22 | class Manager |
||
23 | { |
||
24 | use SecretValidationTrait; |
||
25 | use OathTrait; |
||
26 | |||
27 | /** |
||
28 | * @var int the period parameter that defines the time (in seconds) the OTP token will be valid. Default is 30 for |
||
29 | * Google Authenticator. |
||
30 | */ |
||
31 | protected $counter = 30; |
||
32 | /** |
||
33 | * @var int the times in 30 second cycles to avoid slight out of sync code verification. Setting this to 1 cycle |
||
34 | * will be valid for 60 seconds. |
||
35 | */ |
||
36 | protected $cycles = 1; |
||
37 | /** |
||
38 | * @var TotpEncoderInterface |
||
39 | */ |
||
40 | protected $encoder; |
||
41 | |||
42 | /** |
||
43 | * Auth constructor. |
||
44 | * |
||
45 | * @param SecretKeyValidator|null $secretKeyValidator |
||
46 | * @param TotpEncoderInterface|null $encoder |
||
47 | */ |
||
48 | public function __construct(SecretKeyValidator $secretKeyValidator = null, TotpEncoderInterface $encoder = null) |
||
53 | |||
54 | /** |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function isGoogleAuthenticatorCompatibilityEnabled(): bool |
||
61 | |||
62 | /** |
||
63 | * @return Manager |
||
64 | */ |
||
65 | public function disableGoogleAuthenticatorCompatibility(): Manager |
||
73 | |||
74 | /** |
||
75 | * @return $this|Manager |
||
76 | */ |
||
77 | public function enableGoogleAuthenticatorCompatibility(): self |
||
89 | |||
90 | /** |
||
91 | * @return int |
||
92 | */ |
||
93 | public function getTokenLength(): int |
||
97 | |||
98 | /** |
||
99 | * @param int $tokenLength |
||
100 | * |
||
101 | * @return Manager |
||
102 | */ |
||
103 | public function setTokenLength($tokenLength): Manager |
||
109 | |||
110 | /** |
||
111 | * Wrapper function to Encoder::generateBase32RandomKey method. |
||
112 | * |
||
113 | * @param int $length |
||
114 | * @param string $prefix |
||
115 | * |
||
116 | * @throws InvalidSecretKeyException |
||
117 | * @return mixed|string |
||
118 | */ |
||
119 | public function generateSecretKey(int $length = 16, string $prefix = '') |
||
123 | |||
124 | /** |
||
125 | * @param int $value |
||
126 | * |
||
127 | * @return Manager |
||
128 | */ |
||
129 | public function setCycles(int $value): Manager |
||
135 | |||
136 | /** |
||
137 | * @return int |
||
138 | */ |
||
139 | public function getCycles(): int |
||
143 | |||
144 | /** |
||
145 | * @param $value |
||
146 | * |
||
147 | * @return Manager |
||
148 | */ |
||
149 | public function setCounter(int $value): Manager |
||
155 | |||
156 | /** |
||
157 | * @return int |
||
158 | */ |
||
159 | public function getCounter(): int |
||
163 | |||
164 | /** |
||
165 | * Returns the current Unix Timestamp divided by the $counter period. |
||
166 | * |
||
167 | * @return int |
||
168 | **/ |
||
169 | public function getTimestamp(): int |
||
173 | |||
174 | /** |
||
175 | * Get the current one time password for a base32 encoded secret. |
||
176 | * |
||
177 | * @param string $secret |
||
178 | * |
||
179 | * @throws Exception\InvalidCharactersException |
||
180 | * @throws InvalidSecretKeyException |
||
181 | * @return string |
||
182 | */ |
||
183 | public function getCurrentOneTimePassword(string $secret): string |
||
190 | |||
191 | /** |
||
192 | * Verifies user's key vs current timestamp. |
||
193 | * |
||
194 | * @param string $key the user's input key |
||
195 | * @param string $secret the secret used to |
||
196 | * @param int|null $previousTime |
||
197 | * @param int|null $time |
||
198 | * |
||
199 | * @throws InvalidSecretKeyException |
||
200 | * @throws Exception\InvalidCharactersException |
||
201 | * @return bool|int |
||
202 | */ |
||
203 | public function verify(string $key, string $secret, int $previousTime = null, int $time = null) |
||
219 | |||
220 | /** |
||
221 | * Validates the key (OTP) and returns true if valid, false otherwise. |
||
222 | * |
||
223 | * @param string $key |
||
224 | * @param string $seed |
||
225 | * @param int $startTime |
||
226 | * @param int $time |
||
227 | * @param int|null $previousTime |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | protected function validateOneTimePassword( |
||
248 | } |
||
249 |