Completed
Push — master ( 4e85d3...2a4bc0 )
by Antonio Carlos
02:15
created

Authenticator::getStateless()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Events\LoginFailed;
8
use PragmaRX\Google2FALaravel\Events\LoginSucceeded;
9
use PragmaRX\Google2FALaravel\Exceptions\InvalidOneTimePassword;
10
use PragmaRX\Google2FALaravel\Events\EmptyOneTimePasswordReceived;
11
12
class Authenticator extends Google2FA
13
{
14
    use ErrorBag, Input, Response;
15
16
    /**
17
     * The current password.
18
     *
19
     * @var
20
     */
21
    protected $password;
22
23
    /**
24
     * Flag to disable the session for API usage.
25
     *
26
     * @var
27
     */
28
    protected $stateless = false;
29
30
    /**
31
     * Authenticator constructor.
32
     *
33
     * @param \Illuminate\Http\Request $request
34
     */
35 8
    public function __construct(IlluminateRequest $request)
36
    {
37 8
        parent::__construct($request);
38 8
    }
39
40
    /**
41
     * Authenticator boot.
42
     *
43
     * @param $request
44
     *
45
     * @return Google2FA
46
     */
47 8
    public function boot($request)
48
    {
49 8
        parent::boot($request);
50
51 8
        return $this;
52
    }
53
54
    /**
55
     * Authenticator boot for API usage.
56
     *
57
     * @param $request
58
     *
59
     * @return Google2FA
60
     */
61 1
    public function bootStateless($request)
62
    {
63 1
        $this->boot($request);
64
65 1
        $this->setStateless();
66
67 1
        return $this;
68
    }
69
70
    /**
71
     * Fire login (success or failed).
72
     *
73
     * @param $succeeded
74
     */
75 5
    private function fireLoginEvent($succeeded)
76
    {
77 5
        event(
78 5
            $succeeded
79 4
                ? new LoginSucceeded($this->getUser())
80 5
                : new LoginFailed($this->getUser())
81
        );
82
83 5
        return $succeeded;
84
    }
85
86
    /**
87
     * Get the OTP from user input.
88
     *
89
     * @throws InvalidOneTimePassword
90
     *
91
     * @return mixed
92
     */
93 6
    protected function getOneTimePassword()
94
    {
95 6
        if (is_null($password = $this->getInputOneTimePassword()) || empty($password)) {
96 1
            event(new EmptyOneTimePasswordReceived());
97
98 1
            if ($this->config('throw_exceptions', true)) {
99 1
                throw new InvalidOneTimePassword('One Time Password cannot be empty.');
100
            }
101
        }
102
103 5
        return $password;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getStateless()
110
    {
111
        return $this->stateless;
112
    }
113
114
    /**
115
     * Check if the current use is authenticated via OTP.
116
     *
117
     * @return bool
118
     */
119 8
    public function isAuthenticated()
120
    {
121 8
        return $this->canPassWithoutCheckingOTP()
122 4
            ? true
123 8
            : $this->checkOTP();
124
    }
125
126
    /**
127
     * Check if it is already logged in or passable without checking for an OTP.
128
     *
129
     * @return bool
130
     */
131 8
    protected function canPassWithoutCheckingOTP()
132
    {
133
        return
134 8
            !$this->isEnabled() ||
135 8
            $this->noUserIsAuthenticated() ||
136 8
            !$this->isActivated() ||
137 8
            $this->twoFactorAuthStillValid();
138
    }
139
140
    /**
141
     * Check if the input OTP is valid.
142
     *
143
     * @return bool
144
     */
145 8
    protected function checkOTP()
146
    {
147 8
        if (!$this->inputHasOneTimePassword()) {
148 5
            return false;
149
        }
150
151 6
        if ($isValid = $this->verifyOneTimePassword()) {
152 4
            $this->login();
153
        }
154
155 5
        return $this->fireLoginEvent($isValid);
156
    }
157
158
    /**
159
     * @param mixed $stateless
160
     *
161
     * @return Authenticator
162
     */
163 1
    public function setStateless($stateless = true)
164
    {
165 1
        $this->stateless = $stateless;
166
167 1
        return $this;
168
    }
169
170
    /**
171
     * Verify the OTP.
172
     *
173
     * @return mixed
174
     * @throws InvalidOneTimePassword
175
     */
176 6
    protected function verifyOneTimePassword()
177
    {
178 6
        return $this->verifyAndStoreOneTimePassword($this->getOneTimePassword());
179
    }
180
}
181