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

AuthenticatorController::getOneTimePassword()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 0
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