* symfony 6.3: removed sensio/framework-extra-bundle * symfony 6.3: update * Symfony 6.3.1 update * chore: composer up * symfony 6.4 update * cs: php-code-fixer update * fix composer.lock * add php-http required dependencies * Fix Stan and CS --------- Co-authored-by: Jérôme Tanghe <jerome.tanghe@les-tilleuls.coop>
40 lines
1.5 KiB
PHP
40 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Security\EntryPoint;
|
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
|
use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException;
|
|
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
|
|
|
|
use function Symfony\Component\String\u;
|
|
|
|
/**
|
|
* Simple entry point to prevent redirecting to the login page in case of a
|
|
* JSON API call to a API Platform endpoint.
|
|
*
|
|
* @see ProductSwitchProcessorTest::testProductSwitchProcessorUnauthorizedFailure
|
|
*/
|
|
class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
|
|
{
|
|
public function __construct(
|
|
private readonly UrlGeneratorInterface $urlGenerator,
|
|
) {
|
|
}
|
|
|
|
public function start(Request $request, ?AuthenticationException $authException = null): RedirectResponse
|
|
{
|
|
/** @var string $route */
|
|
$route = $request->attributes->get('_route');
|
|
if ($authException instanceof InsufficientAuthenticationException && u($route)->startsWith('_api_')) {
|
|
throw new UnauthorizedHttpException('', $authException->getMessage(), $authException);
|
|
}
|
|
|
|
return new RedirectResponse($this->urlGenerator->generate('app_login'));
|
|
}
|
|
}
|