Passed
Push — task/laravel-breadcrumbs ( 3beccb...a96280 )
by Yonathan
10:46 queued 10s
created

Google2FA   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 19
dl 0
loc 35
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 33 10
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)
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function handle()
Loading history...
18
    {
19
        $authenticator = app(Authenticator::class)->boot($request);
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')) {
47
            Session::put('url.expected', URL::full());
48
        }
49
        return $authenticator->makeRequestOneTimePasswordResponse();
50
    }
51
}
52