src/Security/AzureSsoAdminAuthenticator.php line 33
<?phpnamespace App\Security;use App\Enum\LoginErrorEnum;use App\Repository\UserRepository;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\RouterInterface;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Exception\AuthenticationException;use Symfony\Component\Security\Core\Exception\UserNotFoundException;use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;use Symfony\Component\Security\Http\Authenticator\Passport\Passport;use TheNetworg\OAuth2\Client\Provider\AzureResourceOwner;class AzureSsoAdminAuthenticator extends AbstractAuthenticator{public function __construct(private readonly UserRepository $userRepository,private readonly RouterInterface $router,) {}public function supports(Request $request): ?bool{return str_starts_with($request->getPathInfo(), '/admin');}public function authenticate(Request $request): Passport{/** @var AzureResourceOwner $azure */$azure = $request->getSession()->get('azure');if (!$azure instanceof AzureResourceOwner) {throw new UserNotFoundException();}$user = $this->userRepository->findOneBy(['email' => $azure->claim('email')]);if (!$user) {throw new UserNotFoundException('User not mapped locally');}return new Passport(new UserBadge($azure->claim('email'),function ($userIdentifier) use ($user) {return $user;}),new CustomCredentials(static function ($credentials, $user) {return true;},$azure));}public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response{// TODO: Implement onAuthenticationSuccess() method.return null;}public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response{if ($exception->getPrevious()?->getMessage() === 'User not mapped locally') {return new RedirectResponse($this->router->generate('app_login_error', ['id' => LoginErrorEnum::NotLocal->value]));}return new RedirectResponse($this->router->generate('app_login_error', ['id' => -1]));}}