-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show the user friendly error page on authn failure
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Surfnet/StepupRa/RaBundle/Security/Authentication/Handler/FailureHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* Copyright 2023 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Surfnet\StepupRa\RaBundle\Security\Authentication\Handler; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Surfnet\StepupRa\RaBundle\Controller\ExceptionController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler; | ||
use Symfony\Component\Security\Http\HttpUtils; | ||
|
||
class FailureHandler extends DefaultAuthenticationFailureHandler | ||
{ | ||
private ExceptionController $exceptionController; | ||
|
||
/** | ||
* @param array<string, mixed> $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); | ||
} | ||
} |