Skip to content

Commit 2300a7b

Browse files
committed
Convert exampleauth-module to symfony-responses
1 parent 49db1dd commit 2300a7b

4 files changed

Lines changed: 49 additions & 47 deletions

File tree

modules/exampleauth/src/Auth/Process/RedirectTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function process(array &$state): void
3232
$url = Module::getModuleURL('exampleauth/redirecttest');
3333

3434
$httpUtils = new Utils\HTTP();
35-
$httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
35+
$response = $httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
36+
$response->send();
3637
}
3738
}

modules/exampleauth/src/Auth/Source/External.php

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SimpleSAML\Module;
1111
use SimpleSAML\Utils;
1212
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
1314
use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
1415

1516
/**
@@ -66,12 +67,12 @@ private function getUser(): ?array
6667
* stored in the users PHP session, but this could be replaced
6768
* with anything.
6869
*/
69-
$session = new SymfonySession();
70-
if (!$session->getId()) {
71-
$session->start();
70+
if (!session_id()) {
71+
// session_start not called before. Do it here
72+
@session_start();
7273
}
7374

74-
if (!$session->has('uid')) {
75+
if (!isset($_SESSION['uid'])) {
7576
// The user isn't authenticated
7677
return null;
7778
}
@@ -82,14 +83,14 @@ private function getUser(): ?array
8283
* to store them as arrays.
8384
*/
8485
$attributes = [
85-
'uid' => [$session->get('uid')],
86-
'displayName' => [$session->get('name')],
87-
'mail' => [$session->get('mail')],
86+
'uid' => [$_SESSION['uid']],
87+
'displayName' => [$_SESSION['name']],
88+
'mail' => [$_SESSION['mail']],
8889
];
8990

9091
// Here we generate a multivalued attribute based on the account type
9192
$attributes['eduPersonAffiliation'] = [
92-
$session->get('type'), /* In this example, either 'student' or 'employee'. */
93+
$_SESSION['type'], /* In this example, either 'student' or 'employee'. */
9394
'member',
9495
];
9596

@@ -100,9 +101,11 @@ private function getUser(): ?array
100101
/**
101102
* Log in using an external authentication helper.
102103
*
104+
* @param \Symfony\Component\HttpFoundation\Request The current request
103105
* @param array &$state Information about the current authentication.
106+
* @return \Symfony\Component\HttpFoundation\Response
104107
*/
105-
public function authenticate(array &$state): void
108+
public function authenticate(Request $request, array &$state): ?Response
106109
{
107110
$attributes = $this->getUser();
108111
if ($attributes !== null) {
@@ -113,7 +116,7 @@ public function authenticate(array &$state): void
113116
* to the authentication process.
114117
*/
115118
$state['Attributes'] = $attributes;
116-
return;
119+
return null;
117120
}
118121

119122
/*
@@ -148,7 +151,7 @@ public function authenticate(array &$state): void
148151
* option to return the user to a specific page afterwards.
149152
*/
150153
$returnTo = Module::getModuleURL('exampleauth/resume', [
151-
'State' => $stateId,
154+
'AuthState' => $stateId,
152155
]);
153156

154157
/*
@@ -167,14 +170,9 @@ public function authenticate(array &$state): void
167170
* the real name of the parameter for the login page.
168171
*/
169172
$httpUtils = new Utils\HTTP();
170-
$httpUtils->redirectTrustedURL($authPage, [
173+
return $httpUtils->redirectTrustedURL($authPage, [
171174
'ReturnTo' => $returnTo,
172175
]);
173-
174-
/*
175-
* The redirect function never returns, so we never get this far.
176-
*/
177-
Assert::true(false);
178176
}
179177

180178

@@ -185,25 +183,27 @@ public function authenticate(array &$state): void
185183
* entered his or her credentials.
186184
*
187185
* @param \Symfony\Component\HttpFoundation\Request $request
186+
* @param \SimpleSAML\Auth\State $authState
187+
* @return \Symfony\Component\HttpFoundation\Response
188188
*
189189
* @throws \SimpleSAML\Error\BadRequest
190190
* @throws \SimpleSAML\Error\Exception
191191
*/
192-
public static function resume(Request $request): void
192+
public static function resume(Request $request, Auth\State $authState): Response
193193
{
194194
/*
195195
* First we need to restore the $state-array. We should have the identifier for
196196
* it in the 'State' request parameter.
197197
*/
198-
if (!$request->query->has('State')) {
198+
if (!$request->query->has('AuthState')) {
199199
throw new Error\BadRequest('Missing "State" parameter.');
200200
}
201201

202202
/*
203203
* Once again, note the second parameter to the loadState function. This must
204204
* match the string we used in the saveState-call above.
205205
*/
206-
$state = Auth\State::loadState($request->query->get('State'), 'exampleauth:External');
206+
$state = $authState::loadState($request->query->get('AuthState'), 'exampleauth:External');
207207

208208
/*
209209
* Now we have the $state-array, and can use it to locate the authentication
@@ -249,12 +249,7 @@ public static function resume(Request $request): void
249249
*/
250250

251251
$state['Attributes'] = $attributes;
252-
Auth\Source::completeAuth($state);
253-
254-
/*
255-
* The completeAuth-function never returns, so we never get this far.
256-
*/
257-
Assert::true(false);
252+
return parent::completeAuth($state);
258253
}
259254

260255

@@ -264,18 +259,22 @@ public static function resume(Request $request): void
264259
*
265260
* @param array &$state The logout state array.
266261
*/
267-
public function logout(array &$state): void
262+
public function logout(array &$state): null
268263
{
269-
$session = new SymfonySession();
270-
if (!$session->getId()) {
271-
$session->start();
264+
if (!session_id()) {
265+
// session_start not called before. Do it here
266+
@session_start();
272267
}
273268

274-
$session->clear();
269+
/**
270+
* In this example we simply remove the 'uid' from the session
271+
*/
272+
unset($_SESSION['uid']);
275273

276-
/*
274+
/**
277275
* If we need to do a redirect to a different page, we could do this
278276
* here, but in this example we don't need to do this.
279277
*/
278+
return null;
280279
}
281280
}

modules/exampleauth/src/Auth/Source/StaticSource.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ public function __construct(array $info, array $config)
5151
/**
5252
* Log in using static attributes.
5353
*
54+
* @param \Symfony\Component\HttpFoundation\Request $request The current request
5455
* @param array &$state Information about the current authentication.
56+
* @return \Symfony\Component\HttpFoundation\Response
5557
*/
56-
public function authenticate(array &$state): void
58+
public function authenticate(Request $request, array &$state): ?Response
5759
{
5860
$state['Attributes'] = $this->attributes;
61+
return null;
5962
}
6063
}

modules/exampleauth/src/Controller/ExampleAuth.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use SimpleSAML\Session;
1313
use SimpleSAML\Utils;
1414
use SimpleSAML\XHTML\Template;
15+
use Symfony\Component\HttpFoundation\RedirectResponse;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpFoundation\Session\Session as SymfonySession;
@@ -68,9 +69,9 @@ public function setAuthState(Auth\State $authState): void
6869
*
6970
* @param \Symfony\Component\HttpFoundation\Request $request The current request.
7071
*
71-
* @return \SimpleSAML\XHTML\Template|\SimpleSAML\HTTP\RunnableResponse
72+
* @return \SimpleSAML\XHTML\Template|\Symfony\Component\HttpFoundation\RedirectResponse
7273
*/
73-
public function authpage(Request $request): Response
74+
public function authpage(Request $request): RedirectResponse
7475
{
7576
/**
7677
* This page serves as a dummy login page.
@@ -145,7 +146,7 @@ public function authpage(Request $request): Response
145146
$session->set('mail', $user['mail']);
146147
$session->set('type', $user['type']);
147148

148-
return new RunnableResponse([$httpUtils, 'redirectTrustedURL'], [$returnTo]);
149+
return $httpUtils->redirectTrustedURL($returnTo);
149150
}
150151
}
151152

@@ -162,41 +163,39 @@ public function authpage(Request $request): Response
162163
* Redirect testpage.
163164
*
164165
* @param \Symfony\Component\HttpFoundation\Request $request The current request.
165-
*
166-
* @return \SimpleSAML\HTTP\RunnableResponse
166+
* @return \Symfony\Component\HttpFoundation\Response
167167
*/
168-
public function redirecttest(Request $request): RunnableResponse
168+
public function redirecttest(Request $request): Response
169169
{
170170
/**
171171
* Request handler for redirect filter test.
172172
*/
173-
$stateId = $request->query->get('StateId');
173+
$stateId = $request->query->get('AuthState');
174174
if ($stateId === null) {
175-
throw new Error\BadRequest('Missing required StateId query parameter.');
175+
throw new Error\BadRequest('Missing required AuthState query parameter.');
176176
}
177177

178178
$state = $this->authState::loadState($stateId, 'exampleauth:redirectfilter-test');
179179
$state['Attributes']['RedirectTest2'] = ['OK'];
180180

181-
return new RunnableResponse([Auth\ProcessingChain::class, 'resumeProcessing'], [$state]);
181+
return Auth\ProcessingChain::resumeProcessing($state);
182182
}
183183

184184

185185
/**
186186
* Resume testpage.
187187
*
188188
* @param \Symfony\Component\HttpFoundation\Request $request The current request.
189-
*
190-
* @return \SimpleSAML\HTTP\RunnableResponse
189+
* @return \Symfony\Component\HttpFoundation\Response
191190
*/
192-
public function resume(Request $request): RunnableResponse
191+
public function resume(Request $request): Response
193192
{
194193
/**
195194
* This page serves as the point where the user's authentication
196195
* process is resumed after the login page.
197196
*
198197
* It simply passes control back to the class.
199198
*/
200-
return new RunnableResponse([External::class, 'resume'], [$request]);
199+
return External::resume($request, $this->authState);
201200
}
202201
}

0 commit comments

Comments
 (0)