GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Totp::verify()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
rs 8.439
cc 6
eloc 12
nc 9
nop 3
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Server;
11
12
use Otp\Otp;
13
use ParagonIE\ConstantTime\Encoding;
14
use SURFnet\VPN\Server\Exception\TotpException;
15
16
class Totp
17
{
18
    /** @var Storage */
19
    private $storage;
20
21
    public function __construct(Storage $storage)
22
    {
23
        $this->storage = $storage;
24
    }
25
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