| Conditions | 6 |
| Paths | 9 |
| Total Lines | 26 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 26 | public function verify($userId, $totpKey, $totpSecret = null) |
||
| 27 | { |
||
| 28 | // for the enroll phase totpSecret is also provided, use that then |
||
| 29 | // instead of fetching one from the DB |
||
| 30 | if (is_null($totpSecret)) { |
||
| 31 | if (!$this->storage->hasTotpSecret($userId)) { |
||
| 32 | throw new TotpException('user has no TOTP secret'); |
||
| 33 | } |
||
| 34 | $totpSecret = $this->storage->getTotpSecret($userId); |
||
| 35 | } |
||
| 36 | |||
| 37 | // store the attempt even before validating it, to be able to count |
||
| 38 | // the (failed) attempts |
||
| 39 | if (false === $this->storage->recordTotpKey($userId, $totpKey)) { |
||
| 40 | throw new TotpException('TOTP key replay'); |
||
| 41 | } |
||
| 42 | |||
| 43 | if (10 < $this->storage->getTotpAttemptCount($userId)) { |
||
| 44 | throw new TotpException('too many attempts at TOTP'); |
||
| 45 | } |
||
| 46 | |||
| 47 | $otp = new Otp(); |
||
| 48 | if (!$otp->checkTotp(Encoding::base32DecodeUpper($totpSecret), $totpKey)) { |
||
| 49 | throw new TotpException('invalid TOTP key'); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 |