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.
Completed
Push — master ( c87823...3d2057 )
by François
02:04
created

TwoFactor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B verifyTotp() 0 26 6
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Server;
20
21
use Base32\Base32;
22
use DateTime;
23
use Otp\Otp;
24
use SURFnet\VPN\Server\Exception\TwoFactorException;
25
26
class TwoFactor
27
{
28
    /** @var Storage */
29
    private $storage;
30
31
    /** @var \DateTime */
32
    private $dateTime;
33
34
    public function __construct(Storage $storage, DateTime $dateTime)
35
    {
36
        $this->storage = $storage;
37
        $this->dateTime = $dateTime;
38
    }
39
40
    public function verifyTotp($userId, $totpKey, $totpSecret = null)
41
    {
42
        // for the enroll phase totpSecret is also provided, use that then
43
        // instead of fetching one from the DB
44
        if (is_null($totpSecret)) {
45
            if (!$this->storage->hasTotpSecret($userId)) {
46
                throw new TwoFactorException('user has no TOTP secret');
47
            }
48
            $totpSecret = $this->storage->getTotpSecret($userId);
49
        }
50
51
        // store the attempt even before validating it, to be able to count
52
        // the (failed) attempts
53
        if (false === $this->storage->recordTotpKey($userId, $totpKey, $this->dateTime)) {
54
            throw new TwoFactorException('TOTP key replay');
55
        }
56
57
        if (10 < $this->storage->getTotpAttemptCount($userId)) {
58
            throw new TwoFactorException('too many attempts at TOTP');
59
        }
60
61
        $otp = new Otp();
62
        if (!$otp->checkTotp(Base32::decode($totpSecret), $totpKey)) {
63
            throw new TwoFactorException('invalid TOTP key');
64
        }
65
    }
66
}
67