src/Controller/BackendBundle/OAuth2Controller.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Controller\BackendBundle;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Session\Session;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  15. use App\BackendBundle\Helper\OAuth2Helper;
  16. use App\BackendBundle\Helper\SiteTitleHelper;
  17. use App\BackendBundle\Security\UserManager\UserNormalProvider;
  18. use App\Entity\User;
  19. class OAuth2Controller extends AbstractController {
  20.     private AuthenticationManagerInterface $authManager;
  21.     private CsrfTokenManagerInterface $csrfTokenManager;
  22.     private EntityManagerInterface $em;
  23.     private OAuth2Helper $oauth2Helper;
  24.     private TokenStorageInterface $tokenStorage;
  25.     private UserNormalProvider $userNormalProvider;
  26.     private UserPasswordHasherInterface $userPasswordHasher;
  27.     private SessionInterface $session;
  28.     private SiteTitleHelper $siteTitleHelper;
  29.     private $sessionEmail 'oauth2_session_email';
  30.     /* needs to match with \App\BackendBundle\Security\Authenticator\OAuth2Authenticator */
  31.     private $sessionOAuthKey 'oauth2_session_username';
  32.     public function __construct(CsrfTokenManagerInterface $csrfTokenManagerEntityManagerInterface $em,
  33.             SiteTitleHelper $sitehelperUserNormalProvider $userNormalProviderUserPasswordHasherInterface $userPasswordHasher,
  34.             AuthenticationManagerInterface $authManagerTokenStorageInterface $tokenStorageOAuth2Helper $oauth2Helper,
  35.             SessionInterface $session) {
  36.         $this->authManager $authManager;
  37.         $this->csrfTokenManager $csrfTokenManager;
  38.         $this->em $em;
  39.         $this->oauth2Helper $oauth2Helper;
  40.         $this->session $session;
  41.         $this->siteTitleHelper $sitehelper;
  42.         $this->tokenStorage $tokenStorage;
  43.         $this->userNormalProvider $userNormalProvider;
  44.         $this->userPasswordHasher $userPasswordHasher;
  45.     }
  46.     /**
  47.      * @return \Symfony\Component\HttpFoundation\Response
  48.      * @Route("/oauthlogin", name="oauth2_login", defaults={"title": "Berufsreise Login", "description": "Berufsreise Login"}) 
  49.      */
  50.     public function oauthLoginAction(Request $requestSession $session) {
  51.         $this->siteTitleHelper->setTitleDescription($request);
  52.         $username '';
  53.         if ($session->has($this->sessionEmail)) {
  54.             $username $session->get($this->sessionEmail'');
  55.         }
  56.         $error false;
  57.         if ($session->has(Security::AUTHENTICATION_ERROR)) {
  58.             $error true;
  59.         }
  60.         return $this->render('@frontend/oauth2/login.html.twig', array(
  61.                     'username' => $username,
  62.                     'error' => $error
  63.         ));
  64.     }
  65.     private function processForm(Request $request) {
  66.         /* get data from submitted form */
  67.         $csrfToken $request->request->get("_csrf_token");
  68.         $username $request->request->get('username');
  69.         $password $request->request->get('password');
  70.         $isCsrfTokenValid $this->isCsrfTokenValid('oauth2_authenticate'$csrfToken);
  71.         if (!$isCsrfTokenValid) {
  72.             return null;
  73.         }
  74.         /* try to get user from submitted data */
  75.         $user $this->processUserData($username$password);
  76.         if (empty($user)) {
  77.             return null;
  78.         }
  79.         $this->session->set('symfony_username'$user->getUsername());
  80.         $parameters $request->query->all();
  81.         $url $this->generateUrl('oauth2_authorize'$parameters);
  82.         return new RedirectResponse($url);
  83.     }
  84.     private function processUserData($username$password) {
  85.         /* @var $user User */
  86.         $user $this->userNormalProvider->loadUserByUsername($username);
  87.         if (empty($user)) {
  88.             return null;
  89.         }
  90.         if (!$user->getIsActive()) {
  91.             return null;
  92.         }
  93.         $isPasswordValid $this->userPasswordHasher->isPasswordValid($user$password);
  94.         if (!$isPasswordValid) {
  95.             return null;
  96.         }
  97.         return $user;
  98.     }
  99.     /**
  100.      * @return \Symfony\Component\HttpFoundation\Response
  101.      * @Route("/oauthlogout", name="oauth2_logout", defaults={"title": "", "description": ""}) 
  102.      */
  103.     public function oauthLogoutAction(Request $request) {
  104.         $user $this->getUser();
  105.         if (empty($user)) {
  106.             return $this->getLogoutRedirect($request);
  107.         }
  108.         $userName $user->getUsername();
  109.         $this->oauth2Helper->revokeTokens($userName);
  110.         $this->session->remove($this->sessionOAuthKey);
  111.         return $this->getLogoutRedirect($request);
  112.     }
  113.     private function getLogoutRedirect(Request $request) {
  114.         $redirectURI $request->query->get('post_logout_redirect_uri');
  115.         if (empty($redirectURI)) {
  116.             $redirectURI 'https://www.berufsreise.at';
  117.         }
  118.         $url urldecode($redirectURI);
  119.         return $this->redirect($url);
  120.     }
  121. }