Skip to content

Commit

Permalink
Show the user friendly error page on authn failure
Browse files Browse the repository at this point in the history
  • Loading branch information
MKodde committed Sep 23, 2024
1 parent 26b513b commit 7498ed4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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
{
/**
* @param array<string, mixed> $options
*/
public function __construct(
HttpKernelInterface $httpKernel,
HttpUtils $httpUtils,
private readonly ExceptionController $exceptionController,
array $options = [],
?LoggerInterface $logger = null,
) {
parent::__construct($httpKernel, $httpUtils, $options, $logger);
}

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);
}
}

0 comments on commit 7498ed4

Please sign in to comment.