src/Common/Controller/SecurityController.php line 14

Open in your IDE?
  1. <?php
  2. namespace PaperKite\Common\Controller;
  3. use Lightbulb\Symfony\Exception\ServiceUnavailableException;
  4. use LogicException;
  5. use OneLogin\Saml2\Error;
  6. use OneLogin\Saml2\ValidationError;
  7. use PaperKite\EmployeeApi\Service\EmployeeAuthenticationService;
  8. use PaperKite\EmployeeApi\Service\SamlService;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
  17. class SecurityController extends AbstractController
  18. {
  19.     #[Route(path'/login'name'security_login')]
  20.     public function login(AuthenticationUtils $authenticationUtilsSamlService $samlService): Response
  21.     {
  22.         if ($this->getUser()) {
  23.             return $this->redirectToRoute('admin_dashboard');
  24.         }
  25.         // get the login error if there is one
  26.         $error $authenticationUtils->getLastAuthenticationError();
  27.         // last username entered by the user
  28.         $lastUsername $authenticationUtils->getLastUsername();
  29.         return $this->render('@EasyAdmin/page/login.html.twig', [
  30.             'last_username' => $lastUsername,
  31.             'error' => $error,
  32.             'csrf_token_intention' => 'authenticate',
  33.             'page_title' => 'Connexion',
  34.             'target_path' => $this->generateUrl('admin_dashboard'),
  35.             'username_label' => 'Username',
  36.             'password_label' => 'Password',
  37.             'username_parameter' => 'username',
  38.             'password_parameter' => 'password',
  39.             'forgot_password_enabled' => true,
  40.             'forgot_password_path' => $samlService->getSsoLoginUrl(),
  41.             'forgot_password_label' => 'SSO Login',
  42.         ]);
  43.     }
  44.     /**
  45.      * @throws ValidationError
  46.      * @throws ServiceUnavailableException
  47.      * @throws Error
  48.      */
  49.     #[Route(name'employee_sso_login_check'methods: ['POST'])]
  50.     public function postSsoLoginToken(Request $requestSamlService $samlServiceEmployeeAuthenticationService $employeeAuthenticationServiceTokenStorageInterface $tokenStorage): RedirectResponse
  51.     {
  52.         $employee $samlService->handleSamlToken();
  53.         if (str_contains($request->get('RelayState'), 'employee-api')) {
  54.             $authCode $employeeAuthenticationService->setAuthCode($employee);
  55.             return new RedirectResponse($this->getParameter('app_url') . $this->getParameter('app_sso_login_route') . '?authCode=' $authCode);
  56.         } else {
  57.             // Manually authenticate the user for web interface
  58.             $token = new PostAuthenticationToken($employee'main'$employee->getRoles());
  59.             $tokenStorage->setToken($token);
  60.             return new RedirectResponse($this->generateUrl('admin_dashboard'));
  61.         }
  62.     }
  63.     #[Route(path'/logout'name'security_logout')]
  64.     public function logout()
  65.     {
  66.         throw new LogicException('This should never be reached!');
  67.     }
  68. }