Completed
Push — master ( e8eaa8...b17f45 )
by Antonio Carlos
06:15
created

Authenticator::storeAuthPassed()   A

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
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace PragmaRX\Google2FALaravel\Support;
4
5
use PragmaRX\Google2FALaravel\Google2FA;
6
use Illuminate\Http\Request as IlluminateRequest;
7
use PragmaRX\Google2FALaravel\Exceptions\InvalidOneTimePassword;
8
9
class Authenticator extends Google2FA
10
{
11
    use ErrorBag, Input, Response;
12
13
    /**
14
     * The current password.
15
     *
16
     * @var
17
     */
18
    protected $password;
19
20
    /**
21
     * Authenticator constructor.
22
     *
23
     * @param \Illuminate\Http\Request $request
24
     */
25
    public function __construct(IlluminateRequest $request)
26
    {
27
        parent::__construct($request);
28
    }
29
30
    /**
31
     * Authenticator boot.
32
     *
33
     * @param $request
34
     *
35
     * @return Google2FA
36
     */
37
    public function boot($request)
38
    {
39
        parent::boot($request);
40
41
        return $this;
42
    }
43
44
    /**
45
     * Get the OTP from user input.
46
     *
47
     * @throws InvalidOneTimePassword
48
     *
49
     * @return mixed
50
     */
51
    protected function getOneTimePassword()
52
    {
53
        if (!is_null($this->password)) {
54
            return $this->password;
55
        }
56
57
        $this->password = $this->getInputOneTimePassword();
58
59
        if (is_null($this->password) || empty($this->password)) {
60
            throw new InvalidOneTimePassword('One Time Password cannot be empty.');
61
        }
62
63
        return $this->password;
64
    }
65
66
    /**
67
     * Check if the current use is authenticated via OTP.
68
     *
69
     * @return bool
70
     */
71
    public function isAuthenticated()
72
    {
73
        return $this->canPassWithoutCheckingOTP()
74
            ? true
75
            : $this->checkOTP();
76
    }
77
78
79
    /**
80
     * Check if it is already logged in or passable without checking for an OTP.
81
     *
82
     * @return bool
83
     */
84
    protected function canPassWithoutCheckingOTP()
85
    {
86
        return
87
            !$this->isEnabled() ||
88
            $this->noUserIsAuthenticated() ||
89
            !$this->isActivated() ||
90
            $this->twoFactorAuthStillValid();
91
    }
92
93
    /**
94
     * Check if the input OTP is valid.
95
     *
96
     * @return bool
97
     */
98
    protected function checkOTP()
99
    {
100
        if (!$this->inputHasOneTimePassword()) {
101
            return false;
102
        }
103
104
        if ($isValid = $this->verifyOneTimePassword()) {
105
            $this->login();
106
        }
107
108
        return $isValid;
109
    }
110
111
    /**
112
     * Verify the OTP.
113
     *
114
     * @return mixed
115
     */
116
    protected function verifyOneTimePassword()
117
    {
118
        return $this->verifyAndStoreOneTimePassword($this->getOneTimePassword());
119
    }
120
}
121