Completed
Push — master ( 53cc59...4e85d3 )
by Antonio Carlos
02:16 queued 10s
created

Authenticator::canPassWithoutCheckingOTP()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 4
rs 10
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
     * 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 7
    public function __construct(IlluminateRequest $request)
36
    {
37 7
        parent::__construct($request);
38 7
    }
39
40
    /**
41
     * Authenticator boot.
42
     *
43
     * @param $request
44
     *
45
     * @return Google2FA
46
     */
47 7
    public function boot($request)
48
    {
49 7
        parent::boot($request);
50
51 7
        return $this;
52
    }
53
54
    /**
55
     * Authenticator boot for API usage.
56
     *
57
     * @param $request
58
     *
59
     * @return Google2FA
60
     */
61
    public function bootApi($request)
62
    {
63
        parent::boot($request);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (boot() instead of bootApi()). Are you sure this is correct? If so, you might want to change this to $this->boot().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
64
65
        $this->stateless = true;
66
67
        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
     * Check if the current use is authenticated via OTP.
108
     *
109
     * @return bool
110
     */
111 7
    public function isAuthenticated()
112
    {
113 7
        return $this->canPassWithoutCheckingOTP()
114 4
            ? true
115 7
            : $this->checkOTP();
116
    }
117
118
    /**
119
     * Check if it is already logged in or passable without checking for an OTP.
120
     *
121
     * @return bool
122
     */
123 7
    protected function canPassWithoutCheckingOTP()
124
    {
125
        return
126 7
            !$this->isEnabled() ||
127 7
            $this->noUserIsAuthenticated() ||
128 7
            !$this->isActivated() ||
129 7
            $this->twoFactorAuthStillValid();
130
    }
131
132
    /**
133
     * Check if the input OTP is valid.
134
     *
135
     * @return bool
136
     */
137 7
    protected function checkOTP()
138
    {
139 7
        if (!$this->inputHasOneTimePassword()) {
140 4
            return false;
141
        }
142
143 6
        if ($isValid = $this->verifyOneTimePassword()) {
144 4
            $this->login();
145
        }
146
147 5
        return $this->fireLoginEvent($isValid);
148
    }
149
150
    /**
151
     * Verify the OTP.
152
     *
153
     * @return mixed
154
     */
155 6
    protected function verifyOneTimePassword()
156
    {
157 6
        return $this->verifyAndStoreOneTimePassword($this->getOneTimePassword());
158
    }
159
}
160