Completed
Push — master ( 9a65be...fb506a )
by Antonio Carlos
02:32 queued 11s
created

Authenticator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 9
dl 0
loc 143
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

9 Methods

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