AuthController::logout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Yaro\Jarboe\Http\Controllers;
4
5
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
6
use BaconQrCode\Renderer\ImageRenderer;
7
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
8
use BaconQrCode\Writer;
9
use Illuminate\Routing\Controller;
10
use Illuminate\Support\Facades\Auth;
11
use PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException;
12
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
13
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
14
use PragmaRX\Google2FA\Google2FA;
15
use Yaro\Jarboe\Events\Auth\InvalidOTP;
16
use Yaro\Jarboe\Events\Auth\LoginFailed;
17
use Yaro\Jarboe\Events\Auth\Registered;
18
use Yaro\Jarboe\Events\Auth\LoginSuccess;
19
use Yaro\Jarboe\Events\Auth\Logout;
20
use Yaro\Jarboe\Http\Middleware\RedirectIfAdminAuthenticated;
21
use Yaro\Jarboe\Http\Requests\Auth\LoginRequest;
22
use Yaro\Jarboe\Http\Requests\Auth\RegisterRequest;
23
24
class AuthController extends Controller
25
{
26
    public function __construct()
27
    {
28
        $this->middleware(RedirectIfAdminAuthenticated::class)->except([
29
            'logout',
30
        ]);
31
    }
32
33
    public function root()
34
    {
35
        return redirect(admin_url('login'));
36
    }
37
38
    public function showLogin()
39
    {
40
        return view('jarboe::auth.login', [
41
            'shouldOTP' => $this->shouldOTP(),
42
        ]);
43
    }
44
45
    protected function getAuthCredentials(LoginRequest $request): array
46
    {
47
        return $request->only('email', 'password');
48
    }
49
50
    public function login(LoginRequest $request)
51
    {
52
        $credentials = $this->getAuthCredentials($request);
53
        $shouldRemember = (bool) $request->get('remember');
54
        if (Auth::guard(admin_user_guard())->attempt($credentials, $shouldRemember)) {
55
            if ($this->isValidOTP(admin_user(), $request->get('otp'))) {
56
                event(new LoginSuccess(admin_user()));
57
                return redirect(admin_url(config('jarboe.admin_panel.dashboard')));
58
            }
59
            event(new InvalidOTP(admin_user()));
60
            Auth::guard(admin_user_guard())->logout();
61
        }
62
        event(new LoginFailed($request));
63
64
        return redirect()->back()->withErrors(['email' => [__('jarboe::auth.user_not_found')]]);
65
    }
66
67
    public function logout()
68
    {
69
        event(new Logout(admin_user()));
70
        Auth::guard(admin_user_guard())->logout();
71
72
        return redirect(admin_url('login'));
73
    }
74
75
    public function showRegister()
76
    {
77
        return view('jarboe::auth.register');
78
    }
79
80
    public function register(RegisterRequest $request)
81
    {
82
        $model = config('jarboe.admin_panel.admin_model');
83
84
        $data = $request->only('name', 'email') + [
85
            'password' => bcrypt($request->get('password')),
86
            'avatar' => '',
87
        ] + $this->getDataForOTP();
88
        $admin = $model::create($data);
89
        event(new Registered($admin));
90
91
        Auth::guard(admin_user_guard())->login($admin);
92
        event(new LoginSuccess(admin_user()));
93
94
        if ($this->shouldOTP()) {
95
            $url = app(Google2FA::class)->getQRCodeUrl(
96
                config('jarboe.admin_panel.two_factor_auth.company_name', config('app.name')),
97
                $admin->email,
98
                $admin->otp_secret
99
            );
100
            $writer = new Writer(new ImageRenderer(
101
                new RendererStyle(180),
102
                new SvgImageBackEnd()
103
            ));
104
            return view('jarboe::auth.otp', [
105
                'svg' => $writer->writeString($url),
106
                'secret' => $admin->otp_secret,
107
            ]);
108
        }
109
        return redirect(admin_url(config('jarboe.admin_panel.dashboard')));
110
    }
111
112
    private function isValidOTP($user, $otp): bool
113
    {
114
        if (!$this->shouldOTP()) {
115
            return true;
116
        }
117
118
        $isValid = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isValid is dead and can be removed.
Loading history...
119
        try {
120
            $isValid = app(Google2FA::class)->verifyKey($user->otp_secret, $otp);
121
        } catch (IncompatibleWithGoogleAuthenticatorException $e) {
122
            // OTP is not valid by default.
123
        } catch (InvalidCharactersException $e) {
124
            // OTP is not valid by default.
125
        } catch (SecretKeyTooShortException $e) {
126
            // OTP is not valid by default.
127
        }
128
129
        return $isValid;
130
    }
131
132
    private function shouldOTP(): bool
133
    {
134
        return (bool) config('jarboe.admin_panel.two_factor_auth.enabled');
135
    }
136
137
    private function getDataForOTP(): array
138
    {
139
        if ($this->shouldOTP()) {
140
            return [
141
                'otp_secret' => app(Google2FA::class)->generateSecretKey(),
142
            ];
143
        }
144
145
        return [];
146
    }
147
}
148