-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathTest.php
More file actions
168 lines (144 loc) · 4.98 KB
/
Copy pathTest.php
File metadata and controls
168 lines (144 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\admin\Controller;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Auth;
use SimpleSAML\Configuration;
use SimpleSAML\HTTP\RunnableResponse;
use SimpleSAML\Locale\Translate;
use SimpleSAML\Module;
use SimpleSAML\Session;
use SimpleSAML\Utils;
use SimpleSAML\XHTML\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller class for the admin module.
*
* This class serves the 'Test authentication sources' views available in the module.
*
* @package SimpleSAML\Module\admin
*/
class Test
{
/**
* @var \SimpleSAML\Utils\Auth
*/
protected Utils\Auth $authUtils;
/**
* @var \SimpleSAML\Auth\Simple|string
* @psalm-var \SimpleSAML\Auth\Simple|class-string
*/
protected $authSimple = Auth\Simple::class;
/**
* @var \SimpleSAML\Auth\State|string
* @psalm-var \SimpleSAML\Auth\State|class-string
*/
protected $authState = Auth\State::class;
/** @var \SimpleSAML\Module\admin\Controller\Menu */
protected Menu $menu;
/**
* TestController constructor.
*
* @param \SimpleSAML\Configuration $config The configuration to use.
* @param \SimpleSAML\Session $session The current user session.
*/
public function __construct(
protected Configuration $config,
protected Session $session,
) {
$this->menu = new Menu();
$this->authUtils = new Utils\Auth();
}
/**
* Inject the \SimpleSAML\Utils\Auth dependency.
*
* @param \SimpleSAML\Utils\Auth $authUtils
*/
public function setAuthUtils(Utils\Auth $authUtils): void
{
$this->authUtils = $authUtils;
}
/**
* Inject the \SimpleSAML\Auth\Simple dependency.
*
* @param \SimpleSAML\Auth\Simple $authSimple
*/
public function setAuthSimple(Auth\Simple $authSimple): void
{
$this->authSimple = $authSimple;
}
/**
* Inject the \SimpleSAML\Auth\State dependency.
*
* @param \SimpleSAML\Auth\State $authState
*/
public function setAuthState(Auth\State $authState): void
{
$this->authState = $authState;
}
/**
* Display the list of available authsources.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string|null $as
* @return \SimpleSAML\XHTML\Template|\SimpleSAML\HTTP\RunnableResponse
*/
public function main(Request $request, ?string $as = null): Response
{
$this->authUtils->requireAdmin();
if (is_null($as)) {
$t = new Template($this->config, 'admin:authsource_list.twig');
$t->data = [
'sources' => Auth\Source::getSources(),
];
} else {
/** @psalm-suppress UndefinedClass */
$authsource = new $this->authSimple($as);
if (!is_null($request->query->get('logout'))) {
return new RunnableResponse([$authsource, 'logout'], [Module::getModuleURL('admin/logout')]);
} elseif (!is_null($request->query->get(Auth\State::EXCEPTION_PARAM))) {
// This is just a simple example of an error
/** @var array $state */
$state = $this->authState::loadExceptionState();
Assert::keyExists($state, Auth\State::EXCEPTION_DATA);
throw $state[Auth\State::EXCEPTION_DATA];
}
if (!$authsource->isAuthenticated()) {
$url = Module::getModuleURL('admin/test/' . $as, []);
$params = [
'ErrorURL' => $url,
'ReturnTo' => $url,
Auth\State::RESTART => $url,
];
return new RunnableResponse([$authsource, 'login'], [$params]);
}
$attributes = $authsource->getAttributes();
$authData = $authsource->getAuthDataArray();
$nameId = $authsource->getAuthData('saml:sp:NameID') ?? false;
$httpUtils = new Utils\HTTP();
$t = new Template($this->config, 'admin:status.twig');
$l = $t->getLocalization();
$l->addAttributeDomains();
$t->data = [
'attributes' => $attributes,
'authData' => $authData,
'remaining' => isset($authData['Expire']) ? $authData['Expire'] - time() : null,
'nameid' => $nameId,
'logouturl' => $httpUtils->getSelfURLNoQuery() . '?as=' . urlencode($as) . '&logout',
];
}
$this->menu->addOption('logout', $this->authUtils->getAdminLogoutURL(), Translate::noop('Log out'));
return $this->menu->insert($t);
}
/**
* Page to show after logout completed
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \SimpleSAML\XHTML\Template
*/
public function logout(Request $request): Template
{
return new Template($this->config, 'admin:logout.twig');
}
}