From 1b7e83c1c02cd1b89e1e990c5686d0d1c8014334 Mon Sep 17 00:00:00 2001 From: n3vrax Date: Tue, 14 Mar 2017 03:53:06 +0200 Subject: [PATCH 1/2] updating --- composer.json | 14 +- src/Action/LoginAction.php | 14 +- src/Action/LogoutAction.php | 16 +-- src/ConfigProvider.php | 26 ---- src/ErrorHandler/UnauthorizedHandler.php | 136 ++++++++++++------ src/Factory/BaseActionFactory.php | 2 +- src/Factory/LoginActionFactory.php | 2 +- src/Factory/LogoutActionFactory.php | 2 +- src/Factory/UnauthorizedHandlerFactory.php | 2 +- .../WebAuthenticationOptionsFactory.php | 2 +- 10 files changed, 117 insertions(+), 99 deletions(-) diff --git a/composer.json b/composer.json index 3916a6b..6d40634 100644 --- a/composer.json +++ b/composer.json @@ -11,12 +11,13 @@ ], "require": { "php": "^7.1", - "psr/http-message": "^1.0", - "container-interop/container-interop": "^1.1", + "psr/http-message": "^1.0.1", + "http-interop/http-middleware": "^0.4.1", + "zendframework/zend-servicemanager": "^3.3.0", + "zendframework/zend-expressive-helpers": "^3.0", "dotkernel/dot-authentication": "^0.1", "dotkernel/dot-event": "^0.1", - "dotkernel/dot-helpers": "^0.1", "dotkernel/dot-flashmessenger": "^0.1" }, "require-dev": { @@ -24,8 +25,8 @@ "squizlabs/php_codesniffer": "^2.3", "zendframework/zend-stdlib": "^3.1", - "zendframework/zend-diactoros": "^1.3", - "zendframework/zend-expressive-template": "^1.0" + "zendframework/zend-diactoros": "^1.3.10", + "zendframework/zend-expressive-template": "^1.0.4" }, "autoload": { "psr-4": { @@ -39,7 +40,8 @@ }, "extra": { "branch-alias": { - "dev-master": "0.2-dev" + "dev-master": "0.1-dev", + "dev-develop": "0.2-dev" } } } diff --git a/src/Action/LoginAction.php b/src/Action/LoginAction.php index fde197b..759aa95 100644 --- a/src/Action/LoginAction.php +++ b/src/Action/LoginAction.php @@ -21,6 +21,8 @@ use Dot\Authentication\Web\Utils; use Dot\FlashMessenger\FlashMessengerInterface; use Dot\Helpers\Route\RouteOptionHelper; +use Interop\Http\ServerMiddleware\DelegateInterface; +use Interop\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\HtmlResponse; @@ -32,7 +34,7 @@ * Class LoginAction * @package Dot\Authentication\Web\Action */ -class LoginAction implements AuthenticationEventListenerInterface +class LoginAction implements MiddlewareInterface, AuthenticationEventListenerInterface { use DispatchAuthenticationEventTrait; use AuthenticationEventListenerTrait; @@ -82,15 +84,11 @@ public function __construct( /** * @param ServerRequestInterface $request - * @param ResponseInterface $response - * @param callable|null $next + * @param DelegateInterface $delegate * @return ResponseInterface */ - public function __invoke( - ServerRequestInterface $request, - ResponseInterface $response, - callable $next = null - ): ResponseInterface { + public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface + { if ($this->authentication->hasIdentity()) { return new RedirectResponse($this->routeHelper->getUri($this->options->getAfterLoginRoute())); } diff --git a/src/Action/LogoutAction.php b/src/Action/LogoutAction.php index 29fa2b0..5351fc4 100644 --- a/src/Action/LogoutAction.php +++ b/src/Action/LogoutAction.php @@ -16,6 +16,8 @@ use Dot\Authentication\Web\Event\DispatchAuthenticationEventTrait; use Dot\Authentication\Web\Options\WebAuthenticationOptions; use Dot\Helpers\Route\RouteOptionHelper; +use Interop\Http\ServerMiddleware\DelegateInterface; +use Interop\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\RedirectResponse; @@ -24,7 +26,7 @@ * Class LogoutAction * @package Dot\Authentication\Web\Action */ -class LogoutAction implements AuthenticationEventListenerInterface +class LogoutAction implements MiddlewareInterface, AuthenticationEventListenerInterface { use AuthenticationEventListenerTrait; use DispatchAuthenticationEventTrait; @@ -56,15 +58,11 @@ public function __construct( /** * @param ServerRequestInterface $request - * @param ResponseInterface $response - * @param callable|null $next - * @return RedirectResponse|ResponseInterface + * @param DelegateInterface $delegate + * @return ResponseInterface */ - public function __invoke( - ServerRequestInterface $request, - ResponseInterface $response, - callable $next = null - ): ResponseInterface { + public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface + { if (!$this->authentication->hasIdentity()) { return new RedirectResponse($this->routeHelper->getUri($this->options->getAfterLogoutRoute())); } diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 1ab3343..543df43 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -25,32 +25,6 @@ public function __invoke(): array return [ 'dependencies' => $this->getDependenciesConfig(), - 'middleware_pipeline' => [ - 'error' => [ - 'middleware' => [ - UnauthorizedHandler::class, - ], - 'error' => true, - 'priority' => -10000, - ], - ], - - //default routes - 'routes' => [ - 'login_route' => [ - 'name' => 'login', - 'path' => '/login', - 'middleware' => LoginAction::class, - 'allowed_methods' => ['GET', 'POST'] - ], - 'logout_route' => [ - 'name' => 'logout', - 'path' => '/logout', - 'middleware' => LogoutAction::class, - 'allowed_methods' => ['GET'] - ], - ], - 'dot_authentication' => [ 'web' => [ 'event_listeners' => [], diff --git a/src/ErrorHandler/UnauthorizedHandler.php b/src/ErrorHandler/UnauthorizedHandler.php index cf63b1f..73b90e6 100644 --- a/src/ErrorHandler/UnauthorizedHandler.php +++ b/src/ErrorHandler/UnauthorizedHandler.php @@ -21,17 +21,20 @@ use Dot\FlashMessenger\FlashMessengerInterface; use Dot\Helpers\Route\RouteOptionHelper; use Dot\Helpers\Route\UriHelperTrait; +use Interop\Http\ServerMiddleware\DelegateInterface; +use Interop\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\UriInterface; use Zend\Diactoros\Response\RedirectResponse; use Zend\Diactoros\Uri; +use Zend\Expressive\Helper\UrlHelper; /** * Class UnauthorizedHandler * @package Dot\Authentication\Web\ErrorHandler */ -class UnauthorizedHandler implements AuthenticationEventListenerInterface +class UnauthorizedHandler implements MiddlewareInterface, AuthenticationEventListenerInterface { use AuthenticationEventListenerTrait; use DispatchAuthenticationEventTrait; @@ -40,6 +43,9 @@ class UnauthorizedHandler implements AuthenticationEventListenerInterface /** @var AuthenticationInterface */ protected $authenticationService; + /** @var UrlHelper */ + protected $urlHelper; + /** @var WebAuthenticationOptions */ protected $options; @@ -74,62 +80,102 @@ public function __construct( $this->routeHelper = $routeHelper; } + /** + * @param ServerRequestInterface $request + * @param DelegateInterface $delegate + * @return ResponseInterface + * @throws \Exception + * @throws \Throwable + */ + public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface + { + try { + $response = $delegate->process($request); + + if (in_array($response->getStatusCode(), $this->statusCodes)) { + return $this->handleUnauthorizedError( + $this->options->getMessagesOptions()->getMessage(MessagesOptions::UNAUTHORIZED), + $request, + $response + ); + } + return $response; + } catch (UnauthorizedException $e) { + return $this->handleUnauthorizedError($e, $request); + } catch (\Throwable $e) { + if (in_array($e->getCode(), $this->statusCodes)) { + return $this->handleUnauthorizedError($e, $request); + } + throw $e; + } catch (\Exception $e) { + if (in_array($e->getCode(), $this->statusCodes)) { + return $this->handleUnauthorizedError($e, $request); + } + throw $e; + } + } + /** * @param $error * @param ServerRequestInterface $request - * @param ResponseInterface $response - * @param callable|null $next + * @param ResponseInterface|null $response * @return ResponseInterface */ - public function __invoke( + protected function handleUnauthorizedError( $error, ServerRequestInterface $request, - ResponseInterface $response, - callable $next = null + ResponseInterface $response = null ): ResponseInterface { - if ($error instanceof \Exception && in_array($error->getCode(), $this->statusCodes) - || in_array($response->getStatusCode(), $this->statusCodes) - ) { - $event = $this->dispatchEvent(AuthenticationEvent::EVENT_UNAUTHORIZED, [ - 'request' => $request, - 'authenticationService' => $this->authenticationService, - 'error' => $error - ]); - if ($event instanceof ResponseInterface) { - return $event; - } - - $messages = $this->getErrorMessages($error); - if (empty($messages)) { - $messages = [$this->options->getMessagesOptions()->getMessage(MessagesOptions::UNAUTHORIZED)]; - } + $event = $this->dispatchEvent(AuthenticationEvent::EVENT_UNAUTHORIZED, [ + 'request' => $request, + 'response' => $response, + 'authenticationService' => $this->authenticationService, + 'error' => $error + ]); + if ($event instanceof ResponseInterface) { + return $event; + } - //add a flash message in case the login page displays errors - if ($this->flashMessenger) { - $this->flashMessenger->addError($messages); - } + $messages = $this->getErrorMessages($error); + if (empty($messages)) { + $messages = [$this->options->getMessagesOptions()->getMessage(MessagesOptions::UNAUTHORIZED)]; + } - /** @var Uri $uri */ - $uri = $this->routeHelper->getUri($this->options->getLoginRoute()); - if ($this->areUriEqual($uri, $request->getUri())) { - throw new RuntimeException( - 'Default unauthorized listener has detected that the login route is not authorized either.' . - ' This can result in an endless redirect loop. ' . - 'Please edit your authorization schema to open login route to guests' - ); - } - if ($this->options->isEnableWantedUrl()) { - $uri = $this->appendQueryParam( - $uri, - $this->options->getWantedUrlName(), - $request->getUri()->__toString() - ); - } + //add a flash message in case the login page displays errors + if ($this->flashMessenger) { + $this->flashMessenger->addError($messages); + } - return new RedirectResponse($uri); + /** @var Uri $uri */ + $uri = $this->routeHelper->getUri($this->options->getLoginRoute()); + if ($this->areUriEqual($uri, $request->getUri())) { + throw new RuntimeException( + 'Default unauthorized listener has detected that the login route is not authorized either.' . + ' This can result in an endless redirect loop. ' . + 'Please edit your authorization schema to open login route to guests' + ); + } + if ($this->options->isEnableWantedUrl()) { + $uri = $this->appendQueryParam( + $uri, + $this->options->getWantedUrlName(), + $request->getUri()->__toString() + ); } - return $next($request, $response, $error); + return new RedirectResponse($uri); + } + + protected function getRedirectUri() + { + $loginRoute = $this->options->getLoginRoute(); + $uri = $this->urlHelper->generate([ + $loginRoute['route_name'] ?? '', + $loginRoute['route_params'] ?? [], + $loginRoute['query_params'] ?? [], + $loginRoute['fragment_identifier'], + $loginRoute['options'] ?? [] + ]); } /** @@ -146,7 +192,7 @@ protected function getErrorMessages($error): array $messages[] = $e; } } - } elseif ($error instanceof \Exception) { + } elseif ($error instanceof \Exception || $error instanceof \Throwable) { if ($this->isDebug() || $error instanceof UnauthorizedException) { $messages[] = $error->getMessage(); } diff --git a/src/Factory/BaseActionFactory.php b/src/Factory/BaseActionFactory.php index d8866dc..c11286c 100644 --- a/src/Factory/BaseActionFactory.php +++ b/src/Factory/BaseActionFactory.php @@ -12,7 +12,7 @@ use Dot\Authentication\Web\Event\AuthenticationEventListenerInterface; use Dot\Authentication\Web\Exception\RuntimeException; use Dot\Authentication\Web\Options\WebAuthenticationOptions; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Zend\EventManager\EventManagerInterface; /** diff --git a/src/Factory/LoginActionFactory.php b/src/Factory/LoginActionFactory.php index 8e4fa76..76ada65 100644 --- a/src/Factory/LoginActionFactory.php +++ b/src/Factory/LoginActionFactory.php @@ -14,7 +14,7 @@ use Dot\Authentication\Web\Options\WebAuthenticationOptions; use Dot\FlashMessenger\FlashMessengerInterface; use Dot\Helpers\Route\RouteOptionHelper; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use Zend\Expressive\Template\TemplateRendererInterface; /** diff --git a/src/Factory/LogoutActionFactory.php b/src/Factory/LogoutActionFactory.php index 95e6c88..fe6e1bb 100644 --- a/src/Factory/LogoutActionFactory.php +++ b/src/Factory/LogoutActionFactory.php @@ -13,7 +13,7 @@ use Dot\Authentication\Web\Action\LogoutAction; use Dot\Authentication\Web\Options\WebAuthenticationOptions; use Dot\Helpers\Route\RouteOptionHelper; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; /** * Class LogoutActionFactory diff --git a/src/Factory/UnauthorizedHandlerFactory.php b/src/Factory/UnauthorizedHandlerFactory.php index 497547f..e6fa3ac 100644 --- a/src/Factory/UnauthorizedHandlerFactory.php +++ b/src/Factory/UnauthorizedHandlerFactory.php @@ -14,7 +14,7 @@ use Dot\Authentication\Web\Options\WebAuthenticationOptions; use Dot\FlashMessenger\FlashMessengerInterface; use Dot\Helpers\Route\RouteOptionHelper; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; /** * Class UnauthorizedHandlerFactory diff --git a/src/Factory/WebAuthenticationOptionsFactory.php b/src/Factory/WebAuthenticationOptionsFactory.php index 30db784..20207d8 100644 --- a/src/Factory/WebAuthenticationOptionsFactory.php +++ b/src/Factory/WebAuthenticationOptionsFactory.php @@ -10,7 +10,7 @@ namespace Dot\Authentication\Web\Factory; use Dot\Authentication\Web\Options\WebAuthenticationOptions; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; /** * Class WebAuthenticationOptionsFactory From cc0020849c273470e92a3ba8e5cef9b22490d20b Mon Sep 17 00:00:00 2001 From: n3vrax Date: Wed, 15 Mar 2017 02:37:12 +0200 Subject: [PATCH 2/2] updating to ZE2 --- CHANGELOG.md | 21 ++++++++++ authentication-web.global.php.dist | 26 +++++++++--- composer.json | 17 ++++---- src/Action/LoginAction.php | 12 +++--- src/Action/LogoutAction.php | 12 +++--- src/ConfigProvider.php | 11 +----- src/ErrorHandler/UnauthorizedHandler.php | 46 ++++------------------ src/Factory/LoginActionFactory.php | 4 +- src/Factory/LogoutActionFactory.php | 4 +- src/Factory/UnauthorizedHandlerFactory.php | 4 +- 10 files changed, 77 insertions(+), 80 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a59d40a..aceb53e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,24 @@ +## 0.2.0 - 2017-03-15 + +Updating package to support ZE2 and other DotKernel dependencies updates + +### Changed +* Updated factories to typehint against PSR11 container +* UnauthorizedHandler changed to a valid ZE2 error handler + +### Added +* Middleware now implement PSR15 MiddlewareInterface + +### Deprecated +* Nothing + +### Removed +* Nothing + +### Fixed +* Nothing + + ## 0.1.1 - 2017-03-10 ### Added diff --git a/authentication-web.global.php.dist b/authentication-web.global.php.dist index fef88d0..39d3bd7 100644 --- a/authentication-web.global.php.dist +++ b/authentication-web.global.php.dist @@ -9,17 +9,33 @@ return [ 'dot_authentication' => [ //this package's specific configuration template 'web' => [ - //change next two only if you changed the default login/logout routes - 'login_route' => ['route_name' => 'login', 'route_params' => [], 'query_params' => []], - 'logout_route' => ['route_name' => 'logout', 'route_params' => []], + 'login_route' => [ + 'route_name' => 'login', + 'route_params' => [], + 'query_params' => [], + 'fragment_id' => null, + 'options' => [] + ], + + 'logout_route' => [ + 'route_name' => 'logout', + 'route_params' => [], + //... + ], //template name to use for the login form 'login_template' => 'app::login', //where to redirect after login success - 'after_login_route' => ['route_name' => 'my-account', 'route_params' => []], + 'after_login_route' => [ + 'route_name' => 'home', + 'route_params' => [] + ], //where to redirect after logging out - 'after_logout_route' => ['route_name' => 'login', 'route_params' => []], + 'after_logout_route' => [ + 'route_name' => 'login', + 'route_params' => [] + ], //enable the wanted url feature, to login to the previously requested uri after login 'enable_wanted_url' => true, diff --git a/composer.json b/composer.json index 6d40634..2831d15 100644 --- a/composer.json +++ b/composer.json @@ -11,22 +11,23 @@ ], "require": { "php": "^7.1", - "psr/http-message": "^1.0.1", + "psr/http-message": "^1.0", "http-interop/http-middleware": "^0.4.1", "zendframework/zend-servicemanager": "^3.3.0", - "zendframework/zend-expressive-helpers": "^3.0", "dotkernel/dot-authentication": "^0.1", - "dotkernel/dot-event": "^0.1", - "dotkernel/dot-flashmessenger": "^0.1" + "dotkernel/dot-event": "^0.2", + "dotkernel/dot-helpers": "^0.2", + "dotkernel/dot-flashmessenger": "^0.2" }, "require-dev": { "phpunit/phpunit": "^4.8", "squizlabs/php_codesniffer": "^2.3", "zendframework/zend-stdlib": "^3.1", - "zendframework/zend-diactoros": "^1.3.10", - "zendframework/zend-expressive-template": "^1.0.4" + "zendframework/zend-diactoros": "^1.3", + "zendframework/zend-expressive-template": "^1.0", + "zendframework/zend-expressive": "^2.0" }, "autoload": { "psr-4": { @@ -40,8 +41,8 @@ }, "extra": { "branch-alias": { - "dev-master": "0.1-dev", - "dev-develop": "0.2-dev" + "dev-master": "0.2-dev", + "dev-develop": "0.3-dev" } } } diff --git a/src/Action/LoginAction.php b/src/Action/LoginAction.php index 759aa95..db0f8bb 100644 --- a/src/Action/LoginAction.php +++ b/src/Action/LoginAction.php @@ -20,7 +20,7 @@ use Dot\Authentication\Web\Options\WebAuthenticationOptions; use Dot\Authentication\Web\Utils; use Dot\FlashMessenger\FlashMessengerInterface; -use Dot\Helpers\Route\RouteOptionHelper; +use Dot\Helpers\Route\RouteHelper; use Interop\Http\ServerMiddleware\DelegateInterface; use Interop\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Message\ResponseInterface; @@ -42,7 +42,7 @@ class LoginAction implements MiddlewareInterface, AuthenticationEventListenerInt /** @var AuthenticationInterface */ protected $authentication; - /** @var RouteOptionHelper */ + /** @var RouteHelper */ protected $routeHelper; /** @var WebAuthenticationOptions */ @@ -64,14 +64,14 @@ class LoginAction implements MiddlewareInterface, AuthenticationEventListenerInt * LoginAction constructor. * @param AuthenticationInterface $authentication * @param TemplateRendererInterface $template - * @param RouteOptionHelper $routeHelper + * @param RouteHelper $routeHelper * @param WebAuthenticationOptions $options * @param FlashMessengerInterface $flashMessenger */ public function __construct( AuthenticationInterface $authentication, TemplateRendererInterface $template, - RouteOptionHelper $routeHelper, + RouteHelper $routeHelper, WebAuthenticationOptions $options, FlashMessengerInterface $flashMessenger ) { @@ -90,7 +90,7 @@ public function __construct( public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface { if ($this->authentication->hasIdentity()) { - return new RedirectResponse($this->routeHelper->getUri($this->options->getAfterLoginRoute())); + return new RedirectResponse($this->routeHelper->generateUri($this->options->getAfterLoginRoute())); } $this->request = $request; @@ -140,7 +140,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele if (empty($error)) { $this->dispatchEvent(AuthenticationEvent::EVENT_AUTHENTICATION_SUCCESS, $params); - $uri = $this->routeHelper->getUri($this->options->getAfterLoginRoute()); + $uri = $this->routeHelper->generateUri($this->options->getAfterLoginRoute()); if ($this->options->isEnableWantedUrl()) { $params = $request->getQueryParams(); $wantedUrlName = $this->options->getWantedUrlName(); diff --git a/src/Action/LogoutAction.php b/src/Action/LogoutAction.php index 5351fc4..08c7f8a 100644 --- a/src/Action/LogoutAction.php +++ b/src/Action/LogoutAction.php @@ -15,7 +15,7 @@ use Dot\Authentication\Web\Event\AuthenticationEventListenerTrait; use Dot\Authentication\Web\Event\DispatchAuthenticationEventTrait; use Dot\Authentication\Web\Options\WebAuthenticationOptions; -use Dot\Helpers\Route\RouteOptionHelper; +use Dot\Helpers\Route\RouteHelper; use Interop\Http\ServerMiddleware\DelegateInterface; use Interop\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Message\ResponseInterface; @@ -34,7 +34,7 @@ class LogoutAction implements MiddlewareInterface, AuthenticationEventListenerIn /** @var AuthenticationInterface */ protected $authentication; - /** @var RouteOptionHelper */ + /** @var RouteHelper */ protected $routeHelper; /** @var WebAuthenticationOptions */ @@ -43,12 +43,12 @@ class LogoutAction implements MiddlewareInterface, AuthenticationEventListenerIn /** * LogoutActionFactory constructor. * @param AuthenticationInterface $authentication - * @param RouteOptionHelper $routeHelper + * @param RouteHelper $routeHelper * @param WebAuthenticationOptions $options */ public function __construct( AuthenticationInterface $authentication, - RouteOptionHelper $routeHelper, + RouteHelper $routeHelper, WebAuthenticationOptions $options ) { $this->authentication = $authentication; @@ -64,7 +64,7 @@ public function __construct( public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface { if (!$this->authentication->hasIdentity()) { - return new RedirectResponse($this->routeHelper->getUri($this->options->getAfterLogoutRoute())); + return new RedirectResponse($this->routeHelper->generateUri($this->options->getAfterLogoutRoute())); } $event = $this->dispatchEvent(AuthenticationEvent::EVENT_BEFORE_LOGOUT, [ 'request' => $request, @@ -81,7 +81,7 @@ public function process(ServerRequestInterface $request, DelegateInterface $dele 'authenticationService' => $this->authentication ]); - $uri = $this->routeHelper->getUri($this->options->getAfterLogoutRoute()); + $uri = $this->routeHelper->generateUri($this->options->getAfterLogoutRoute()); return new RedirectResponse($uri); } } diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 543df43..8789116 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -26,16 +26,7 @@ public function __invoke(): array 'dependencies' => $this->getDependenciesConfig(), 'dot_authentication' => [ - 'web' => [ - 'event_listeners' => [], - - 'login_route' => ['route_name' => 'login'], - 'logout_route' => ['route_name' => 'logout'], - - 'messages_options' => [ - 'messages' => [], - ], - ] + 'web' => [] ] ]; } diff --git a/src/ErrorHandler/UnauthorizedHandler.php b/src/ErrorHandler/UnauthorizedHandler.php index 73b90e6..e5be9d1 100644 --- a/src/ErrorHandler/UnauthorizedHandler.php +++ b/src/ErrorHandler/UnauthorizedHandler.php @@ -19,16 +19,13 @@ use Dot\Authentication\Web\Options\MessagesOptions; use Dot\Authentication\Web\Options\WebAuthenticationOptions; use Dot\FlashMessenger\FlashMessengerInterface; -use Dot\Helpers\Route\RouteOptionHelper; -use Dot\Helpers\Route\UriHelperTrait; +use Dot\Helpers\Route\RouteHelper; use Interop\Http\ServerMiddleware\DelegateInterface; use Interop\Http\ServerMiddleware\MiddlewareInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Message\UriInterface; use Zend\Diactoros\Response\RedirectResponse; use Zend\Diactoros\Uri; -use Zend\Expressive\Helper\UrlHelper; /** * Class UnauthorizedHandler @@ -38,18 +35,14 @@ class UnauthorizedHandler implements MiddlewareInterface, AuthenticationEventLis { use AuthenticationEventListenerTrait; use DispatchAuthenticationEventTrait; - use UriHelperTrait; /** @var AuthenticationInterface */ protected $authenticationService; - /** @var UrlHelper */ - protected $urlHelper; - /** @var WebAuthenticationOptions */ protected $options; - /** @var RouteOptionHelper */ + /** @var RouteHelper */ protected $routeHelper; /** @var FlashMessengerInterface */ @@ -64,13 +57,13 @@ class UnauthorizedHandler implements MiddlewareInterface, AuthenticationEventLis /** * UnauthorizedHandler constructor. * @param AuthenticationInterface $authenticationService - * @param RouteOptionHelper $routeHelper + * @param RouteHelper $routeHelper * @param WebAuthenticationOptions $options * @param FlashMessengerInterface $flashMessenger */ public function __construct( AuthenticationInterface $authenticationService, - RouteOptionHelper $routeHelper, + RouteHelper $routeHelper, WebAuthenticationOptions $options, FlashMessengerInterface $flashMessenger ) { @@ -147,8 +140,8 @@ protected function handleUnauthorizedError( } /** @var Uri $uri */ - $uri = $this->routeHelper->getUri($this->options->getLoginRoute()); - if ($this->areUriEqual($uri, $request->getUri())) { + $uri = $this->routeHelper->generateUri($this->options->getLoginRoute()); + if ($this->routeHelper->uriEquals($uri, $request->getUri())) { throw new RuntimeException( 'Default unauthorized listener has detected that the login route is not authorized either.' . ' This can result in an endless redirect loop. ' . @@ -156,7 +149,7 @@ protected function handleUnauthorizedError( ); } if ($this->options->isEnableWantedUrl()) { - $uri = $this->appendQueryParam( + $uri = $this->routeHelper->appendQueryParam( $uri, $this->options->getWantedUrlName(), $request->getUri()->__toString() @@ -166,18 +159,6 @@ protected function handleUnauthorizedError( return new RedirectResponse($uri); } - protected function getRedirectUri() - { - $loginRoute = $this->options->getLoginRoute(); - $uri = $this->urlHelper->generate([ - $loginRoute['route_name'] ?? '', - $loginRoute['route_params'] ?? [], - $loginRoute['query_params'] ?? [], - $loginRoute['fragment_identifier'], - $loginRoute['options'] ?? [] - ]); - } - /** * @param $error * @return array @@ -231,17 +212,4 @@ public function setDebug(bool $debug) { $this->debug = $debug; } - - /** - * @param UriInterface $uri1 - * @param UriInterface $uri2 - * @return bool - */ - protected function areUriEqual(UriInterface $uri1, UriInterface $uri2): bool - { - return $uri1->getScheme() === $uri2->getScheme() - && $uri1->getHost() === $uri2->getHost() - && $uri1->getPath() === $uri2->getPath() - && $uri1->getPort() === $uri2->getPort(); - } } diff --git a/src/Factory/LoginActionFactory.php b/src/Factory/LoginActionFactory.php index 76ada65..2c9c89c 100644 --- a/src/Factory/LoginActionFactory.php +++ b/src/Factory/LoginActionFactory.php @@ -13,7 +13,7 @@ use Dot\Authentication\Web\Action\LoginAction; use Dot\Authentication\Web\Options\WebAuthenticationOptions; use Dot\FlashMessenger\FlashMessengerInterface; -use Dot\Helpers\Route\RouteOptionHelper; +use Dot\Helpers\Route\RouteHelper; use Psr\Container\ContainerInterface; use Zend\Expressive\Template\TemplateRendererInterface; @@ -37,7 +37,7 @@ public function __invoke(ContainerInterface $container, string $requestedName): $action = new $requestedName( $container->get(AuthenticationInterface::class), $container->get(TemplateRendererInterface::class), - $container->get(RouteOptionHelper::class), + $container->get(RouteHelper::class), $container->get(WebAuthenticationOptions::class), $container->get(FlashMessengerInterface::class) ); diff --git a/src/Factory/LogoutActionFactory.php b/src/Factory/LogoutActionFactory.php index fe6e1bb..a923136 100644 --- a/src/Factory/LogoutActionFactory.php +++ b/src/Factory/LogoutActionFactory.php @@ -12,7 +12,7 @@ use Dot\Authentication\AuthenticationInterface; use Dot\Authentication\Web\Action\LogoutAction; use Dot\Authentication\Web\Options\WebAuthenticationOptions; -use Dot\Helpers\Route\RouteOptionHelper; +use Dot\Helpers\Route\RouteHelper; use Psr\Container\ContainerInterface; /** @@ -31,7 +31,7 @@ public function __invoke(ContainerInterface $container, string $requestedName): /** @var LogoutAction $action */ $action = new $requestedName( $container->get(AuthenticationInterface::class), - $container->get(RouteOptionHelper::class), + $container->get(RouteHelper::class), $container->get(WebAuthenticationOptions::class) ); diff --git a/src/Factory/UnauthorizedHandlerFactory.php b/src/Factory/UnauthorizedHandlerFactory.php index e6fa3ac..0329e2b 100644 --- a/src/Factory/UnauthorizedHandlerFactory.php +++ b/src/Factory/UnauthorizedHandlerFactory.php @@ -13,7 +13,7 @@ use Dot\Authentication\Web\ErrorHandler\UnauthorizedHandler; use Dot\Authentication\Web\Options\WebAuthenticationOptions; use Dot\FlashMessenger\FlashMessengerInterface; -use Dot\Helpers\Route\RouteOptionHelper; +use Dot\Helpers\Route\RouteHelper; use Psr\Container\ContainerInterface; /** @@ -32,7 +32,7 @@ public function __invoke(ContainerInterface $container, string $requestedName): /** @var UnauthorizedHandler $handler */ $handler = new $requestedName( $container->get(AuthenticationInterface::class), - $container->get(RouteOptionHelper::class), + $container->get(RouteHelper::class), $container->get(WebAuthenticationOptions::class), $container->get(FlashMessengerInterface::class) );