<?php
namespace App\Controller;
use App\Entity\Utilisateur;
use Exception;
use Firebase\JWT\JWT;
use Google_Client;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class SecurityController extends AbstractController
{
private $client;
private $email = null;
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils, Request $request): Response
{
$user = $this->get('security.token_storage')->getToken()->getUser();
$this->client = new Google_Client();
$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';
$this->client->setAuthConfig('credentialsignin.json');
$this->client->setRedirectUri($rurl);
$this->client->setAccessType('offline');
$this->client->setScopes("email");
$auth_url = urldecode($this->client->createAuthUrl());
if ($this->email) {
$user = $this->getDoctrine()->getRepository(Utilisateur::class)->findOneByUsername($this->email);
$this->get('security.token_storage')->getToken()->setUser($user);
return $this->redirectToRoute('home');
}
if ($user != 'anon.') {
return $this->redirectToRoute('home');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'auth_url' => $auth_url]);
}
/**
*
* @Route("/googlesignin", name="googlesignin")
*/
public function googlesigninAction(Request $request)
{
JWT::$leeway = 240;
$code = isset($_GET['code']) ? $_GET['code'] : NULL;
$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';
$client = new Google_Client();
$client->setAuthConfig('credentialsignin.json');
$client->setRedirectUri($rurl);
$client->setAccessType('offline');
$client->setScopes("email");
if (isset($code)) {
try {
$token = $client->fetchAccessTokenWithAuthCode($code);
$client->setAccessToken($token);
} catch (Exception $e) {
echo $e->getMessage();
}
try {
$pay_load = $client->verifyIdToken();
} catch (Exception $e) {
echo $e->getMessage();
}
} else {
$pay_load = null;
}
if (isset($pay_load)) {
$this->email = $pay_load['email'];
$user = $this->getDoctrine()->getRepository(Utilisateur::class)->findOneByUsername($pay_load['email']);
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
return $this->redirectToRoute('home');
}
return $this->redirectToRoute('home');
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}