src/Bundle/AdminShibbolethBundle/Security/AdminShibbolethGuardAuthenticator.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Bundle\AdminShibbolethBundle\Security;
  3. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  4. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Security\Core\User\UserProviderInterface;
  15. use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
  16. use App\Bundle\AdminShibbolethBundle\Security\User\AdminShibbolethUserProviderInterface;
  17. /**
  18.  * Class AdminShibbolethGuardAuthenticator
  19.  * @package App\Bundle\AdminShibbolethBundle\Security
  20.  */
  21. class AdminShibbolethGuardAuthenticator extends AbstractGuardAuthenticator
  22. {
  23.     /**
  24.      * @var array
  25.      */
  26.     private $config;
  27.     /**
  28.      * @var Router
  29.      */
  30.     private $router;
  31.     /**
  32.      * @var TokenStorageInterface
  33.      */
  34.     private $tokenStorage;
  35.     /**
  36.      * @var string
  37.      */
  38.     private $login_path;
  39.     /**
  40.      * @var string
  41.      */
  42.     private $logout_path;
  43.     /**
  44.      * @var string
  45.      */
  46.     private $login_target;
  47.     /**
  48.      * @var string
  49.      */
  50.     private $session_id;
  51.     /**
  52.      * @var string
  53.      */
  54.     private $username;
  55.     /**
  56.      * @var array
  57.      */
  58.     private $attributes;
  59.     /**
  60.      * ShibbolethGuardAuthenticator constructor.
  61.      * @param array $config
  62.      * @param Router $router
  63.      * @param TokenStorageInterface $tokenStorage
  64.      */
  65.     public function __construct(array $configRouter $routerTokenStorageInterface $tokenStorage)
  66.     {
  67.         $this->config $config;
  68.         $this->router $router;
  69.         $this->tokenStorage $tokenStorage;
  70.         $this->login_path $config['login_path'];
  71.         $this->logout_path $config['logout_path'];
  72.         $this->login_target $config['login_target'];
  73.         $this->session_id $config['session_id'];
  74.         $this->username $config['username'];
  75.         $this->attributes $config['attributes'];
  76.         if(!in_array($this->username$this->attributes))
  77.             throw new InvalidConfigurationException("Shibboleth configuration error : the value of username parameter must be in attributes list parameter");
  78.     }
  79.     /**
  80.      * @param Request $request
  81.      * @return bool
  82.      */
  83.     public function supports(Request $request){
  84.         if (!empty($this->getAttribute($request$this->session_id))) {
  85.             return true;
  86.         }
  87.         return false;
  88.     }
  89.     /**
  90.      * @param Request $request
  91.      * @param AuthenticationException|null $authException
  92.      * @return RedirectResponse
  93.      */
  94.     public function start(Request $requestAuthenticationException $authException null)
  95.     {
  96.         return new RedirectResponse("{$request->getSchemeAndHttpHost()}/".trim($this->login_path'/')."?target=".(empty($this->login_target)? $request->getUri() : "{$request->getSchemeAndHttpHost()}{$this->router->generate($this->login_target)}"));
  97.     }
  98.     /**
  99.      * @param Request $request
  100.      * @return array|null
  101.      */
  102.     public function getCredentials(Request $request)
  103.     {
  104.         $credentials = array();
  105.         $credentials['username'] = $this->getAttribute($request$this->username);
  106.         foreach($this->attributes as $attribute){
  107.             $credentials[$attribute] = $this->getAttribute($request$attribute);
  108.         }
  109.         return $credentials;
  110.     }
  111.     /**
  112.      * @param mixed $credentials
  113.      * @param UserProviderInterface $userProvider
  114.      * @return UserInterface
  115.      */
  116.     public function getUser($credentialsUserProviderInterface $userProvider)
  117.     {
  118.         if(empty($credentials['username']))
  119.             throw new UsernameNotFoundException("The username attribute is empty");
  120.         if($userProvider instanceof ShibbolethUserProviderInterface)
  121.             return $userProvider->loadUser($credentials);
  122.         else if($userProvider instanceof  UserProviderInterface)
  123.             return $userProvider->loadUserByUsername($credentials['username']);
  124.         return null;
  125.     }
  126.     /**
  127.      * @param mixed $credentials
  128.      * @param UserInterface $user
  129.      * @return bool
  130.      */
  131.     public function checkCredentials($credentialsUserInterface $user)
  132.     {
  133.         return true;
  134.     }
  135.     /**
  136.      * @param Request $request
  137.      * @param AuthenticationException $exception
  138.      * @return JsonResponse
  139.      */
  140.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
  141.     {
  142. //        return new JsonResponse(array('message' => $exception->getMessageKey()), Response::HTTP_FORBIDDEN);
  143.         return new JsonResponse(array('message' => "Vous n'avez pas les droits pour accéder à cette application"), Response::HTTP_FORBIDDEN);
  144.     }
  145.     /**
  146.      * @param Request $request
  147.      * @param TokenInterface $token
  148.      * @param string $providerKey
  149.      * @return null
  150.      */
  151.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey)
  152.     {
  153.         return null;
  154.     }
  155.     /**
  156.      * @return bool
  157.      */
  158.     public function supportsRememberMe()
  159.     {
  160.         return false;
  161.     }
  162.     /**
  163.      * @param Request $request
  164.      * @param $name
  165.      * @return mixed
  166.      */
  167.     private function getAttribute(Request $request$name){
  168.         $attributes = array($namestrtoupper($name), "HTTP_".strtoupper($name), "REDIRECT_{$name}");
  169.         foreach($attributes as $attribute)
  170.             if(!empty($request->server->has($attribute))) return $request->server->get($attribute);
  171.     }
  172. }