<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller;
use App\Entity\LoginLog;
use App\Repository\UserRepository;
use App\Services\OktaApiService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
/**
* Controller used to manage the application security.
* See https://symfony.com/doc/current/cookbook/security/form_login_setup.html.
*
* @author Ryan Weaver <weaverryan@gmail.com>
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class SecurityController extends AbstractController
{
private SessionInterface $session;
private OktaApiService $okta;
private UserRepository $userRepository;
private EntityManagerInterface $em;
private TokenStorageInterface $tokenStorage;
public function __construct(
SessionInterface $session,
OktaApiService $okta,
UserRepository $UserRepository,
EntityManagerInterface $em,
TokenStorageInterface $tokenStorage
) {
$this->session = $session;
$this->okta = $okta;
$this->userRepository = $UserRepository;
$this->em = $em;
$this->tokenStorage = $tokenStorage;
}
/**
* @Route("/errorlogin", name="security_errorlogin")
*/
public function errorlogin(AuthenticationUtils $helper): Response
{
return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
'last_username' => '',
'logoutMessage' => '',
// last authentication error (if any)
'error' => $helper->getLastAuthenticationError(),
'errorMessage' => 'You do not have access to this application. Please request access to Baker Hughes team',
'legacy' => 'N', 'myidurl' => $this->okta->buildAuthorizeUrl(),
]);
}
private function LoginLog($user, $username, $action, $attuale, $request) {
$ip = $request->getClientIp();
$loginLog = new LoginLog();
// $location = []; //unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $ip)); //$_SERVER['REMOTE_ADDR']);
$_tmp = file_get_contents('https://ipinfo.io/' . $ip . '/json');
if ((bool) $_tmp) {
$location = json_decode($_tmp, true);
if (json_last_error() !== JSON_ERROR_NONE || !is_array($location)) {
$location = [];
}
} else {
$location = [];
}
$loginLog->setUser($user);
$loginLog->setUsername($username);
$loginLog->setDataLogin(new \DateTime());
$loginLog->setAttuale($attuale);
$loginLog->setAAction($action);
$loginLog->setAIp($ip);
$loginLog->setAAgent($request->headers->get('User-Agent'));
$loginLog->setGeoLoc($location);
$this->em->persist($loginLog);
$this->em->flush();
}
/**
* @Route("/logoutpage", name="security_logoutpage")
*/
public function logoutpage(Request $request, AuthenticationUtils $helper): Response
{
$user = $this->getUser();
if ($user != null) {
$this->LoginLog($user, $user->getUsername(), 9003, 0, $request);
}
$this->session->set('userislocked', 0);
$this->tokenStorage->setToken(null);
$this->session->set('_security_main', null);
return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
'last_username' => '',
// last authentication error (if any)
'error' => $helper->getLastAuthenticationError(),
'logoutMessage' => 'Logout successful',
'errorMessage' => '',
'legacy' => 'N', 'myidurl' => $this->okta->buildAuthorizeUrl(),
]);
}
/**
* @Route("/login", name="security_login")
*/
public function login(AuthenticationUtils $helper): Response
{
if ($this->getParameter('app.legacy_login') === 'Y') {
return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
'last_username' => $helper->getLastUsername(),
// last authentication error (if any)
'logoutMessage' => '',
'error' => $helper->getLastAuthenticationError(),
'errorMessage' => '',
'legacy' => 'Y', 'myidurl' => ''
]);
}
else {
return $this->redirect($this->okta->buildAuthorizeUrl());
}
/* $token = $this->okta->authorizeUser();
if (!$token) {
// return $this->redirectToRoute('home');
return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
'last_username' => $helper->getLastUsername(),
// last authentication error (if any)
'error' => $helper->getLastAuthenticationError(),
'legacy' => 'N',
'myidurl' => $this->getParameter('app.okta_app_url')
]);
}
$email = $token->email;
$username = $token->username;
$user = $this->userRepository->loadUserByUsername($username);
if (! $user) {
return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
'last_username' => $helper->getLastUsername(),
// last authentication error (if any)
'error' => 'Unable to login',
'legacy' => 'N', 'myidurl' => $this->getParameter('app.okta_app_url')]);
}
// Manually authenticate the user
$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('welcome_page');
*/
/*
return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
'last_username' => $helper->getLastUsername(),
// last authentication error (if any)
'error' => $helper->getLastAuthenticationError(),
]);
* */
}
/**
* This is the route the user can use to logout.
*
* But, this will never be executed. Symfony will intercept this first
* and handle the logout automatically. See logout in app/config/security.yml
*
* @Route("/logout", name="security_logout")
*/
public function logout(): void
{
throw new \Exception('This should never be reached!');
}
}