Verify::handleError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
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\Customerlogin;
23
24
use Magento\Customer\Api\Data\CustomerInterface;
25
use Magento\Framework\App\Action\Context;
26
use Magento\Framework\App\ResponseInterface;
27
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
28
use Rossmitchell\Twofactor\Model\Config\Customer as CustomerAdmin;
29
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor;
30
use Rossmitchell\Twofactor\Model\Customer\Attribute\TwoFactorSecret;
31
use Rossmitchell\Twofactor\Model\Customer\Customer;
32
use Rossmitchell\Twofactor\Model\Customer\Session;
33
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\Verify as GoogleVerify;
34
use Rossmitchell\Twofactor\Model\Urls\Fetcher;
35
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
36
37
class Verify extends AbstractController
38
{
39
40
    /**
41
     * @var TwoFactorSecret
42
     */
43
    private $secret;
44
    /**
45
     * @var GoogleVerify
46
     */
47
    private $verify;
48
    /**
49
     * @var IsVerified
50
     */
51
    private $isVerified;
52
    /**
53
     * @var Session
54
     */
55
    private $customerSession;
56
57
    /**
58
     * Constructor
59
     *
60
     * @param Context          $context
61
     * @param Customer         $customerGetter
62
     * @param TwoFactorSecret  $secret
63
     * @param GoogleVerify     $verify
64
     * @param Fetcher          $fetcher
65
     * @param IsVerified       $isVerified
66
     * @param Session          $customerSession
67
     * @param CustomerAdmin    $customerAdmin
68
     * @param IsUsingTwoFactor $isUsingTwoFactor
69
     */
70 4
    public function __construct(
71
        Context $context,
72
        Customer $customerGetter,
73
        TwoFactorSecret $secret,
74
        GoogleVerify $verify,
75
        Fetcher $fetcher,
76
        IsVerified $isVerified,
77
        Session $customerSession,
78
        CustomerAdmin $customerAdmin,
79
        IsUsingTwoFactor $isUsingTwoFactor
80
    ) {
81 4
        parent::__construct($context, $customerAdmin, $customerGetter, $fetcher, $isUsingTwoFactor);
82 4
        $this->secret          = $secret;
83 4
        $this->verify          = $verify;
84 4
        $this->isVerified      = $isVerified;
85 4
        $this->customerSession = $customerSession;
86 4
    }
87
88
    /**
89
     * Dispatch request
90
     *
91
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
92
     * @throws \Magento\Framework\Exception\NotFoundException
93
     */
94 4 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...
95
    {
96 4
        if ($this->shouldActionBeRun() === false) {
97 2
            return $this->getRedirectAction();
98
        }
99
100 2
        $secret   = $this->getRequest()->getParam('secret');
101 2
        $customer = $this->getCustomer();
102 2
        if ($customer === false) {
103
            return $this->handleError();
104
        }
105 2
        $verificationPassed = $this->verifySecret($customer, $secret);
106
107 2
        if ($verificationPassed === false) {
108 1
            return $this->handleError();
109
        }
110
111 1
        return $this->handleSuccess();
112
    }
113
114 2
    private function verifySecret(CustomerInterface $customer, $postedSecret)
115
    {
116 2
        $customerSecret = $this->secret->getValue($customer);
117
        try {
118 2
            $verified = $this->verify->verify($customerSecret, $postedSecret);
119
        } catch (InvalidCharactersException $exception) {
120
            $verified = false;
121
        }
122
123 2
        return $verified;
124
    }
125
126 1
    private function handleSuccess()
127
    {
128 1
        $this->isVerified->setIsVerified($this->customerSession);
129 1
        $this->addSuccessMessage();
130 1
        $accountUrl = $this->getUrlFetcher()->getCustomerAccountUrl();
131
132 1
        return $this->redirect($accountUrl);
133
    }
134
135 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...
136
    {
137 1
        $this->isVerified->removeIsVerified($this->customerSession);
138 1
        $this->addErrorMessage();
139 1
        $authenticateUrl = $this->getUrlFetcher()->getAuthenticationUrl();
140
141 1
        return $this->redirect($authenticateUrl);
142
    }
143
144 1
    private function addErrorMessage()
145
    {
146 1
        $this->messageManager->addErrorMessage("Two Factor Code was incorrect");
147 1
    }
148
149 1
    private function addSuccessMessage()
150
    {
151 1
        $this->messageManager->addSuccessMessage("Two Factor Code was correct");
152 1
    }
153
}
154