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\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 Fetcher
50
     */
51
    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...
52
    /**
53
     * @var IsVerified
54
     */
55
    private $isVerified;
56
    /**
57
     * @var Session
58
     */
59
    private $customerSession;
60
61
    /**
62
     * Constructor
63
     *
64
     * @param Context $context
65
     * @param Customer $customerGetter
66
     * @param TwoFactorSecret $secret
67
     * @param GoogleVerify $verify
68
     * @param Fetcher $fetcher
69
     * @param IsVerified $isVerified
70
     * @param Session $customerSession
71
     * @param CustomerAdmin $customerAdmin
72
     * @param IsUsingTwoFactor $isUsingTwoFactor
73
     */
74 4 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
        Customer $customerGetter,
77
        TwoFactorSecret $secret,
78
        GoogleVerify $verify,
79
        Fetcher $fetcher,
80
        IsVerified $isVerified,
81
        Session $customerSession,
82
        CustomerAdmin $customerAdmin,
83
        IsUsingTwoFactor $isUsingTwoFactor
84
    ) {
85 4
        parent::__construct($context, $customerAdmin, $customerGetter, $fetcher, $isUsingTwoFactor);
86 4
        $this->secret          = $secret;
87 4
        $this->verify          = $verify;
88 4
        $this->fetcher         = $fetcher;
89 4
        $this->isVerified      = $isVerified;
90 4
        $this->customerSession = $customerSession;
91 4
    }
92
93
    /**
94
     * Dispatch request
95
     *
96
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
97
     * @throws \Magento\Framework\Exception\NotFoundException
98
     */
99 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...
100
    {
101 4
        if ($this->shouldActionBeRun() === false) {
102 2
            return $this->getRedirectAction();
103
        }
104
105 2
        $secret   = $this->getRequest()->getParam('secret');
106 2
        $customer = $this->getCustomer();
107 2
        $verificationPassed = $this->verifySecret($customer, $secret);
108
109 2
        if ($verificationPassed === false) {
110 1
            return $this->handleError();
111
        }
112
113 1
        return $this->handleSuccess();
114
    }
115
116 2
    private function verifySecret(CustomerInterface $customer, $postedSecret)
117
    {
118 2
        $customerSecret = $this->secret->getValue($customer);
119
        try {
120 2
            $verified = $this->verify->verify($customerSecret, $postedSecret);
121
        } catch (InvalidCharactersException $exception) {
122
            $verified = false;
123
        }
124
125 2
        return $verified;
126
    }
127
128 1
    private function handleSuccess()
129
    {
130 1
        $this->isVerified->setIsVerified($this->customerSession);
131 1
        $this->addSuccessMessage();
132 1
        $accountUrl = $this->fetcher->getCustomerAccountUrl();
133
134 1
        return $this->redirect($accountUrl);
135
    }
136
137 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...
138
    {
139 1
        $this->isVerified->removeIsVerified($this->customerSession);
140 1
        $this->addErrorMessage();
141 1
        $authenticateUrl = $this->fetcher->getAuthenticationUrl();
142
143 1
        return $this->redirect($authenticateUrl);
144
    }
145
146 1
    private function addErrorMessage()
147
    {
148 1
        $this->messageManager->addErrorMessage("Two Factor Code was incorrect");
149 1
    }
150
151 1
    private function addSuccessMessage()
152
    {
153 1
        $this->messageManager->addSuccessMessage("Two Factor Code was correct");
154 1
    }
155
}
156