<?php
namespace App\Controller;
use App\Lib\Misc\ConfManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use App\Lib\Auth\CASAuthentication;
use App\Lib\Auth\CookieManager;
use App\Lib\Services\ThreeDSPlatform;
class LoginController extends AbstractController
{
private $session;
private $conf;
private $authCtrl;
/**
* LoginController constructor.
* @param SessionInterface $session
*/
public function __construct(SessionInterface $session, ConfManager $conf)
{
$this->session = $session;
$this->conf = $conf;
}
/**
* @Route("/login", name="login")
* @param Request $request
* @return Response
* @throws \Exception
*/
public function index(Request $request)
{
$user = '';
$error = '';
$post = $request->request->all();
$auth = array();
if(isset($post["user"]) && isset($post["password"])) {
/***************************
* For the login, we provide a fake session ID to ensure a cookie can be created.
* When the client receive a DS session, we delete the fake session
*/
$tmpObj = new \stdClass();
$tmpObj->id = uniqid();
$this->session->set("me", $tmpObj);
$clonedSessionId = $tmpObj->id;
unset($tmpObj);
CookieManager::createCookieDirFromId($clonedSessionId);
$this->session->set("credentials",
array(
"user" => $post["user"],
"password" => $post["password"]
)
);
$tenant = $this->getParameter("tenant");
$meUrl = "https://eu1-ifwe.3dexperience.3ds.com/api/users/current";
$casAuthentication = new CASAuthentication($this->session, $this->conf);
$auth = $casAuthentication->doAuthFor($tenant,$meUrl);
if(!empty($auth)) {
$threeDSPlatform = new ThreeDSPlatform($casAuthentication, $this->session, $this->conf);
$me = $threeDSPlatform->getMeInfos();
$this->session->set("me", $me);
//renaming fake session
CookieManager::renameCookieDir($clonedSessionId, $this->session);
return $this->redirectToRoute('main');
}
}
//CookieManager::resetCookie($clonedSession);
return $this->render('login/index.html.twig', [
'user' => $user,
'error' => $error,
]);
}
}