Passed
Push — master ( 9e76ce...d12a36 )
by Ross
22:08
created

Verify::verifySecret()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * A two factor authentication module that protects both the admin and customer logins
4
 * Copyright (C) 2017  Ross Mitchell
5
 *
6
 * This file is part of Rossmitchell/Twofactor.
7
 *
8
 * Rossmitchell/Twofactor is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace Rossmitchell\Twofactor\Controller\Adminhtml\Adminlogin;
23
24
use Magento\Backend\App\Action;
25
use Magento\Backend\App\Action\Context;
26
use Magento\Framework\App\ResponseInterface;
27
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
28
use Rossmitchell\Twofactor\Model\Admin\AdminUser;
29
use Rossmitchell\Twofactor\Model\Admin\Attribute\TwoFactorSecret;
30
use Rossmitchell\Twofactor\Model\Admin\Session;
31
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\Verify as GoogleVerify;
32
use Rossmitchell\Twofactor\Model\Urls\Fetcher;
33
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
34
use Rossmitchell\Twofactor\Model\Config\Admin as UserAdmin;
35
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor;
36
37
class Verify extends AbstractController
38
{
39
    /**
40
     * @var TwoFactorSecret
41
     */
42
    private $twoFactorSecret;
43
    /**
44
     * @var GoogleVerify
45
     */
46
    private $verify;
47
    /**
48
     * @var IsVerified
49
     */
50
    private $isVerified;
51
    /**
52
     * @var Session
53
     */
54
    private $adminSession;
55
    /**
56
     * @var Fetcher
57
     */
58
    private $fetcher;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
59
60
    /**
61
     * Verify constructor.
62
     *
63
     * @param Context          $context
64
     * @param UserAdmin        $userAdmin
65
     * @param AdminUser        $adminGetter
66
     * @param Fetcher          $fetcher
67
     * @param IsUsingTwoFactor $isUsingTwoFactor
68
     * @param TwoFactorSecret  $twoFactorSecret
69
     * @param GoogleVerify     $verify
70
     * @param IsVerified       $isVerified
71
     * @param Session          $adminSession
72
     * @param Fetcher          $fetcher
73
     */
74 3 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
        Context $context,
76
        UserAdmin $userAdmin,
77
        AdminUser $adminGetter,
78
        IsUsingTwoFactor $isUsingTwoFactor,
79
        TwoFactorSecret $twoFactorSecret,
80
        GoogleVerify $verify,
81
        IsVerified $isVerified,
82
        Session $adminSession,
83
        Fetcher $fetcher
84
    ) {
85 3
        parent::__construct($context, $userAdmin, $adminGetter, $fetcher, $isUsingTwoFactor);
86 3
        $this->twoFactorSecret = $twoFactorSecret;
87 3
        $this->verify          = $verify;
88 3
        $this->isVerified      = $isVerified;
89 3
        $this->adminSession    = $adminSession;
90 3
        $this->fetcher         = $fetcher;
91 3
    }
92
93
    /**
94
     * Dispatch request
95
     *
96
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
97
     * @throws \Magento\Framework\Exception\NotFoundException
98
     */
99 3 View Code Duplication
    public function execute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101 3
        if ($this->shouldActionBeRun() === false) {
102 1
            return $this->getRedirectAction();
103
        }
104
105 2
        $secret    = $this->getRequest()->getParam('secret');
106 2
        $adminUser = $this->getAdminUser();
107
108 2
        $verificationPassed = $this->verifySecret($adminUser, $secret);
109
110 2
        if ($verificationPassed === false) {
111 1
            return $this->handleError();
112
        }
113
114 1
        return $this->handleSuccess();
115
    }
116
117 2
    private function verifySecret($adminUser, $postedSecret)
118
    {
119 2
        $customerSecret = $this->twoFactorSecret->getValue($adminUser);
120
        try {
121 2
            $verified = $this->verify->verify($customerSecret, $postedSecret);
122
        } catch (InvalidCharactersException $exception) {
123
            $verified = false;
124
        }
125
126 2
        return $verified;
127
    }
128
129 1 View Code Duplication
    private function handleError()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131 1
        $this->isVerified->removeIsVerified($this->adminSession);
132 1
        $this->addErrorMessage();
133 1
        $authenticateUrl = $this->fetcher->getAuthenticationUrl(true);
134
135 1
        return $this->redirect($authenticateUrl);
136
    }
137
138 1
    private function addErrorMessage()
139
    {
140 1
        $this->messageManager->addErrorMessage("Two Factor Code was incorrect");
141 1
    }
142
143 1
    private function handleSuccess()
144
    {
145 1
        $this->isVerified->setIsVerified($this->adminSession);
146 1
        $accountUrl = $this->fetcher->getAdminDashboardUrl();
147
148 1
        return $this->redirect($accountUrl);
149
    }
150
151
    public function _isAllowed()
0 ignored issues
show
introduced by
The use of public non-interface method in ACTION is discouraged.
Loading history...
152
    {
153
        return true;
154
    }
155
}
156