1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\negotiate\Controller; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use SimpleSAML\Auth; |
9
|
|
|
use SimpleSAML\Configuration; |
10
|
|
|
use SimpleSAML\Error; |
11
|
|
|
use SimpleSAML\HTTP\RunnableResponse; |
12
|
|
|
use SimpleSAML\Logger; |
13
|
|
|
use SimpleSAML\Metadata\MetaDataStorageHandler; |
14
|
|
|
use SimpleSAML\Module; |
15
|
|
|
use SimpleSAML\Module\negotiate\Auth\Source\Negotiate; |
16
|
|
|
use SimpleSAML\Session; |
17
|
|
|
use SimpleSAML\XHTML\Template; |
18
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Controller class for the negotiate module. |
23
|
|
|
* |
24
|
|
|
* This class serves the different views available in the module. |
25
|
|
|
* |
26
|
|
|
* @package simplesamlphp/simplesamlphp-module-negotiate |
27
|
|
|
*/ |
28
|
|
|
class NegotiateController |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var \SimpleSAML\Auth\Source|string |
32
|
|
|
* @psalm-var \SimpleSAML\Auth\Source|class-string |
33
|
|
|
*/ |
34
|
|
|
protected $authSource = Auth\Source::class; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \SimpleSAML\Auth\State|string |
38
|
|
|
* @psalm-var \SimpleSAML\Auth\State|class-string |
39
|
|
|
*/ |
40
|
|
|
protected $authState = Auth\State::class; |
41
|
|
|
|
42
|
|
|
/** @var \SimpleSAML\Configuration */ |
43
|
|
|
protected $config; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var \SimpleSAML\Logger|string |
47
|
|
|
* @psalm-var \SimpleSAML\Logger|class-string |
48
|
|
|
*/ |
49
|
|
|
protected $logger = Logger::class; |
50
|
|
|
|
51
|
|
|
/** @var \SimpleSAML\Metadata\MetaDataStorageHandler|null */ |
52
|
|
|
protected ?MetaDataStorageHandler $metadataHandler = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var \SimpleSAML\Module|string |
56
|
|
|
* @psalm-var \SimpleSAML\Module|class-string |
57
|
|
|
*/ |
58
|
|
|
protected $module = Module::class; |
59
|
|
|
|
60
|
|
|
/** @var \SimpleSAML\Session */ |
61
|
|
|
protected Session $session; |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Controller constructor. |
66
|
|
|
* |
67
|
|
|
* It initializes the global configuration and session for the controllers implemented here. |
68
|
|
|
* |
69
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
70
|
|
|
* @param \SimpleSAML\Session $session The session to use by the controllers. |
71
|
|
|
* |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
Configuration $config, |
76
|
|
|
Session $session, |
77
|
|
|
) { |
78
|
|
|
$this->config = $config; |
79
|
|
|
$this->session = $session; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Inject the \SimpleSAML\Auth\Source dependency. |
85
|
|
|
* |
86
|
|
|
* @param \SimpleSAML\Auth\Source $authSource |
87
|
|
|
*/ |
88
|
|
|
public function setAuthSource(Auth\Source $authSource): void |
89
|
|
|
{ |
90
|
|
|
$this->authSource = $authSource; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Inject the \SimpleSAML\Auth\State dependency. |
96
|
|
|
* |
97
|
|
|
* @param \SimpleSAML\Auth\State $authState |
98
|
|
|
*/ |
99
|
|
|
public function setAuthState(Auth\State $authState): void |
100
|
|
|
{ |
101
|
|
|
$this->authState = $authState; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Inject the \SimpleSAML\Logger dependency. |
107
|
|
|
* |
108
|
|
|
* @param \SimpleSAML\Logger $logger |
109
|
|
|
*/ |
110
|
|
|
public function setLogger(Logger $logger): void |
111
|
|
|
{ |
112
|
|
|
$this->logger = $logger; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the metadata storage handler instance. |
118
|
|
|
* |
119
|
|
|
* @return MetaDataStorageHandler |
120
|
|
|
*/ |
121
|
|
|
protected function getMetadataStorageHandler(): MetaDataStorageHandler |
122
|
|
|
{ |
123
|
|
|
return $this->metadataHandler ?: MetaDataStorageHandler::getMetadataHandler(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Inject the \SimpleSAML\Metadata\MetaDataStorageHandler dependency. |
129
|
|
|
* |
130
|
|
|
* @param \SimpleSAML\Metadata\MetaDataStorageHandler $handler |
131
|
|
|
*/ |
132
|
|
|
public function setMetadataStorageHandler(MetaDataStorageHandler $handler): void |
133
|
|
|
{ |
134
|
|
|
$this->metadataHandler = $handler; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Inject the \SimpleSAML\Module dependency. |
140
|
|
|
* |
141
|
|
|
* @param \SimpleSAML\Module $module |
142
|
|
|
*/ |
143
|
|
|
public function setModule(Module $module): void |
144
|
|
|
{ |
145
|
|
|
$this->module = $module; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Show enable. |
151
|
|
|
* |
152
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
153
|
|
|
* @return \SimpleSAML\XHTML\Template |
154
|
|
|
* @throws Exception |
155
|
|
|
*/ |
156
|
|
|
public function enable(Request $request): Template |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$this->session->setData('negotiate:disable', 'session', false, 86400); // 24*60*60=86400 |
159
|
|
|
|
160
|
|
|
$cookie = new Cookie( |
161
|
|
|
'NEGOTIATE_AUTOLOGIN_DISABLE_PERMANENT', |
162
|
|
|
null, // value |
163
|
|
|
mktime(0, 0, 0, 1, 1, 2038), // expire |
164
|
|
|
'/', // path |
165
|
|
|
'', // domain |
166
|
|
|
true, // secure |
167
|
|
|
true, // httponly |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$t = new Template($this->config, 'negotiate:enable.twig'); |
171
|
|
|
$t->headers->setCookie($cookie); |
172
|
|
|
$t->data['url'] = $this->module::getModuleURL('negotiate/disable'); |
173
|
|
|
|
174
|
|
|
return $t; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Show disable. |
180
|
|
|
* |
181
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
182
|
|
|
* @return \SimpleSAML\XHTML\Template |
183
|
|
|
* @throws Exception |
184
|
|
|
*/ |
185
|
|
|
public function disable(Request $request): Template |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
$this->session->setData('negotiate:disable', 'session', false, 86400); //24*60*60=86400 |
188
|
|
|
|
189
|
|
|
$cookie = new Cookie( |
190
|
|
|
'NEGOTIATE_AUTOLOGIN_DISABLE_PERMANENT', |
191
|
|
|
'true', // value |
192
|
|
|
mktime(0, 0, 0, 1, 1, 2038), // expire |
193
|
|
|
'/', // path |
194
|
|
|
'', // domain |
195
|
|
|
true, // secure |
196
|
|
|
true, // httponly |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$t = new Template($this->config, 'negotiate:disable.twig'); |
200
|
|
|
$t->headers->setCookie($cookie); |
201
|
|
|
$t->data['url'] = $this->module::getModuleURL('negotiate/enable'); |
202
|
|
|
|
203
|
|
|
return $t; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Show retry |
209
|
|
|
* |
210
|
|
|
* @param Request $request The request that lead to this retry operation. |
211
|
|
|
* @return \SimpleSAML\HTTP\RunnableResponse |
212
|
|
|
* @throws \Exception |
213
|
|
|
* @throws \SimpleSAML\Error\BadRequest |
214
|
|
|
*/ |
215
|
|
|
public function retry(Request $request): RunnableResponse |
216
|
|
|
{ |
217
|
|
|
/** @psalm-var string|null $authState */ |
218
|
|
|
$authState = $request->query->get('AuthState', null); |
219
|
|
|
if ($authState === null) { |
220
|
|
|
throw new Error\BadRequest('Missing required AuthState query parameter.'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$state = $this->authState::loadState($authState, Negotiate::STAGEID); |
224
|
|
|
|
225
|
|
|
$mdh = $this->getMetadataStorageHandler(); |
226
|
|
|
$idpid = $mdh->getMetaDataCurrentEntityID('saml20-idp-hosted', 'metaindex'); |
227
|
|
|
$idpmeta = $mdh->getMetaData($idpid, 'saml20-idp-hosted'); |
228
|
|
|
|
229
|
|
|
if (isset($idpmeta['auth'])) { |
230
|
|
|
$source = $this->authSource::getById($idpmeta['auth']); |
231
|
|
|
if ($source === null) { |
232
|
|
|
throw new Error\BadRequest('Invalid AuthId "' . $idpmeta['auth'] . '" - not found.'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$this->session->setData('negotiate:disable', 'session', false, 86400); //24*60*60=86400 |
236
|
|
|
$this->logger::debug('Negotiate(retry) - session enabled, retrying.'); |
237
|
|
|
|
238
|
|
|
return new RunnableResponse([$source, 'authenticate'], [&$state]); |
239
|
|
|
} |
240
|
|
|
throw new Exception('Negotiate - retry - no "auth" parameter found in IdP metadata.'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Show fallback |
246
|
|
|
* |
247
|
|
|
* @param Request $request The request that lead to this retry operation. |
248
|
|
|
* |
249
|
|
|
* @return \SimpleSAML\HTTP\RunnableResponse |
250
|
|
|
* @throws \SimpleSAML\Error\BadRequest |
251
|
|
|
* @throws \SimpleSAML\Error\NoState |
252
|
|
|
*/ |
253
|
|
|
public function fallback(Request $request): RunnableResponse |
254
|
|
|
{ |
255
|
|
|
/** @psalm-var string|null $authState */ |
256
|
|
|
$authState = $request->query->get('AuthState', null); |
257
|
|
|
if ($authState === null) { |
258
|
|
|
throw new Error\BadRequest('Missing required AuthState query parameter.'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$state = $this->authState::loadState($authState, Negotiate::STAGEID); |
262
|
|
|
|
263
|
|
|
$this->logger::debug('backend - fallback: ' . $state['LogoutState']['negotiate:backend']); |
264
|
|
|
|
265
|
|
|
return new RunnableResponse([Negotiate::class, 'fallBack'], [&$state]); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.