From 66d2a36983328a490e31cd2646e9255c488d4f04 Mon Sep 17 00:00:00 2001 From: Michiel Kodde Date: Wed, 18 Sep 2024 11:53:00 +0200 Subject: [PATCH] Show the user friendly error page on authn failure See: https://www.pivotaltracker.com/story/show/188230772 --- config/services.yaml | 6 ++ .../Authentication/Handler/FailureHandler.php | 59 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/Surfnet/StepupRa/RaBundle/Security/Authentication/Handler/FailureHandler.php diff --git a/config/services.yaml b/config/services.yaml index 7f782716..ad885408 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -48,3 +48,9 @@ services: Surfnet\StepupRa\RaBundle\Security\Authentication\AuthenticatedSessionStateHandler: alias: ra.security.authentication.session.session_storage + + Surfnet\SamlBundle\Security\Authentication\Handler\FailureHandler: + class: Surfnet\StepupRa\RaBundle\Security\Authentication\Handler\FailureHandler + public: false + arguments: + $exceptionController: '@Surfnet\StepupRa\RaBundle\Controller\ExceptionController' diff --git a/src/Surfnet/StepupRa/RaBundle/Security/Authentication/Handler/FailureHandler.php b/src/Surfnet/StepupRa/RaBundle/Security/Authentication/Handler/FailureHandler.php new file mode 100644 index 00000000..0765a80d --- /dev/null +++ b/src/Surfnet/StepupRa/RaBundle/Security/Authentication/Handler/FailureHandler.php @@ -0,0 +1,59 @@ + $options + */ + public function __construct( + HttpKernelInterface $httpKernel, + HttpUtils $httpUtils, + ExceptionController $exceptionController, + array $options = [], + ?LoggerInterface $logger = null, + ) { + parent::__construct($httpKernel, $httpUtils, $options, $logger); + $this->exceptionController = $exceptionController; + } + + public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response + { + $message = sprintf( + 'Authentication failure: %s: "%s"', + $exception->getMessageKey(), + $exception->getMessage(), + ); + $this->logger->notice($message); + // The exception controller is used to show the failed authentication + return $this->exceptionController->show($request, $exception); + } +}