|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace SURFnet\VPN\Common\Http; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Common\TplInterface; |
|
21
|
|
|
use SURFnet\VPN\Common\Http\Exception\HttpException; |
|
22
|
|
|
use SURFnet\VPN\Common\HttpClient\ServerClient; |
|
23
|
|
|
|
|
24
|
|
|
class TwoFactorModule implements ServiceModuleInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var \SURFnet\VPN\Common\HttpClient\ServerClient */ |
|
27
|
|
|
private $serverClient; |
|
28
|
|
|
|
|
29
|
|
|
/** @var SessionInterface */ |
|
30
|
|
|
private $session; |
|
31
|
|
|
|
|
32
|
|
|
/** @var \SURFnet\VPN\Common\TplInterface */ |
|
33
|
|
|
private $tpl; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(ServerClient $serverClient, SessionInterface $session, TplInterface $tpl) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->serverClient = $serverClient; |
|
38
|
|
|
$this->session = $session; |
|
39
|
|
|
$this->tpl = $tpl; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function init(Service $service) |
|
43
|
|
|
{ |
|
44
|
|
|
$service->post( |
|
45
|
|
|
'/_two_factor/auth/verify', |
|
46
|
|
|
function (Request $request, array $hookData) { |
|
47
|
|
|
$userId = $hookData['auth']; |
|
48
|
|
|
|
|
49
|
|
|
$this->session->delete('_two_factor_verified'); |
|
50
|
|
|
|
|
51
|
|
|
$otpKey = $request->getPostParameter('otpKey'); |
|
52
|
|
|
$redirectTo = $request->getPostParameter('_two_factor_auth_redirect_to'); |
|
53
|
|
|
|
|
54
|
|
|
// validate OTP key |
|
55
|
|
|
if (0 === preg_match('/^[0-9]{6}$/', $otpKey)) { |
|
56
|
|
|
throw new HttpException('invalid OTP key format', 400); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// validate the URL |
|
60
|
|
View Code Duplication |
if (false === filter_var($redirectTo, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_PATH_REQUIRED)) { |
|
|
|
|
|
|
61
|
|
|
throw new HttpException('invalid redirect_to URL', 400); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// extract the "host" part of the URL |
|
65
|
|
View Code Duplication |
if (false === $redirectToHost = parse_url($redirectTo, PHP_URL_HOST)) { |
|
|
|
|
|
|
66
|
|
|
throw new HttpException('invalid redirect_to URL, unable to extract host', 400); |
|
67
|
|
|
} |
|
68
|
|
|
if ($request->getServerName() !== $redirectToHost) { |
|
69
|
|
|
throw new HttpException('redirect_to does not match expected host', 400); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($this->serverClient->verifyOtpKey($userId, $otpKey)) { |
|
73
|
|
|
$this->session->set('_two_factor_verified', true); |
|
74
|
|
|
|
|
75
|
|
|
return new RedirectResponse($redirectTo, 302); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// invalid otp |
|
79
|
|
|
$response = new Response(200, 'text/html'); |
|
80
|
|
|
$response->setBody( |
|
81
|
|
|
$this->tpl->render( |
|
82
|
|
|
'twoFactorAuthentication', |
|
83
|
|
|
[ |
|
84
|
|
|
'_two_factor_auth_invalid_key' => true, |
|
85
|
|
|
'_two_factor_auth_redirect_to' => $redirectTo, |
|
86
|
|
|
] |
|
87
|
|
|
) |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
return $response; |
|
91
|
|
|
} |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.