Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the user friendly error page on authn failure #332

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Comment on lines +52 to +56
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm overriding the default FailureHandler here, allowing custom authentication failure handling.

In our case, we show our own stepup user facing error page. Showing a table with error parameters that can be sent to the support team. Helping them enormously in debugging/audit trailing the failure.

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be strange to inject a controller, and render a response here. But this does exactly what we want this error handler to do. 🤷‍♂️

}
}