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 ( c4a10f...5fd32e )
by James
01:57
created

Authenticator::verifyOneTimePassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PragmaRX\Google2FALaravel\Support;
6
7
use Illuminate\Http\Request as IlluminateRequest;
8
use Illuminate\Support\Facades\DB;
9
use PragmaRX\Google2FALaravel\Events\EmptyOneTimePasswordReceived;
10
use PragmaRX\Google2FALaravel\Events\LoginFailed;
11
use PragmaRX\Google2FALaravel\Events\LoginSucceeded;
12
use PragmaRX\Google2FALaravel\Exceptions\InvalidOneTimePassword;
13
use PragmaRX\Google2FALaravel\Google2FA;
14
15
/**
16
 * Class Authenticator
17
 */
18
class Authenticator extends Google2FA
19
{
20
    use ErrorBag, Input, Response, Session;
21
22
    /**
23
     * The current password.
24
     *
25
     * @var
26
     */
27
    protected $password;
28
29
    /**
30
     * Authenticator constructor.
31
     *
32
     * @param \Illuminate\Http\Request $request
33
     */
34 1
    public function __construct(IlluminateRequest $request)
35
    {
36 1
        parent::__construct($request);
37 1
    }
38
39
    /**
40
     * Authenticator boot.
41
     *
42
     * @param $request
43
     *
44
     * @return Google2FA
45
     */
46 1
    public function boot($request)
47
    {
48 1
        parent::boot($request);
49
50 1
        return $this;
51
    }
52
53
    /**
54
     * Authenticator boot for API usage.
55
     *
56
     * @param $request
57
     *
58
     * @return Google2FA
59
     */
60
    public function bootStateless($request)
61
    {
62
        $this->boot($request);
63
64
        $this->setStateless();
65
66
        return $this;
67
    }
68
69
    /**
70
     * Removes expired tokens from the database.
71
     */
72 1
    public function cleanupTokens(): void
73
    {
74 1
        DB::table('2fa_tokens')->where('expires_at', '<=', date('Y-m-d H:i:s'))->delete();
75
    }
76
77
    /**
78
     * Fire login (success or failed).
79
     *
80
     * @param $succeeded
81
     *
82
     */
83
    private function fireLoginEvent($succeeded)
84
    {
85
        event(
86
            $succeeded
87
                ? new LoginSucceeded($this->getUser())
88
                : new LoginFailed($this->getUser())
89
        );
90
91
        return $succeeded;
92
    }
93
94
    /**
95
     * Get the OTP from user input.
96
     *
97
     * @throws InvalidOneTimePassword
98
     *
99
     * @return mixed
100
     */
101
    protected function getOneTimePassword()
102
    {
103
        $password = $this->getInputOneTimePassword();
104
105
        if (is_null($password) || empty($password)) {
106
            event(new EmptyOneTimePasswordReceived());
107
108
            if ($this->config('throw_exceptions', true)) {
109
                throw new InvalidOneTimePassword(config('google2fa.error_messages.cannot_be_empty'));
110
            }
111
        }
112
113
        return $password;
114
    }
115
116
    /**
117
     * Check if the current use is authenticated via OTP.
118
     *
119
     * @return bool
120
     */
121 1
    public function isAuthenticated()
122
    {
123 1
        return $this->canPassWithoutCheckingOTP() || ($this->checkOTP() === Constants::OTP_VALID);
124
    }
125
126
    /**
127
     * @return bool
128
     */
129 1
    public function hasValidCookieToken(): bool
130
    {
131 1
        $storeInCookie = config('google2fa.store_in_cookie', false);
132 1
        if (false === $storeInCookie) {
133 1
            return false;
134
        }
135
        $cookieName = config('google2fa.cookie_name', 'google2fa_token');
136
137
        /** @var Request $request */
138
        $token = $this->getRequest()->cookies->get($cookieName);
139
        $time  = date('Y-m-d H:i:s');
140
141
        // check DB for token.
142
        $count = DB::table('2fa_tokens')
143
                   ->where('token', $token)
144
                   ->where('expires_at', '>', $time)
145
                   ->where('user_id', $this->getUser()->id)->count();
146
147
        return 1 === $count;
148
    }
149
150
    /**
151
     * Check if it is already logged in or passable without checking for an OTP.
152
     *
153
     * @return bool
154
     */
155 1
    protected function canPassWithoutCheckingOTP()
156
    {
157
        return
158 1
            !$this->isEnabled()
159 1
            || $this->noUserIsAuthenticated()
160 1
            || !$this->isActivated()
161 1
            || $this->twoFactorAuthStillValid();
162
    }
163
164
    /**
165
     * Check if the input OTP is valid. Returns one of the possible OTP_STATUS codes:
166
     * 'empty', 'valid' or 'invalid'.
167
     *
168
     * @return string
169
     */
170 1
    protected function checkOTP()
171
    {
172 1
        if (!$this->inputHasOneTimePassword() || empty($this->getInputOneTimePassword())) {
173 1
            return Constants::OTP_EMPTY;
174
        }
175
176
        $isValid = $this->verifyOneTimePassword();
177
178
        if ($isValid) {
179
            $this->login();
180
            $this->fireLoginEvent($isValid);
181
182
            return Constants::OTP_VALID;
183
        }
184
185
        return Constants::OTP_INVALID;
186
    }
187
188
    /**
189
     * Verify the OTP.
190
     *
191
     * @throws InvalidOneTimePassword
192
     *
193
     * @return mixed
194
     */
195
    protected function verifyOneTimePassword()
196
    {
197
        return $this->verifyAndStoreOneTimePassword($this->getOneTimePassword());
198
    }
199
}
200