Passed
Push — feature/settings-2fa ( d5cfe0...3c3c7f )
by Chris
30:52 queued 23:32
created

Google2FA::handle()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 32
rs 7.6666
cc 10
nc 11
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
25
            // If tokens do not match, cookie is no longer valid.
26
            if ($remember !== null && $remember !== $user->getRememberDeviceToken()) {
27
                Cookie::forget($user->getRememberDeviceKey());
28
            }
29
30
            if ($authenticator->isAuthenticated() || ($remember !== null && $remember === $user->getRememberDeviceToken())) {
31
                if (!$authenticator->isAuthenticated()) {
32
                    Log::notice('User skipped OTP entry with known device.', ['id' => $request->user()->id]);
33
                    $authenticator->login();
34
                }
35
                return $next($request);
36
            }
37
        } else {
38
            if ($authenticator->isAuthenticated()) {
39
                return $next($request);
40
            }
41
        }
42
43
        // Unlike \PragmaRX\Google2FALaravel\Middleware, set the intended url.
44
        // Check if the intended url already exists, if not then store in global session.
45
        if (!session()->has('url.expected')) {
46
            Session::put('url.expected', URL::full());
47
        }
48
        return $authenticator->makeRequestOneTimePasswordResponse();
49
    }
50
}
51