2015-11-23 13:19:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Gist\Security;
|
|
|
|
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
|
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
|
|
|
use Gist\Service\UserProvider;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
|
|
|
|
|
|
|
/**
|
2016-11-13 00:44:23 +01:00
|
|
|
* Class AuthenticationProvider.
|
|
|
|
*
|
2015-11-23 13:19:13 +01:00
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class AuthenticationProvider implements AuthenticationProviderInterface
|
|
|
|
{
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* @var UserProvider
|
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
protected $userProvider;
|
|
|
|
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* __construct.
|
|
|
|
*
|
|
|
|
* @param UserProvider $userProvider
|
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
public function __construct(UserProvider $userProvider)
|
|
|
|
{
|
|
|
|
$this->userProvider = $userProvider;
|
|
|
|
}
|
|
|
|
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* Authenticates.
|
|
|
|
*
|
|
|
|
* @param TokenInterface $token
|
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
public function authenticate(TokenInterface $token)
|
|
|
|
{
|
|
|
|
$user = $this->userProvider->loadUserByUsername($token->getUser());
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
$isValid = $this->userProvider->getEncoder()->isPasswordValid(
|
|
|
|
$user->getPassword(),
|
|
|
|
$token->getCredentials(),
|
2016-11-13 00:44:23 +01:00
|
|
|
$user->getSalt()
|
2015-11-23 13:19:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!$isValid) {
|
|
|
|
throw new AuthenticationException('Authentication failed.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new AuthenticationException('Authentication failed.');
|
|
|
|
}
|
|
|
|
|
2016-11-13 00:44:23 +01:00
|
|
|
/**
|
|
|
|
* Returns if the token instance is supported.
|
|
|
|
*
|
|
|
|
* @param TokenInterface $token
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-11-23 13:19:13 +01:00
|
|
|
public function supports(TokenInterface $token)
|
|
|
|
{
|
|
|
|
return $token instanceof UsernamePasswordToken;
|
|
|
|
}
|
|
|
|
}
|