welcome back to dyb-tech
This commit is contained in:
+158
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Provider;
|
||||
|
||||
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent;
|
||||
use Lexik\Bundle\JWTAuthenticationBundle\Events;
|
||||
use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailureException;
|
||||
use Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Token\JWTUserToken;
|
||||
use Lexik\Bundle\JWTAuthenticationBundle\Security\Authenticator\JWTAuthenticator;
|
||||
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManagerInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* JWTProvider.
|
||||
*
|
||||
* @author Nicolas Cabot <n.cabot@lexik.fr>
|
||||
*
|
||||
* @deprecated since 2.0, will be removed in 3.0. See
|
||||
* {@link JWTAuthenticator} instead
|
||||
*/
|
||||
class JWTProvider implements AuthenticationProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var UserProviderInterface
|
||||
*/
|
||||
protected $userProvider;
|
||||
|
||||
/**
|
||||
* @var JWTManagerInterface
|
||||
*/
|
||||
protected $jwtManager;
|
||||
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $userIdentityField;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $userIdClaim;
|
||||
|
||||
/**
|
||||
* @param string $userIdClaim
|
||||
*/
|
||||
public function __construct(
|
||||
UserProviderInterface $userProvider,
|
||||
JWTManagerInterface $jwtManager,
|
||||
EventDispatcherInterface $dispatcher,
|
||||
$userIdClaim
|
||||
) {
|
||||
@trigger_error(sprintf('The "%s" class is deprecated since version 2.0 and will be removed in 3.0. See "%s" instead.', self::class, JWTTokenAuthenticator::class), E_USER_DEPRECATED);
|
||||
|
||||
$this->userProvider = $userProvider;
|
||||
$this->jwtManager = $jwtManager;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->userIdentityField = 'username';
|
||||
$this->userIdClaim = $userIdClaim;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function authenticate(TokenInterface $token)
|
||||
{
|
||||
try {
|
||||
if (!$payload = $this->jwtManager->decode($token)) {
|
||||
throw $this->createAuthenticationException();
|
||||
}
|
||||
} catch (JWTDecodeFailureException $e) {
|
||||
throw $this->createAuthenticationException($e);
|
||||
}
|
||||
|
||||
$user = $this->getUserFromPayload($payload);
|
||||
|
||||
$authToken = new JWTUserToken($user->getRoles());
|
||||
$authToken->setUser($user);
|
||||
$authToken->setRawToken($token->getCredentials());
|
||||
|
||||
$event = new JWTAuthenticatedEvent($payload, $authToken);
|
||||
$this->dispatcher->dispatch($event, Events::JWT_AUTHENTICATED);
|
||||
|
||||
return $authToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load user from payload, using username by default.
|
||||
* Override this to load by another property.
|
||||
*
|
||||
* @return UserInterface
|
||||
*/
|
||||
protected function getUserFromPayload(array $payload)
|
||||
{
|
||||
if (!isset($payload[$this->userIdClaim])) {
|
||||
throw $this->createAuthenticationException();
|
||||
}
|
||||
|
||||
if (method_exists($this->userProvider, 'loadUserByIdentifier')) {
|
||||
return $this->userProvider->loadUserByIdentifier($payload[$this->userIdClaim]);
|
||||
}
|
||||
|
||||
return $this->userProvider->loadUserByUsername($payload[$this->userIdClaim]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports(TokenInterface $token)
|
||||
{
|
||||
return $token instanceof JWTUserToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUserIdentityField()
|
||||
{
|
||||
return $this->userIdentityField;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $userIdentityField
|
||||
*/
|
||||
public function setUserIdentityField($userIdentityField)
|
||||
{
|
||||
$this->userIdentityField = $userIdentityField;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUserIdClaim()
|
||||
{
|
||||
return $this->userIdClaim;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param JWTDecodeFailureException $previous
|
||||
*
|
||||
* @return AuthenticationException
|
||||
*/
|
||||
private function createAuthenticationException(JWTDecodeFailureException $previous = null)
|
||||
{
|
||||
$message = (null === $previous) ? 'Invalid JWT Token' : $previous->getMessage();
|
||||
|
||||
return new AuthenticationException($message, 401, $previous);
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Token;
|
||||
|
||||
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
|
||||
|
||||
if (interface_exists(GuardTokenInterface::class)) {
|
||||
/**
|
||||
* Compatibility layer ensuring the guard token interface is applied when available.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
abstract class JWTCompatUserToken extends AbstractToken implements GuardTokenInterface
|
||||
{
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
abstract class JWTCompatUserToken extends AbstractToken
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JWTUserToken.
|
||||
*
|
||||
* @author Nicolas Cabot <n.cabot@lexik.fr>
|
||||
*/
|
||||
class JWTUserToken extends JWTCompatUserToken
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $rawToken;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $providerKey;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $roles = [], UserInterface $user = null, $rawToken = null, $firewallName = null)
|
||||
{
|
||||
parent::__construct($roles);
|
||||
|
||||
if ($user) {
|
||||
$this->setUser($user);
|
||||
}
|
||||
|
||||
$this->setRawToken($rawToken);
|
||||
|
||||
if (method_exists($this, 'setAuthenticated')) {
|
||||
$this->setAuthenticated(true);
|
||||
}
|
||||
|
||||
$this->providerKey = $firewallName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $rawToken
|
||||
*/
|
||||
public function setRawToken($rawToken)
|
||||
{
|
||||
$this->rawToken = $rawToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCredentials()
|
||||
{
|
||||
return $this->rawToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 2.10, use getFirewallName() instead
|
||||
*/
|
||||
public function getProviderKey()
|
||||
{
|
||||
@trigger_error(sprintf('The "%s" method is deprecated since version 2.10 and will be removed in 3.0. Use "%s::getFirewallName()" instead.', __METHOD__, self::class), E_USER_DEPRECATED);
|
||||
|
||||
return $this->getFirewallName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFirewallName()
|
||||
{
|
||||
return $this->providerKey;
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Token;
|
||||
|
||||
use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
|
||||
|
||||
/**
|
||||
* PreAuthenticationJWTUserToken.
|
||||
*
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
*/
|
||||
final class PreAuthenticationJWTUserToken extends PreAuthenticationGuardToken implements PreAuthenticationJWTUserTokenInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $rawToken;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $payload;
|
||||
|
||||
/**
|
||||
* @param string $rawToken
|
||||
*/
|
||||
public function __construct($rawToken)
|
||||
{
|
||||
$this->rawToken = $rawToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCredentials()
|
||||
{
|
||||
return $this->rawToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setPayload(array $payload)
|
||||
{
|
||||
$this->payload = $payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPayload()
|
||||
{
|
||||
return $this->payload;
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Token;
|
||||
|
||||
use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
|
||||
|
||||
interface PreAuthenticationJWTUserTokenInterface extends GuardTokenInterface
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function setPayload(array $payload);
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPayload();
|
||||
}
|
||||
Reference in New Issue
Block a user