Completed
Pull Request — master (#86)
by
unknown
03:08
created

Authenticator::checkOTP()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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