Issues (69)

src/Middleware/VerifyTwoFactorOtp.php (5 issues)

1
<?php
2
3
namespace Thinkstudeo\Rakshak\Middleware;
4
5
use Closure;
6
use Illuminate\Support\Carbon;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Cache;
9
use Thinkstudeo\Rakshak\Rakshak;
10
use Thinkstudeo\Rakshak\RakshakSetting;
11
12
class VerifyTwoFactorOtp
13
{
14
    /**
15
     * Handle the incoming request.
16
     *
17
     * @param Request $request
0 ignored issues
show
The type Thinkstudeo\Rakshak\Middleware\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
18
     * @param Closure $next
19
     * @return void
20
     */
21
    public function handle($request, Closure $next)
22
    {
23
        if ($this->is2faEnabled()) {
24
            $user = Auth::user();
25
            if ($user->otp_expiry > Carbon::now()) {
0 ignored issues
show
Accessing otp_expiry on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
26
                return $next($request);
27
            }
28
29
            $user->otp_token = Rakshak::generateOtp();
0 ignored issues
show
Accessing otp_token on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
30
            $user->save();
31
32
            Rakshak::sendOtp($user);
33
34
            return redirect(Rakshak::verifyOtpPath());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(Thinkstu...kshak::verifyOtpPath()) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type void.
Loading history...
35
        }
36
37
        return $next($request);
38
    }
39
40
    /**
41
     * Determine whether the 2fa is enabled for the current user.
42
     *
43
     * @return bool
44
     */
45
    private function is2faEnabled()
46
    {
47
        $enabled = config('rakshak.enable_2fa');
48
        // $controlLevel = RakshakSetting::first()->control_level_2fa;
49
        $controlLevel = Cache::get('rakshak.control_level_2fa');
50
51
        if ($enabled && $controlLevel === 'admin') {
52
            return true;
53
        }
54
55
        if ($enabled && $controlLevel === 'user') {
56
            return Auth::user()->enable_2fa;
0 ignored issues
show
Accessing enable_2fa on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
57
        }
58
    }
59
}
60