src/Security/AzureSsoAdminAuthenticator.php line 39

  1. <?php
  2. namespace App\Security;
  3. use App\Enum\LoginErrorEnum;
  4. use App\Repository\UserRepository;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  11. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  12. use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  16. use TheNetworg\OAuth2\Client\Provider\AzureResourceOwner;
  17. class AzureSsoAdminAuthenticator extends AbstractAuthenticator
  18. {
  19.     public function __construct(
  20.         private readonly UserRepository $userRepository,
  21.         private readonly RouterInterface $router,
  22.     ) {
  23.     }
  24.     public function supports(Request $request): ?bool
  25.     {
  26.         return str_starts_with($request->getPathInfo(), '/admin');
  27.     }
  28.     public function authenticate(Request $request): Passport
  29.     {
  30.         /** @var AzureResourceOwner $azure */
  31.         $azure $request->getSession()->get('azure');
  32.         if (!$azure instanceof AzureResourceOwner) {
  33.             throw new UserNotFoundException();
  34.         }
  35.         $user $this->userRepository->findOneBy(['email' => $azure->claim('email')]);
  36.         if (!$user) {
  37.             throw new UserNotFoundException('User not mapped locally');
  38.         }
  39.         return new Passport(
  40.             new UserBadge(
  41.                 $azure->claim('email'),
  42.                 function ($userIdentifier) use ($user) {
  43.                     return $user;
  44.                 }
  45.             ),
  46.             new CustomCredentials(
  47.                 static function ($credentials$user) {
  48.                     return true;
  49.                 },
  50.                 $azure
  51.             )
  52.         );
  53.     }
  54.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  55.     {
  56.         // TODO: Implement onAuthenticationSuccess() method.
  57.         return null;
  58.     }
  59.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  60.     {
  61.         if ($exception->getPrevious()?->getMessage() === 'User not mapped locally') {
  62.             return new RedirectResponse(
  63.                 $this->router->generate('app_login_error', ['id' => LoginErrorEnum::NotLocal->value])
  64.             );
  65.         }
  66.         return new RedirectResponse(
  67.             $this->router->generate('app_login_error', ['id' => -1])
  68.         );
  69.     }
  70. }