|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Common\Http; |
|
11
|
|
|
|
|
12
|
|
|
use fkooman\SeCookie\SessionInterface; |
|
13
|
|
|
use SURFnet\VPN\Common\Http\Exception\HttpException; |
|
14
|
|
|
use SURFnet\VPN\Common\TplInterface; |
|
15
|
|
|
|
|
16
|
|
|
class FormAuthenticationModule implements ServiceModuleInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
private $userPass; |
|
20
|
|
|
|
|
21
|
|
|
/** @var \fkooman\SeCookie\SessionInterface; */ |
|
22
|
|
|
private $session; |
|
23
|
|
|
|
|
24
|
|
|
/** @var \SURFnet\VPN\Common\TplInterface */ |
|
25
|
|
|
private $tpl; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(array $userPass, SessionInterface $session, TplInterface $tpl) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->userPass = $userPass; |
|
30
|
|
|
$this->session = $session; |
|
31
|
|
|
$this->tpl = $tpl; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function init(Service $service) |
|
35
|
|
|
{ |
|
36
|
|
|
$service->post( |
|
37
|
|
|
'/_form/auth/verify', |
|
38
|
|
|
function (Request $request) { |
|
39
|
|
|
$this->session->delete('_form_auth_user'); |
|
40
|
|
|
|
|
41
|
|
|
$authUser = $request->getPostParameter('userName'); |
|
42
|
|
|
$authPass = $request->getPostParameter('userPass'); |
|
43
|
|
|
$redirectTo = $request->getPostParameter('_form_auth_redirect_to'); |
|
44
|
|
|
|
|
45
|
|
|
// validate the URL |
|
46
|
|
View Code Duplication |
if (false === filter_var($redirectTo, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_PATH_REQUIRED)) { |
|
|
|
|
|
|
47
|
|
|
throw new HttpException('invalid redirect_to URL', 400); |
|
48
|
|
|
} |
|
49
|
|
|
// extract the "host" part of the URL |
|
50
|
|
View Code Duplication |
if (false === $redirectToHost = parse_url($redirectTo, PHP_URL_HOST)) { |
|
|
|
|
|
|
51
|
|
|
throw new HttpException('invalid redirect_to URL, unable to extract host', 400); |
|
52
|
|
|
} |
|
53
|
|
|
if ($request->getServerName() !== $redirectToHost) { |
|
54
|
|
|
throw new HttpException('redirect_to does not match expected host', 400); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (array_key_exists($authUser, $this->userPass)) { |
|
58
|
|
|
if (false !== password_verify($authPass, $this->userPass[$authUser])) { |
|
59
|
|
|
$this->session->regenerate(true); |
|
60
|
|
|
$this->session->set('_form_auth_user', $authUser); |
|
61
|
|
|
|
|
62
|
|
|
return new RedirectResponse($redirectTo, 302); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// invalid authentication |
|
67
|
|
|
$response = new Response(200, 'text/html'); |
|
68
|
|
|
$response->setBody( |
|
69
|
|
|
$this->tpl->render( |
|
70
|
|
|
'formAuthentication', |
|
71
|
|
|
[ |
|
72
|
|
|
'_form_auth_invalid_credentials' => true, |
|
73
|
|
|
'_form_auth_invalid_credentials_user' => $authUser, |
|
74
|
|
|
'_form_auth_redirect_to' => $redirectTo, |
|
75
|
|
|
'_form_auth_login_page' => true, |
|
76
|
|
|
] |
|
77
|
|
|
) |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
return $response; |
|
81
|
|
|
} |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
$service->post( |
|
85
|
|
|
'/_form/auth/logout', |
|
86
|
|
|
function (Request $request) { |
|
87
|
|
|
// delete authentication information |
|
88
|
|
|
$this->session->delete('_form_auth_user'); |
|
89
|
|
|
$this->session->delete('_two_factor_verified'); |
|
90
|
|
|
$this->session->regenerate(true); |
|
91
|
|
|
|
|
92
|
|
|
return new RedirectResponse($request->getHeader('HTTP_REFERER')); |
|
93
|
|
|
} |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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.