|
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\TplInterface; |
|
14
|
|
|
|
|
15
|
|
|
class FormAuthenticationHook implements BeforeHookInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var \fkooman\SeCookie\SessionInterface */ |
|
18
|
|
|
private $session; |
|
19
|
|
|
|
|
20
|
|
|
/** @var \SURFnet\VPN\Common\TplInterface */ |
|
21
|
|
|
private $tpl; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(SessionInterface $session, TplInterface $tpl) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->session = $session; |
|
26
|
|
|
$this->tpl = $tpl; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function executeBefore(Request $request, array $hookData) |
|
30
|
|
|
{ |
|
31
|
|
|
if ('POST' === $request->getRequestMethod()) { |
|
32
|
|
|
// ignore POST to verify endpoint |
|
33
|
|
|
if ('/_form/auth/verify' === $request->getPathInfo()) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
// ignore POST to token endpoint |
|
37
|
|
|
if ('/_oauth/token' === $request->getPathInfo()) { |
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($this->session->has('_form_auth_user')) { |
|
43
|
|
|
return $this->session->get('_form_auth_user'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// any other URL, enforce authentication |
|
47
|
|
|
$response = new Response(200, 'text/html'); |
|
48
|
|
|
$response->setBody( |
|
49
|
|
|
$this->tpl->render( |
|
50
|
|
|
'formAuthentication', |
|
51
|
|
|
[ |
|
52
|
|
|
'_form_auth_invalid_credentials' => false, |
|
53
|
|
|
'_form_auth_redirect_to' => $request->getUri(), |
|
54
|
|
|
'_form_auth_login_page' => true, |
|
55
|
|
|
] |
|
56
|
|
|
) |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
return $response; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|