src/Controller/SecurityController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Utilisateur;
  4. use Exception;
  5. use Firebase\JWT\JWT;
  6. use Google_Client;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  14. class SecurityController extends AbstractController
  15. {
  16. private $client;
  17. private $email = null;
  18. /**
  19. * @Route("/login", name="app_login")
  20. */
  21. public function login(AuthenticationUtils $authenticationUtils, Request $request): Response
  22. {
  23. $user = $this->get('security.token_storage')->getToken()->getUser();
  24. $this->client = new Google_Client();
  25. $rurl = ($request->getHost() == 'ideodash.ma' or $request->getHost() == 'ideodash.com' or $request->getHost() == "dev.ideodash.com") ? 'https://' . $_SERVER["SERVER_NAME"] . '/googlesignin' : 'http://' . $_SERVER["SERVER_NAME"] . '/googlesignin';
  26. $this->client->setAuthConfig('credentialsignin.json');
  27. $this->client->setRedirectUri($rurl);
  28. $this->client->setAccessType('offline');
  29. $this->client->setScopes("email");
  30. $auth_url = urldecode($this->client->createAuthUrl());
  31. if ($this->email) {
  32. $user = $this->getDoctrine()->getRepository(Utilisateur::class)->findOneByUsername($this->email);
  33. $this->get('security.token_storage')->getToken()->setUser($user);
  34. return $this->redirectToRoute('home');
  35. }
  36. if ($user != 'anon.') {
  37. return $this->redirectToRoute('home');
  38. }
  39. // get the login error if there is one
  40. $error = $authenticationUtils->getLastAuthenticationError();
  41. // last username entered by the user
  42. $lastUsername = $authenticationUtils->getLastUsername();
  43. return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'auth_url' => $auth_url]);
  44. }
  45. /**
  46. *
  47. * @Route("/googlesignin", name="googlesignin")
  48. */
  49. public function googlesigninAction(Request $request)
  50. {
  51. JWT::$leeway = 240;
  52. $code = isset($_GET['code']) ? $_GET['code'] : NULL;
  53. $rurl = ($request->getHost() == 'ideodash.ma' or $request->getHost() == 'ideodash.com' or $request->getHost() == "dev.ideodash.com") ? 'https://' . $_SERVER["SERVER_NAME"] . '/googlesignin' : 'http://' . $_SERVER["SERVER_NAME"] . '/googlesignin';
  54. $client = new Google_Client();
  55. $client->setAuthConfig('credentialsignin.json');
  56. $client->setRedirectUri($rurl);
  57. $client->setAccessType('offline');
  58. $client->setScopes("email");
  59. if (isset($code)) {
  60. try {
  61. $token = $client->fetchAccessTokenWithAuthCode($code);
  62. $client->setAccessToken($token);
  63. } catch (Exception $e) {
  64. echo $e->getMessage();
  65. }
  66. try {
  67. $pay_load = $client->verifyIdToken();
  68. } catch (Exception $e) {
  69. echo $e->getMessage();
  70. }
  71. } else {
  72. $pay_load = null;
  73. }
  74. if (isset($pay_load)) {
  75. $this->email = $pay_load['email'];
  76. $user = $this->getDoctrine()->getRepository(Utilisateur::class)->findOneByUsername($pay_load['email']);
  77. $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
  78. $this->get('security.token_storage')->setToken($token);
  79. $this->get('session')->set('_security_main', serialize($token));
  80. return $this->redirectToRoute('home');
  81. }
  82. return $this->redirectToRoute('home');
  83. }
  84. /**
  85. * @Route("/logout", name="app_logout")
  86. */
  87. public function logout()
  88. {
  89. throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  90. }
  91. }