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