Completed
Pull Request — master (#18)
by
unknown
11:55
created

AuthenticatorController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 5
dl 0
loc 96
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A boot() 0 5 1
A getOneTimePassword() 0 14 4
A isAuthenticated() 0 6 2
A checkOTP() 0 12 3
A verifyOneTimePassword() 0 4 1
1
<?php
2
3
namespace PragmaRX\Google2FALaravel\Support;
4
5
use Carbon\Carbon;
6
use Google2FA;
7
use Illuminate\Http\Request as IlluminateRequest;
8
use PragmaRX\Google2FA\Support\Constants as Google2FAConstants;
9
use PragmaRX\Google2FALaravel\Exceptions\InvalidOneTimePassword;
10
use PragmaRX\Google2FALaravel\Exceptions\InvalidSecretKey;
11
12
class AuthenticatorController extends Authenticator
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 IlluminateRequest $request
27
     */
28
    public function __construct(IlluminateRequest $request)
29
    {
30
        parent::__construct($request);
31
    }
32
33
    /**
34
     * Authenticator boot.
35
     *
36
     * @param $request
37
     *
38
     * @return Authenticator
39
     */
40
    public function boot($request)
41
    {
42
        parent::boot($request);
43
        return $this;
44
    }
45
46
    /**
47
     * Get the OTP from user input.
48
     *
49
     * @throws InvalidOneTimePassword
50
     *
51
     * @return mixed
52
     */
53
    protected function getOneTimePassword()
54
    {
55
        if (!is_null($this->password)) {
56
            return $this->password;
57
        }
58
59
        $this->password = $this->getInputOneTimePassword();
60
61
        if (is_null($this->password) || empty($this->password)) {
62
            throw new InvalidOneTimePassword('One Time Password cannot be empty.');
63
        }
64
65
        return $this->password;
66
    }
67
68
    /**
69
     * Check if the current use is authenticated via OTP.
70
     *
71
     * @return bool
72
     */
73
    public function isAuthenticated()
74
    {
75
        return $this->canPassWithoutCheckingOTP()
76
            ? true
77
            : $this->checkOTP();
78
    }
79
80
    /**
81
     * Check if the input OTP is valid.
82
     *
83
     * @return bool
84
     */
85
    protected function checkOTP()
86
    {
87
        if (!$this->inputHasOneTimePassword()) {
88
            return false;
89
        }
90
91
        if ($isValid = $this->verifyOneTimePassword()) {
92
            $this->login();
93
        }
94
95
        return $isValid;
96
    }
97
98
    /**
99
     * Verify the OTP.
100
     *
101
     * @return mixed
102
     */
103
    protected function verifyOneTimePassword()
104
    {
105
        return $this->verifyAndStoreOneTimePassord($this->getOneTimePassword());
106
    }
107
}
108