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 ( 8a6e2f...0fa588 )
by François
04:23 queued 01:31
created

OtpLog::createTableQueries()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
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
namespace SURFnet\VPN\Server;
19
20
use PDO;
21
use PDOException;
22
23
/**
24
 * Keep track of the used OTP keys so they cannot be replayed.
25
 */
26
class OtpLog
27
{
28
    /** @var PDO */
29
    private $db;
30
31
    public function __construct(PDO $db)
32
    {
33
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
34
        $this->db = $db;
35
    }
36
37
    public function record($userId, $otpKey, $timeUnix)
38
    {
39
        $stmt = $this->db->prepare(
40
            'INSERT INTO otp_log (
41
                user_id,
42
                otp_key,
43
                time_unix
44
             ) 
45
             VALUES(
46
                :user_id, 
47
                :otp_key,
48
                :time_unix
49
             )'
50
        );
51
52
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
53
        $stmt->bindValue(':otp_key', $otpKey, PDO::PARAM_STR);
54
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
55
        try {
56
            $stmt->execute();
57
        } catch (PDOException $e) {
58
            return false;
59
        }
60
61
        return true;
62
    }
63
64
    public function housekeeping($timeUnix)
65
    {
66
        $stmt = $this->db->prepare(
67
            sprintf(
68
                'DELETE FROM otp_log
69
                    WHERE time_unix < :time_unix'
70
            )
71
        );
72
73
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
74
        $stmt->execute();
75
    }
76
77
    public function init()
78
    {
79
        $queryList = [
80
            'CREATE TABLE IF NOT EXISTS otp_log (
81
                user_id VARCHAR(255) NOT NULL,
82
                otp_key VARCHAR(255) NOT NULL,
83
                time_unix INTEGER NOT NULL,
84
                UNIQUE(user_id, otp_key)
85
            )',
86
        ];
87
88
        foreach ($queryList as $query) {
89
            $this->db->query($query);
90
        }
91
    }
92
}
93