From e97ee5ecbe9e9911d88e15739d9392b3a092705a Mon Sep 17 00:00:00 2001 From: Tim van Dijen <tvdijen@gmail.com> Date: Tue, 13 Aug 2019 17:49:16 +0200 Subject: [PATCH] Use Webmozart for assertions --- composer.json | 1 + modules/admin/lib/TestController.php | 3 ++- .../Controller/{LoginController.php => Login.php} | 15 ++++++++------- ...{RedirectionController.php => Redirection.php} | 12 +++++++----- 4 files changed, 18 insertions(+), 13 deletions(-) rename modules/core/lib/Controller/{LoginController.php => Login.php} (94%) rename modules/core/lib/Controller/{RedirectionController.php => Redirection.php} (93%) diff --git a/composer.json b/composer.json index e8b316894..50f0b8a33 100644 --- a/composer.json +++ b/composer.json @@ -82,6 +82,7 @@ "symfony/dependency-injection": "^3.4 || ^4.0", "symfony/yaml": "^3.4 || ^4.0", "twig/twig": "~1.0 || ~2.0", + "webmozart/assert": "~1.4", "whitehat101/apr1-md5": "~1.0" }, "require-dev": { diff --git a/modules/admin/lib/TestController.php b/modules/admin/lib/TestController.php index ac4afebcd..b77da54f8 100644 --- a/modules/admin/lib/TestController.php +++ b/modules/admin/lib/TestController.php @@ -14,6 +14,7 @@ use SimpleSAML\Utils; use SimpleSAML\XHTML\Template; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Webmozart\Assert\Assert; /** * Controller class for the admin module. @@ -71,7 +72,7 @@ class TestController // This is just a simple example of an error /** @var array $state */ $state = Auth\State::loadExceptionState(); - assert(array_key_exists(Auth\State::EXCEPTION_DATA, $state)); + Assert::keyExists($state, Auth\State::EXCEPTION_DATA); throw $state[Auth\State::EXCEPTION_DATA]; } diff --git a/modules/core/lib/Controller/LoginController.php b/modules/core/lib/Controller/Login.php similarity index 94% rename from modules/core/lib/Controller/LoginController.php rename to modules/core/lib/Controller/Login.php index c41d7e053..8a5859ff5 100644 --- a/modules/core/lib/Controller/LoginController.php +++ b/modules/core/lib/Controller/Login.php @@ -13,6 +13,7 @@ use SimpleSAML\Utils; use SimpleSAML\XHTML\Template; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; +use Webmozart\Assert\Assert; /** * Controller class for the core module. @@ -21,7 +22,7 @@ use Symfony\Component\HttpFoundation\Request; * * @package SimpleSAML\Module\core */ -class LoginController +class Login { /** @var \SimpleSAML\Configuration */ protected $config; @@ -64,8 +65,8 @@ class LoginController * * @param string $as The identifier of the authentication source. * - * @return \SimpleSAML\XHTML\Template|RedirectResponse An HTML template or a redirection if we are not - * authenticated. + * @return \SimpleSAML\XHTML\Template|\Symfony\Component\HttpFoundation\RedirectResponse + * An HTML template or a redirection if we are not authenticated. * * @throws \SimpleSAML\Error\Exception An exception in case the auth source specified is invalid. */ @@ -112,8 +113,8 @@ class LoginController * @param Request $request The request that lead to this login operation. * @param string|null $as The name of the authentication source to use, if any. Optional. * - * @return \SimpleSAML\XHTML\Template|\SimpleSAML\HTTP\RunnableResponse|RedirectResponse An HTML template, a - * redirect or a "runnable" response. + * @return \SimpleSAML\XHTML\Template|\SimpleSAML\HTTP\RunnableResponse|\Symfony\Component\HttpFoundation\RedirectResponse + * An HTML template, a redirect or a "runnable" response. * * @throws \SimpleSAML\Error\Exception */ @@ -150,7 +151,7 @@ class LoginController /** @var array $state */ $state = Auth\State::loadExceptionState(); - assert(array_key_exists(Auth\State::EXCEPTION_DATA, $state)); + Assert::keyExists($state, Auth\State::EXCEPTION_DATA); $e = $state[Auth\State::EXCEPTION_DATA]; throw $e; @@ -182,7 +183,7 @@ class LoginController public function logout($as) { $auth = new Auth\Simple($as); - return new RunnableResponse([$auth, 'logout'], [$this->config->getBasePath() . 'logout.php']); + return new RunnableResponse([$auth, 'logout'], [$this->config->getBasePath() . 'core/logout/' . urlencode($as)]); } diff --git a/modules/core/lib/Controller/RedirectionController.php b/modules/core/lib/Controller/Redirection.php similarity index 93% rename from modules/core/lib/Controller/RedirectionController.php rename to modules/core/lib/Controller/Redirection.php index efb88d93c..b418a10f0 100644 --- a/modules/core/lib/Controller/RedirectionController.php +++ b/modules/core/lib/Controller/Redirection.php @@ -11,6 +11,7 @@ use SimpleSAML\Session; use SimpleSAML\Utils; use SimpleSAML\XHTML\Template; use Symfony\Component\HttpFoundation\Request; +use Webmozart\Assert\Assert; /** * Controller class for the core module. @@ -19,7 +20,7 @@ use Symfony\Component\HttpFoundation\Request; * * @package SimpleSAML\Module\core */ -class RedirectionController +class Redirection { /** @var \SimpleSAML\Configuration */ protected $config; @@ -73,6 +74,7 @@ class RedirectionController if (empty($sessionId) || empty($postId)) { throw new Error\BadRequest('Invalid session info data.'); } + } else { throw new Error\BadRequest('Missing redirection info parameter.'); } @@ -91,15 +93,15 @@ class RedirectionController $session->deleteData('core_postdatalink', $postId); - assert(is_array($postData)); - assert(array_key_exists('url', $postData)); - assert(array_key_exists('post', $postData)); + Assert::isArray($postData); + Assert::keyExists($postData, 'url'); + Assert::keyExists($postData, 'post'); if (!Utils\HTTP::isValidURL($postData['url'])) { throw new Error\Exception('Invalid destination URL.'); } - $t = new Template($this->config, 'post.php'); + $t = new Template($this->config, 'post.twig'); $t->data['destination'] = $postData['url']; $t->data['post'] = $postData['post']; return $t; -- GitLab