Completed
Push — master ( 1fef13...0df3bd )
by Antonio Carlos
01:20
created

Authenticator::getOneTimePassword()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 4
rs 9.8666
c 0
b 0
f 0
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;
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 7
    public function __construct(IlluminateRequest $request)
29
    {
30 7
        parent::__construct($request);
31 7
    }
32
33
    /**
34
     * Authenticator boot.
35
     *
36
     * @param $request
37
     *
38
     * @return Google2FA
39
     */
40 7
    public function boot($request)
41
    {
42 7
        parent::boot($request);
43
44 7
        return $this;
45
    }
46
47
    /**
48
     * Fire login (success or failed).
49
     *
50
     * @param $succeeded
51
     */
52 5
    private function fireLoginEvent($succeeded)
53
    {
54 5
        event(
55 5
            $succeeded
56 4
                ? new LoginSucceeded($this->getUser())
57 5
                : new LoginFailed($this->getUser())
58
        );
59
60 5
        return $succeeded;
61
    }
62
63
    /**
64
     * Get the OTP from user input.
65
     *
66
     * @throws InvalidOneTimePassword
67
     *
68
     * @return mixed
69
     */
70 6
    protected function getOneTimePassword()
71
    {
72 6
        if (is_null($password = $this->getInputOneTimePassword()) || empty($password)) {
73 1
            event(new EmptyOneTimePasswordReceived());
74
75 1
            if ($this->config('throw_exceptions', true)) {
76 1
                throw new InvalidOneTimePassword('One Time Password cannot be empty.');
77
            }
78
        }
79
80 5
        return $password;
81
    }
82
83
    /**
84
     * Check if the current use is authenticated via OTP.
85
     *
86
     * @return bool
87
     */
88 7
    public function isAuthenticated()
89
    {
90 7
        return $this->canPassWithoutCheckingOTP()
91 4
            ? true
92 7
            : $this->checkOTP();
93
    }
94
95
    /**
96
     * Check if it is already logged in or passable without checking for an OTP.
97
     *
98
     * @return bool
99
     */
100 7
    protected function canPassWithoutCheckingOTP()
101
    {
102
        return
103 7
            !$this->isEnabled() ||
104 7
            $this->noUserIsAuthenticated() ||
105 7
            !$this->isActivated() ||
106 7
            $this->twoFactorAuthStillValid();
107
    }
108
109
    /**
110
     * Check if the input OTP is valid.
111
     *
112
     * @return bool
113
     */
114 7
    protected function checkOTP()
115
    {
116 7
        if (!$this->inputHasOneTimePassword()) {
117 4
            return false;
118
        }
119
120 6
        if ($isValid = $this->verifyOneTimePassword()) {
121 4
            $this->login();
122
        }
123
124 5
        return $this->fireLoginEvent($isValid);
125
    }
126
127
    /**
128
     * Verify the OTP.
129
     *
130
     * @return mixed
131
     */
132 6
    protected function verifyOneTimePassword()
133
    {
134 6
        return $this->verifyAndStoreOneTimePassword($this->getOneTimePassword());
135
    }
136
}
137