Issues (404)

Branch: dev

app/Http/Middleware/Google2FA.php (2 issues)

1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use PragmaRX\Google2FALaravel\Support\Authenticator;
7
use Illuminate\Support\Facades\Cookie;
8
use Illuminate\Support\Facades\Log;
9
use Illuminate\Support\Facades\Session;
10
use Illuminate\Support\Facades\URL;
11
12
/**
13
 * Based on \PragmaRX\Google2FALaravel\Middleware
14
 */
15
class Google2FA
16
{
17
    public function handle($request, Closure $next)
18
    {
19
        $authenticator = app(Authenticator::class)->boot($request);
0 ignored issues
show
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        $authenticator = /** @scrutinizer ignore-call */ app(Authenticator::class)->boot($request);
Loading history...
20
        $user = $request->user();
21
22
        if ($user !== null) {
23
            $remember = $request->cookie($user->getRememberDeviceKey());
24
            // If tokens do not match, cookie is no longer valid.
25
            if ($remember !== null && $remember !== $user->getRememberDeviceToken()) {
26
                    Cookie::queue(
27
                        Cookie::forget($user->getRememberDeviceKey())
28
                    );
29
            }
30
31
            if ($authenticator->isAuthenticated() || ($remember !== null && $remember === $user->getRememberDeviceToken())) {
32
                if (!$authenticator->isAuthenticated()) {
33
                    Log::notice('User skipped OTP entry with known device.', ['id' => $request->user()->id]);
34
                    $authenticator->login();
35
                }
36
                return $next($request);
37
            }
38
        } else {
39
            if ($authenticator->isAuthenticated()) {
40
                return $next($request);
41
            }
42
        }
43
44
        // Unlike \PragmaRX\Google2FALaravel\Middleware, set the intended url.
45
        // Check if the intended url already exists, if not then store in global session.
46
        if (!session()->has('url.expected')) {
0 ignored issues
show
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        if (!/** @scrutinizer ignore-call */ session()->has('url.expected')) {
Loading history...
47
            Session::put('url.expected', URL::full());
48
        }
49
        return $authenticator->makeRequestOneTimePasswordResponse();
50
    }
51
}
52