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

TwoFactorController::rememberDevice()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 3
nc 3
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Cookie;
7
use Facades\App\Services\WhichPortal;
8
use Illuminate\Support\Facades\Lang;
9
use PragmaRX\Google2FALaravel\Support\Authenticator;
10
11
class TwoFactorController extends AuthController
12
{
13
    public function activate(Request $request)
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function activate()
Loading history...
14
    {
15
        $user = $request->user();
16
        $google2fa = app('pragmarx.google2fa');
17
        $secret = $google2fa->generateSecretKey();
0 ignored issues
show
Bug introduced by
The method generateSecretKey() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

17
        /** @scrutinizer ignore-call */ 
18
        $secret = $google2fa->generateSecretKey();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
18
        $qrImage = $google2fa->getQRCodeInline(
0 ignored issues
show
Bug introduced by
The method getQRCodeInline() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

18
        /** @scrutinizer ignore-call */ 
19
        $qrImage = $google2fa->getQRCodeInline(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
19
            config('app.name'),
20
            $user->email,
21
            $secret
22
        );
23
24
        return view('auth.two_factor', [
25
            'qr_image' => $qrImage,
26
            'secret' => $secret,
27
        ]);
28
    }
29
30
    public function deactivate(Request $request)
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function deactivate()
Loading history...
31
    {
32
        $user = $request->user();
33
        $user->google2fa_secret = null;
34
        $user->recovery_codes = null;
35
        $user->save();
36
        $user->refresh();
37
38
        $profile_url = '';
39
        if (WhichPortal::isApplicantPortal()) {
40
                $profile_url = route('settings.edit');
41
        } elseif (WhichPortal::isManagerPortal()) {
42
            $profile_url = route('manager.settings.edit');
43
        } elseif (WhichPortal::isAdminPortal()) {
44
            $profile_url = backpack_url('2fa');
45
        }
46
47
        return redirect($profile_url);
48
    }
49
50
    public function confirm(Request $request)
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function confirm()
Loading history...
51
    {
52
        $user = $request->user();
53
        $validatedData = $request->validate([
54
            'secret' => 'required|string',
55
            'one_time_password' => 'required|string',
56
        ]);
57
        $secret = $validatedData['secret'];
58
        $one_time_password = $validatedData['one_time_password'];
59
60
        // A 2fa secret is already set up, no need to do anything.
61
        if (!empty($user->google2fa_secret)) {
62
            return redirect()->route('home');
63
        }
64
65
        // Check that the one time password matches the secret.
66
        $authenticator = app(Authenticator::class)->boot($request);
67
        $isCorrect = $authenticator->verifyGoogle2FA($secret, $one_time_password);
68
69
        if ($isCorrect) {
70
            // The password matched the secret! Save the secret, and authenticate.
71
            $user->google2fa_secret = $secret;
72
            $user->save();
73
            $user->refresh();
74
            $authenticator->login();
75
76
            $this->rememberDevice($request);
77
78
            $recovery_codes_url = '';
79
            if (WhichPortal::isApplicantPortal()) {
80
                $recovery_codes_url = route('recovery_codes.show');
81
            } elseif (WhichPortal::isManagerPortal()) {
82
                $recovery_codes_url = route('manager.recovery_codes.show');
83
            } elseif (WhichPortal::isAdminPortal()) {
84
                $recovery_codes_url = route('admin.recovery_codes.show');
85
            }
86
87
            return redirect($recovery_codes_url);
88
        } else {
89
            $activation_url = '';
90
            if (WhichPortal::isApplicantPortal()) {
91
                $activation_url = route('two_factor.activate');
92
            } elseif (WhichPortal::isManagerPortal()) {
93
                $activation_url = route('manager.two_factor.activate');
94
            } elseif (WhichPortal::isAdminPortal()) {
95
                $activation_url = backpack_url('admin.two_factor.activate');
96
            }
97
98
            return redirect($activation_url)
99
                ->withErrors(['otp' => Lang::get('two_factor.activation_otp_error')]);
100
        }
101
    }
102
103
    public function redirectToExpected(Request $request)
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function redirectToExpected()
Loading history...
104
    {
105
        $this->rememberDevice($request);
106
        // Assuming 2fa passes, redirect to the expected url and remove it from session.
107
        // NOTE: the url.expected is set in app\Http\Middleware\Google2FA.php.
108
        $expectedUrl = session()->get('url.expected');
109
        session()->remove('url.expected');
110
111
        return redirect($expectedUrl);
112
    }
113
114
    protected function rememberDevice(Request $request)
1 ignored issue
show
Coding Style Documentation introduced by
Missing doc comment for function rememberDevice()
Loading history...
115
    {
116
        $user = $request->user();
117
        $remember = $request->input('remember_device');
118
119
        if ($remember) {
120
            if (empty($user->getRememberDeviceToken())) {
121
                $user->cycleRememberDeviceToken();
122
            }
123
            Cookie::queue(
124
                $user->getRememberDeviceKey(),
125
                $user->getRememberDeviceToken(),
126
                config('google2fa.lifetime')
127
            );
128
        }
129
    }
130
}
131