2015-11-23 13:19:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gist\Security;
|
|
|
|
|
|
|
|
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
|
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
|
|
|
|
|
|
|
/**
|
2016-11-13 00:44:23 +01:00
|
|
|
* Class AuthenticationListener.
|
|
|
|
*
|
2015-11-23 13:19:13 +01:00
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class AuthenticationListener implements ListenerInterface
|
|
|
|
{
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* @var TokenStorageInterface
|
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
protected $tokenStorage;
|
|
|
|
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* @var AuthenticationManagerInterface
|
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
protected $authenticationManager;
|
|
|
|
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* __construct.
|
|
|
|
*
|
|
|
|
* @param TokenStorageInterface $tokenStorage
|
|
|
|
* @param AuthenticationManagerInterface $authenticationManager
|
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager)
|
|
|
|
{
|
|
|
|
$this->tokenStorage = $tokenStorage;
|
|
|
|
$this->authenticationManager = $authenticationManager;
|
|
|
|
}
|
|
|
|
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* @param GetResponseEvent $event
|
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
public function handle(GetResponseEvent $event)
|
|
|
|
{
|
2016-11-13 00:44:23 +01:00
|
|
|
$request = $event->getRequest();
|
2015-11-23 13:19:13 +01:00
|
|
|
$username = $request->get('_username');
|
|
|
|
$password = $request->get('_password');
|
|
|
|
|
|
|
|
if (!empty($username)) {
|
|
|
|
$token = new UsernamePasswordToken($username, $password, 'default');
|
2016-11-13 00:44:23 +01:00
|
|
|
|
2015-11-23 13:19:13 +01:00
|
|
|
try {
|
|
|
|
$authToken = $this->authenticationManager->authenticate($token);
|
|
|
|
$this->tokenStorage->setToken($token);
|
|
|
|
|
|
|
|
return;
|
|
|
|
} catch (AuthenticationException $failed) {
|
|
|
|
$this->tokenStorage->setToken(null);
|
|
|
|
|
|
|
|
return;
|
2016-11-13 00:44:23 +01:00
|
|
|
}
|
2015-11-23 13:19:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|