Skip to content

Commit a4a9d04

Browse files
committed
Convert core-module to symfony-responses
1 parent faaf36b commit a4a9d04

10 files changed

Lines changed: 69 additions & 52 deletions

File tree

modules/core/src/Auth/Process/Cardinality.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ public function process(array &$state): void
195195
if (array_key_exists('core:cardinality:errorAttributes', $state)) {
196196
$id = Auth\State::saveState($state, 'core:cardinality');
197197
$url = Module::getModuleURL('core/error/cardinality');
198-
$this->httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
199-
return;
198+
$response = $this->httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
199+
$response->send();
200200
}
201201
}
202202
}

modules/core/src/Auth/Process/CardinalitySingle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public function process(array &$state): void
122122
if (array_key_exists('core:cardinality:errorAttributes', $state)) {
123123
$id = Auth\State::saveState($state, 'core:cardinality');
124124
$url = Module::getModuleURL('core/error/cardinality');
125-
$this->httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
126-
return;
125+
$response = $this->httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
126+
$response->send();
127127
}
128128
}
129129
}

modules/core/src/Auth/Process/WarnShortSSOInterval.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function process(array &$state): void
5353
$id = Auth\State::saveState($state, 'core:short_sso_interval');
5454
$url = Module::getModuleURL('core/short_sso_interval');
5555
$httpUtils = new Utils\HTTP();
56-
$httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
56+
$response = $httpUtils->redirectTrustedURL($url, ['StateId' => $id]);
57+
$response->send();
5758
}
5859
}

modules/core/src/Auth/Source/AbstractSourceSelector.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use SimpleSAML\Auth;
99
use SimpleSAML\Configuration;
1010
use SimpleSAML\Error;
11+
use Symfony\Component\HttpFoundation\Request;
12+
use Symfony\Component\HttpFoundation\Response;
1113

1214
/**
1315
* Authentication source which delegates authentication to secondary
@@ -51,9 +53,10 @@ public function __construct(array $info, array $config)
5153
* save the state, and at a later stage, load the state, update it with the authentication
5254
* information about the user, and call completeAuth with the state array.
5355
*
56+
* @param \Symfony\Component\HttpFoundation\Request The current request
5457
* @param array &$state Information about the current authentication.
5558
*/
56-
public function authenticate(array &$state): void
59+
public function authenticate(Request $request, array &$state): ?Response
5760
{
5861
$source = $this->selectAuthSource($state);
5962
$as = Auth\Source::getById($source);
@@ -62,27 +65,30 @@ public function authenticate(array &$state): void
6265
}
6366

6467
$state['sourceSelector:selected'] = $source;
65-
static::doAuthentication($as, $state);
68+
return static::doAuthentication($as, $state);
6669
}
6770

6871

6972
/**
73+
* @param \Symfony\Component\HttpFoundation\Request $request
7074
* @param \SimpleSAML\Auth\Source $as
7175
* @param array $state
72-
* @return void
7376
*/
74-
public static function doAuthentication(Auth\Source $as, array &$state): void
77+
public static function doAuthentication(Request $request, Auth\Source $as, array &$state): ?Response
7578
{
7679
try {
77-
$as->authenticate($state);
80+
$response = $as->authenticate($request, $state);
81+
if ($response instanceof Response) {
82+
return $response;
83+
}
7884
} catch (Error\Exception $e) {
7985
Auth\State::throwException($state, $e);
8086
} catch (Exception $e) {
8187
$e = new Error\UnserializableException($e);
8288
Auth\State::throwException($state, $e);
8389
}
8490

85-
Auth\Source::completeAuth($state);
91+
return parent::completeAuth($state);
8692
}
8793

8894

modules/core/src/Auth/UserPassBase.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use SimpleSAML\Logger;
1313
use SimpleSAML\Module;
1414
use SimpleSAML\Utils;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
1517

1618
/**
1719
* Helper class for username/password authentication.
@@ -194,9 +196,11 @@ public function isRememberMeChecked(): bool
194196
* This function saves the information about the login, and redirects to a
195197
* login page.
196198
*
199+
* @param \Symfony\Component\HttpFoundation\Request The current request
197200
* @param array &$state Information about the current authentication.
201+
* @return \Symfony\Component\HttpFoundation\Response
198202
*/
199-
public function authenticate(array &$state): void
203+
public function authenticate(Request $request, array &$state): ?Response
200204
{
201205
/*
202206
* Save the identifier of this authentication source, so that we can
@@ -236,7 +240,7 @@ public function authenticate(array &$state): void
236240
$attributes = $this->login($username, $password);
237241
$state['Attributes'] = $attributes;
238242

239-
return;
243+
return null;
240244
}
241245

242246
// Save the $state-array, so that we can restore it after a redirect
@@ -249,10 +253,7 @@ public function authenticate(array &$state): void
249253
$url = Module::getModuleURL('core/loginuserpass');
250254
$params = ['AuthState' => $id];
251255
$httpUtils = new Utils\HTTP();
252-
$httpUtils->redirectTrustedURL($url, $params);
253-
254-
// The previous function never returns, so this code is never executed.
255-
Assert::true(false);
256+
return $httpUtils->redirectTrustedURL($url, $params);
256257
}
257258

258259

@@ -283,7 +284,7 @@ abstract protected function login(string $username, string $password): array;
283284
* @param string $username The username the user wrote.
284285
* @param string $password The password the user wrote.
285286
*/
286-
public static function handleLogin(string $authStateId, string $username, string $password): void
287+
public static function handleLogin(string $authStateId, string $username, string $password): Response
287288
{
288289
// Here we retrieve the state array we saved in the authenticate-function.
289290
$state = Auth\State::loadState($authStateId, self::STAGEID);
@@ -316,6 +317,6 @@ public static function handleLogin(string $authStateId, string $username, string
316317
$state['Attributes'] = $attributes;
317318

318319
// Return control to SimpleSAMLphp after successful authentication.
319-
Auth\Source::completeAuth($state);
320+
return parent::completeAuth($state);
320321
}
321322
}

modules/core/src/Auth/UserPassOrgBase.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use SimpleSAML\Logger;
1111
use SimpleSAML\Module;
1212
use SimpleSAML\Utils;
13+
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpFoundation\Response;
1315

1416
/**
1517
* Helper class for username/password/organization authentication.
@@ -206,9 +208,10 @@ public function getRememberOrganizationChecked(): bool
206208
* This function saves the information about the login, and redirects to a
207209
* login page.
208210
*
211+
* @param \Symfony\Component\HttpFoundation\Request The current request
209212
* @param array &$state Information about the current authentication.
210213
*/
211-
public function authenticate(array &$state): void
214+
public function authenticate(Request $request, array &$state): ?Response
212215
{
213216
// We are going to need the authId in order to retrieve this authentication source later
214217
$state[self::AUTHID] = $this->authId;
@@ -218,7 +221,7 @@ public function authenticate(array &$state): void
218221
$url = Module::getModuleURL('core/loginuserpassorg');
219222
$params = ['AuthState' => $id];
220223
$httpUtils = new Utils\HTTP();
221-
$httpUtils->redirectTrustedURL($url, $params);
224+
return $httpUtils->redirectTrustedURL($url, $params);
222225
}
223226

224227

@@ -269,7 +272,7 @@ public static function handleLogin(
269272
string $username,
270273
string $password,
271274
string $organization,
272-
): void {
275+
): Response {
273276
/* Retrieve the authentication state. */
274277
$state = Auth\State::loadState($authStateId, self::STAGEID);
275278

@@ -314,7 +317,7 @@ public static function handleLogin(
314317
$state['PersistentAuthData'][] = self::ORGID;
315318

316319
$state['Attributes'] = $attributes;
317-
Auth\Source::completeAuth($state);
320+
return parent::completeAuth($state);
318321
}
319322

320323

modules/core/src/Controller/ErrorReport.php

Lines changed: 4 additions & 3 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

@@ -45,9 +46,9 @@ public function __construct(
4546

4647
/**
4748
* @param \Symfony\Component\HttpFoundation\Request $request
48-
* @return \SimpleSAML\XHTML\Template|\SimpleSAML\HTTP\RunnableResponse
49+
* @return \SimpleSAML\XHTML\Template|\Symfony\Component\HttpFoundation\RedirectResponse
4950
*/
50-
public function main(Request $request): Response
51+
public function main(Request $request): Template|RedirectResponse
5152
{
5253
// this page will redirect to itself after processing a POST request and sending the email
5354
if ($request->server->get('REQUEST_METHOD') !== 'POST') {
@@ -103,6 +104,6 @@ public function main(Request $request): Response
103104

104105
// redirect the user back to this page to clear the POST request
105106
$httpUtils = new Utils\HTTP();
106-
return new RunnableResponse([$httpUtils, 'redirectTrustedURL'], [$httpUtils->getSelfURLNoQuery()]);
107+
return $httpUtils->redirectTrustedURL($httpUtils->getSelfURLNoQuery());
107108
}
108109
}

modules/core/src/Controller/Exception.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SimpleSAML\Session;
1515
use SimpleSAML\Utils;
1616
use SimpleSAML\XHTML\Template;
17+
use Symfony\Component\HttpFoundation\RedirectResponse;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\HttpFoundation\Response;
1920

@@ -122,7 +123,7 @@ public function error(Request $request, string $code): Response
122123
*
123124
* @param \Symfony\Component\HttpFoundation\Request $request The request that lead to this login operation.
124125
* @throws \SimpleSAML\Error\BadRequest
125-
* @return \SimpleSAML\XHTML\Template|\Symfony\Component\HttpFoundation\RedirectResponse
126+
* @return \SimpleSAML\XHTML\Template
126127
* An HTML template or a redirection if we are not authenticated.
127128
*/
128129
public function cardinality(Request $request): Response
@@ -158,7 +159,7 @@ public function cardinality(Request $request): Response
158159
* @return \SimpleSAML\XHTML\Template|\Symfony\Component\HttpFoundation\RedirectResponse
159160
* An HTML template or a redirection if we are not authenticated.
160161
*/
161-
public function nocookie(Request $request): Response
162+
public function nocookie(Request $request): Template|RedirectResponse
162163
{
163164
$retryURL = $request->query->get('retryURL', null);
164165
if ($retryURL !== null) {
@@ -181,13 +182,12 @@ public function nocookie(Request $request): Response
181182
*
182183
* @return (
183184
* \SimpleSAML\XHTML\Template|
184-
* \SimpleSAML\HTTP\RunnableResponse|
185185
* \Symfony\Component\HttpFoundation\RedirectResponse
186-
* ) An HTML template, a redirect or a "runnable" response.
186+
* ) An HTML template, or a redirect response.
187187
*
188188
* @throws \SimpleSAML\Error\BadRequest
189189
*/
190-
public function shortSsoInterval(Request $request): Response
190+
public function shortSsoInterval(Request $request): Template|Response
191191
{
192192
$stateId = $request->query->get('StateId', false);
193193
if ($stateId === false) {
@@ -199,7 +199,7 @@ public function shortSsoInterval(Request $request): Response
199199
$continue = $request->query->get('continue', false);
200200
if ($continue !== false) {
201201
// The user has pressed the continue/retry-button
202-
Auth\ProcessingChain::resumeProcessing($state);
202+
return Auth\ProcessingChain::resumeProcessing($state);
203203
}
204204

205205
$t = new Template($this->config, 'core:short_sso_interval.twig');

modules/core/src/Controller/Login.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SimpleSAML\Utils;
1515
use SimpleSAML\XHTML\Template;
1616
use Symfony\Component\HttpFoundation\Cookie;
17+
use Symfony\Component\HttpFoundation\RedirectResponse;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\HttpFoundation\Response;
1920

@@ -101,6 +102,7 @@ public function welcome(): Template
101102
* username/password authentication.
102103
*
103104
* @param \Symfony\Component\HttpFoundation\Request $request
105+
* @return \Symfony\Component\HttpFoundation\Response
104106
*/
105107
public function loginuserpass(Request $request): Response
106108
{
@@ -147,6 +149,7 @@ public static function registerErrorCodeClass(ErrorCodes $ecc): void
147149
* @param \Symfony\Component\HttpFoundation\Request $request
148150
* @param \SimpleSAML\Module\core\Auth\UserPassBase|\SimpleSAML\Module\core\Auth\UserPassOrgBase $source
149151
* @param array $state
152+
* @return \Symfony\Component\HttpFoundation\Response
150153
*/
151154
private function handleLogin(Request $request, UserPassBase|UserPassOrgBase $source, array $state): Response
152155
{
@@ -238,9 +241,9 @@ private function handleLogin(Request $request, UserPassBase|UserPassOrgBase $sou
238241

239242
try {
240243
if ($source instanceof UserPassOrgBase) {
241-
$source::handleLogin($authStateId, $username, $password, $organization);
244+
return $source::handleLogin($authStateId, $username, $password, $organization);
242245
} else {
243-
$source::handleLogin($authStateId, $username, $password);
246+
return $source::handleLogin($authStateId, $username, $password);
244247
}
245248
} catch (Error\Error $e) {
246249
// Login failed. Extract error code and parameters, to display the error
@@ -362,6 +365,7 @@ private function handleLogin(Request $request, UserPassBase|UserPassOrgBase $sou
362365
* username/password/organization authentication.
363366
*
364367
* @param \Symfony\Component\HttpFoundation\Request $request
368+
* @return \Symfony\Component\HttpFoundation\Response
365369
*/
366370
public function loginuserpassorg(Request $request): Response
367371
{
@@ -506,8 +510,9 @@ private function getReturnPath(Request $request): string
506510
* This clears the user's IdP discovery choices.
507511
*
508512
* @param \Symfony\Component\HttpFoundation\Request $request The request that lead to this login operation.
513+
* @return \Symfony\Component\HttpFoundation\RedirectResponse
509514
*/
510-
public function cleardiscochoices(Request $request): void
515+
public function cleardiscochoices(Request $request): RedirectResponse
511516
{
512517
$httpUtils = new Utils\HTTP();
513518

@@ -527,6 +532,6 @@ public function cleardiscochoices(Request $request): void
527532
$returnTo = $this->getReturnPath($request);
528533

529534
// Redirect to destination.
530-
$httpUtils->redirectTrustedURL($returnTo);
535+
return $httpUtils->redirectTrustedURL($returnTo);
531536
}
532537
}

0 commit comments

Comments
 (0)