Completed
Pull Request — master (#18)
by
unknown
20:30 queued 06:19
created

AuthenticatorController::verifyOneTimePassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace PragmaRX\Google2FALaravel\Support;
4
5
use Illuminate\Http\Request as IlluminateRequest;
6
use PragmaRX\Google2FALaravel\Exceptions\InvalidOneTimePassword;
7
8
class AuthenticatorController extends Authenticator
9
{
10
    use ErrorBag, Input, Response;
11
12
    /**
13
     * The current password.
14
     *
15
     * @var
16
     */
17
    protected $password;
18
19
    /**
20
     * Authenticator constructor.
21
     *
22
     * @param IlluminateRequest $request
23
     */
24
    public function __construct(IlluminateRequest $request)
25
    {
26
        parent::__construct($request);
27
    }
28
29
    /**
30
     * Authenticator boot.
31
     *
32
     * @param $request
33
     *
34
     * @return Authenticator
35
     */
36
    public function boot($request)
37
    {
38
        parent::boot($request);
39
40
        return $this;
41
    }
42
43
    /**
44
     * Get the OTP from user input.
45
     *
46
     * @throws InvalidOneTimePassword
47
     *
48
     * @return mixed
49
     */
50
    protected function getOneTimePassword()
51
    {
52
        if (!is_null($this->password)) {
53
            return $this->password;
54
        }
55
56
        $this->password = $this->getInputOneTimePassword();
57
58
        if (is_null($this->password) || empty($this->password)) {
59
            throw new InvalidOneTimePassword('One Time Password cannot be empty.');
60
        }
61
62
        return $this->password;
63
    }
64
65
    /**
66
     * Check if the current use is authenticated via OTP.
67
     *
68
     * @return bool
69
     */
70
    public function isAuthenticated()
71
    {
72
        return $this->canPassWithoutCheckingOTP()
73
            ? true
74
            : $this->checkOTP();
75
    }
76
77
    /**
78
     * Check if the input OTP is valid.
79
     *
80
     * @return bool
81
     */
82
    protected function checkOTP()
83
    {
84
        if (!$this->inputHasOneTimePassword()) {
85
            return false;
86
        }
87
88
        if ($isValid = $this->verifyOneTimePassword()) {
89
            $this->login();
90
        }
91
92
        return $isValid;
93
    }
94
95
    /**
96
     * Verify the OTP.
97
     *
98
     * @return mixed
99
     */
100
    protected function verifyOneTimePassword()
101
    {
102
        return $this->verifyAndStoreOneTimePassord($this->getOneTimePassword());
103
    }
104
}
105