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 ( c0d893...b5b87f )
by James
05:14
created

Authenticator   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 68.33%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 11
dl 0
loc 192
ccs 41
cts 60
cp 0.6833
rs 10
c 0
b 0
f 0

11 Methods

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