|
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\Model\Urls; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Framework\UrlInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Checker |
|
28
|
|
|
* |
|
29
|
|
|
* This is used as a way to check is a user is on a certain page. It provides semantic names for this and the ability |
|
30
|
|
|
* to check either customer or admin users |
|
31
|
|
|
*/ |
|
32
|
|
|
class Checker |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Fetcher |
|
36
|
|
|
*/ |
|
37
|
|
|
private $fetcher; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var UrlInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $url; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Checker constructor. |
|
45
|
|
|
* @param Fetcher $fetcher |
|
46
|
|
|
* @param UrlInterface $url |
|
47
|
|
|
*/ |
|
48
|
42 |
|
public function __construct(Fetcher $fetcher, UrlInterface $url) |
|
49
|
|
|
{ |
|
50
|
42 |
|
$this->fetcher = $fetcher; |
|
51
|
42 |
|
$this->url = $url; |
|
52
|
42 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Checks if the user is currently on the two factor authentication page. Can be used to check both customers and |
|
56
|
|
|
* admin users. |
|
57
|
|
|
* |
|
58
|
|
|
* @param bool $forAdmin - true to check for admin users, false to check for customers |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
24 |
|
public function areWeOnTheAuthenticationPage($forAdmin = false) |
|
62
|
|
|
{ |
|
63
|
24 |
|
$authenticationUrl = $this->fetcher->getAuthenticationUrl($forAdmin); |
|
64
|
|
|
|
|
65
|
24 |
|
return $this->compareUrls($this->getCurrentUrl(), $authenticationUrl); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Checks if the user is currently on the two factor verification page. Can be used to check both customers and |
|
70
|
|
|
* admin users. |
|
71
|
|
|
* |
|
72
|
|
|
* @param bool $forAdmin - true to check for admin users, false to check for customers |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
20 |
|
public function areWeOnTheVerificationPage($forAdmin = false) |
|
76
|
|
|
{ |
|
77
|
20 |
|
$verificationUrl = $this->fetcher->getVerificationUrl($forAdmin); |
|
78
|
|
|
|
|
79
|
20 |
|
return $this->compareUrls($this->getCurrentUrl(), $verificationUrl); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the current URL |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
24 |
|
private function getCurrentUrl() |
|
88
|
|
|
{ |
|
89
|
24 |
|
return $this->url->getCurrentUrl(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Compares two URLs. |
|
94
|
|
|
* |
|
95
|
|
|
* @param $firstUrl |
|
96
|
|
|
* @param $secondUrl |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
24 |
|
private function compareUrls($firstUrl, $secondUrl) |
|
100
|
|
|
{ |
|
101
|
24 |
|
return ($this->cleanUrl($firstUrl) === $this->cleanUrl($secondUrl)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* This is used normalise the URLs to a known structure and remove any trailing characters that could prevent them |
|
106
|
|
|
* from getting checked correctly. |
|
107
|
|
|
* |
|
108
|
|
|
* For these checks I'm not that conceded if the customer is using http or https so we strip that out. Equally I'm |
|
109
|
|
|
* not worried about ports, or query params so they are removed as well |
|
110
|
|
|
* |
|
111
|
|
|
* @param $url |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
24 |
|
private function cleanUrl($url) |
|
115
|
|
|
{ |
|
116
|
24 |
|
$parts = parse_url($url); |
|
|
|
|
|
|
117
|
24 |
|
$cleanUrl = $parts['host'] . '/' . $parts['path']; |
|
118
|
24 |
|
$noRewriteUrl = str_replace('/index.php', '', $cleanUrl); |
|
119
|
|
|
|
|
120
|
24 |
|
return trim($noRewriteUrl, '\t\n\r/'); |
|
121
|
|
|
} |
|
122
|
|
|
} |