Passed
Push — master ( 64a1db...f9577e )
by Ross
02:54
created

Verify::addErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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\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\TwoFactorUrls;
33
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
34
35
class Verify extends Action
36
{
37
    /**
38
     * @var AdminUser
39
     */
40
    private $adminUser;
41
    /**
42
     * @var TwoFactorSecret
43
     */
44
    private $twoFactorSecret;
45
    /**
46
     * @var GoogleVerify
47
     */
48
    private $verify;
49
    /**
50
     * @var IsVerified
51
     */
52
    private $isVerified;
53
    /**
54
     * @var Session
55
     */
56
    private $adminSession;
57
    /**
58
     * @var TwoFactorUrls
59
     */
60
    private $twoFactorUrls;
61
62
    /**
63
     * Verify constructor.
64
     *
65
     * @param Context         $context
66
     * @param AdminUser       $adminUser
67
     * @param TwoFactorSecret $twoFactorSecret
68
     * @param GoogleVerify    $verify
69
     * @param IsVerified      $isVerified
70
     * @param Session         $adminSession
71
     * @param TwoFactorUrls   $twoFactorUrls
72
     */
73
    public function __construct(
74
        Context $context,
75
        AdminUser $adminUser,
76
        TwoFactorSecret $twoFactorSecret,
77
        GoogleVerify $verify,
78
        IsVerified $isVerified,
79
        Session $adminSession,
80
        TwoFactorUrls $twoFactorUrls
81
    ) {
82
        parent::__construct($context);
83
        $this->adminUser       = $adminUser;
84
        $this->twoFactorSecret = $twoFactorSecret;
85
        $this->verify          = $verify;
86
        $this->isVerified      = $isVerified;
87
        $this->adminSession = $adminSession;
88
        $this->twoFactorUrls = $twoFactorUrls;
89
    }
90
91
    /**
92
     * Dispatch request
93
     *
94
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
95
     * @throws \Magento\Framework\Exception\NotFoundException
96
     */
97 View Code Duplication
    public function execute()
98
    {
99
        $secret    = $this->getRequest()->getParam('secret');
100
        $adminUser = $this->adminUser->getAdminUser();
101
102
        $verificationPassed = $this->verifySecret($adminUser, $secret);
103
104
        if ($verificationPassed === false) {
105
            return $this->handleError();
106
        }
107
108
        return $this->handleSuccess();
109
    }
110
111
    private function verifySecret($adminUser, $postedSecret)
112
    {
113
        $customerSecret = $this->twoFactorSecret->getValue($adminUser);
114
        try {
115
            $verified = $this->verify->verify($customerSecret, $postedSecret);
116
        } catch (InvalidCharactersException $exception) {
117
            $verified = false;
118
        }
119
120
        return $verified;
121
    }
122
123
    private function handleError()
124
    {
125
        $this->isVerified->removeIsVerified($this->adminSession);
126
        $this->addErrorMessage();
127
        $authenticateUrl = $this->twoFactorUrls->getAuthenticationUrl(true);
128
129
        return $this->redirect($authenticateUrl);
130
    }
131
132
    private function addErrorMessage()
133
    {
134
        $this->messageManager->addErrorMessage("Two Factor Code was incorrect");
135
    }
136
137
    private function handleSuccess()
138
    {
139
        $this->isVerified->setIsVerified($this->adminSession);
140
        $accountUrl = $this->twoFactorUrls->getAdminDashboardUrl();
141
142
        return $this->redirect($accountUrl);
143
    }
144
145
    private function redirect($path)
146
    {
147
        $redirect = $this->resultRedirectFactory->create();
148
        $redirect->setPath($path);
149
150
        return $redirect;
151
    }
152
153
    public function _isAllowed()
154
    {
155
        return true;
156
    }
157
}
158