Passed
Push — master ( 5a0b35...4eeecb )
by Ross
03:36
created

Verify::handleMissingCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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