Passed
Push — master ( ec8b22...b724fa )
by Darko
08:07
created

Google2FAAuthenticator::isAuthenticated()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 15
rs 10
cc 3
nc 2
nop 0
1
<?php
2
3
namespace App\Support;
4
5
use PragmaRX\Google2FALaravel\Exceptions\InvalidSecretKey;
6
use PragmaRX\Google2FALaravel\Support\Authenticator;
7
8
class Google2FAAuthenticator extends Authenticator
9
{
10
    /**
11
     * Check if the user is authenticated for 2FA
12
     */
13
    public function isAuthenticated()
14
    {
15
        // First check - directly check for the cookie before any other logic
16
        $cookie = request()->cookie('2fa_trusted_device');
17
18
        if ($cookie && $this->checkCookieValidity($cookie)) {
19
            // Force the session to be marked as 2FA authenticated
20
            session([config('google2fa.session_var') => true]);
21
            session([config('google2fa.session_var').'.auth.passed_at' => time()]);
22
23
            // Successful authentication with cookie
24
            return true;
25
        }
26
27
        return parent::isAuthenticated();
28
    }
29
30
    /**
31
     * Directly validate the cookie without any output or logging
32
     */
33
    private function checkCookieValidity($cookie)
34
    {
35
        try {
36
            $data = @json_decode($cookie, true);
37
38
            if (! is_array($data)) {
39
                return false;
40
            }
41
42
            // Validate all required fields
43
            if (! isset($data['user_id'], $data['token'], $data['expires_at'])) {
44
                return false;
45
            }
46
47
            // Ensure the user ID matches
48
            if ((int) $data['user_id'] !== (int) $this->getUser()->id) {
49
                return false;
50
            }
51
52
            // Ensure the token is not expired
53
            if (time() > $data['expires_at']) {
54
                return false;
55
            }
56
57
            // All checks passed - this is a valid cookie
58
            return true;
59
        } catch (\Exception $e) {
60
            return false;
61
        }
62
    }
63
64
    protected function canPassWithoutCheckingOTP(): bool
65
    {
66
        if (! $this->getUser()->passwordSecurity) {
67
            return true;
68
        }
69
70
        return
71
            ! $this->getUser()->passwordSecurity->google2fa_enable ||
72
            ! $this->isEnabled() ||
73
            $this->noUserIsAuthenticated() ||
74
            $this->twoFactorAuthStillValid() ||
75
            $this->isDeviceTrusted();
76
    }
77
78
    /**
79
     * Check if current device is trusted
80
     */
81
    protected function isDeviceTrusted(): bool
82
    {
83
        try {
84
            $cookie = request()->cookie('2fa_trusted_device');
85
            $user = $this->getUser();
86
87
            if (! $cookie) {
88
                return false;
89
            }
90
91
            $data = @json_decode($cookie, true);
0 ignored issues
show
Bug introduced by
It seems like $cookie can also be of type array; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

91
            $data = @json_decode(/** @scrutinizer ignore-type */ $cookie, true);
Loading history...
92
93
            // Check for JSON decode errors silently
94
            if (json_last_error() !== JSON_ERROR_NONE) {
95
                return false;
96
            }
97
98
            // Validate the required fields silently
99
            if (! isset($data['user_id'], $data['token'], $data['expires_at'])) {
100
                return false;
101
            }
102
103
            // Check if the token belongs to the current user
104
            if ((int) $data['user_id'] !== (int) $user->id) {
105
                return false;
106
            }
107
108
            // Check if the token is expired
109
            if (time() > $data['expires_at']) {
110
                return false;
111
            }
112
113
            // Device is trusted and token is valid
114
            return true;
115
        } catch (\Exception $e) {
116
            // Silently handle any exceptions
117
            return false;
118
        }
119
    }
120
121
    /**
122
     * @return mixed
123
     *
124
     * @throws InvalidSecretKey
125
     */
126
    protected function getGoogle2FASecretKey()
127
    {
128
        $secret = $this->getUser()->passwordSecurity->{$this->config('otp_secret_column')};
129
130
        if (empty($secret)) {
131
            throw new InvalidSecretKey('Secret key cannot be empty.');
132
        }
133
134
        return $secret;
135
    }
136
137
    /**
138
     * Override the parent isEnabled method to force-disable 2FA when a trusted device is detected
139
     */
140
    public function isEnabled()
141
    {
142
        // Check for trusted device cookie
143
        $trustedCookie = request()->cookie('2fa_trusted_device');
144
145
        if ($trustedCookie && auth()->check()) {
146
            try {
147
                $cookieData = json_decode($trustedCookie, true);
0 ignored issues
show
Bug introduced by
It seems like $trustedCookie can also be of type array; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

147
                $cookieData = json_decode(/** @scrutinizer ignore-type */ $trustedCookie, true);
Loading history...
148
149
                if (json_last_error() === JSON_ERROR_NONE &&
150
                    isset($cookieData['user_id'], $cookieData['token'], $cookieData['expires_at']) &&
151
                    (int) $cookieData['user_id'] === (int) auth()->id() &&
152
                    time() <= $cookieData['expires_at']) {
153
154
                    // If we have a valid cookie, force-disable 2FA
155
                    session([config('google2fa.session_var') => true]);
156
                    session([config('google2fa.session_var').'.auth.passed_at' => time()]);
157
158
                    // Completely disable 2FA for this request
159
                    return false;
160
                }
161
            } catch (\Exception $e) {
162
                // Silently handle any exceptions
163
            }
164
        }
165
166
        // Otherwise, use the parent implementation
167
        return parent::isEnabled();
168
    }
169
}
170