The start of something beautiful
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\User;
|
||||
|
||||
/**
|
||||
* User class for which to create instances from JWT tokens.
|
||||
*
|
||||
* Note: This is only useful when using the JWTUserProvider (database-less).
|
||||
*
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
*/
|
||||
class JWTUser implements JWTUserInterface
|
||||
{
|
||||
private string $userIdentifier;
|
||||
private array $roles;
|
||||
|
||||
public function __construct(string $userIdentifier, array $roles = [])
|
||||
{
|
||||
$this->userIdentifier = $userIdentifier;
|
||||
$this->roles = $roles;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function createFromPayload($username, array $payload): JWTUserInterface
|
||||
{
|
||||
if (isset($payload['roles'])) {
|
||||
return new static($username, (array) $payload['roles']);
|
||||
}
|
||||
|
||||
return new static($username);
|
||||
}
|
||||
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->getUserIdentifier();
|
||||
}
|
||||
|
||||
public function getUserIdentifier(): string
|
||||
{
|
||||
return $this->userIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRoles(): array
|
||||
{
|
||||
return $this->roles;
|
||||
}
|
||||
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSalt(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function eraseCredentials(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\User;
|
||||
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
interface JWTUserInterface extends UserInterface
|
||||
{
|
||||
/**
|
||||
* Creates a new instance from a given JWT payload.
|
||||
*
|
||||
* @param string $username
|
||||
*
|
||||
* @return JWTUserInterface
|
||||
*/
|
||||
public static function createFromPayload($username, array $payload);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\User;
|
||||
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
/**
|
||||
* JWT User provider.
|
||||
*
|
||||
* @author Robin Chalas <robin.chalas@gmail.com>
|
||||
*/
|
||||
final class JWTUserProvider implements PayloadAwareUserProviderInterface
|
||||
{
|
||||
private string $class;
|
||||
|
||||
private array $cache = [];
|
||||
|
||||
/**
|
||||
* @param string $class The {@link JWTUserInterface} implementation FQCN for which to provide instances
|
||||
*/
|
||||
public function __construct(string $class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* To be removed at the same time as symfony 5.4 support.
|
||||
*/
|
||||
public function loadUserByUsername(string $username): UserInterface
|
||||
{
|
||||
// to be removed at the same time as symfony 5.4 support
|
||||
throw new \LogicException('This method is implemented for BC purpose and should never be called.');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param array $payload The JWT payload from which to create an instance
|
||||
*/
|
||||
public function loadUserByIdentifier(string $identifier, array $payload = []): UserInterface
|
||||
{
|
||||
return $this->loadUserByIdentifierAndPayload($identifier, $payload);
|
||||
}
|
||||
|
||||
public function loadUserByIdentifierAndPayload(string $identifier, array $payload): UserInterface
|
||||
{
|
||||
if (isset($this->cache[$identifier])) {
|
||||
return $this->cache[$identifier];
|
||||
}
|
||||
|
||||
$class = $this->class;
|
||||
|
||||
return $this->cache[$identifier] = $class::createFromPayload($identifier, $payload);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsClass($class): bool
|
||||
{
|
||||
return $class === $this->class || (new \ReflectionClass($class))->implementsInterface(JWTUserInterface::class);
|
||||
}
|
||||
|
||||
public function refreshUser(UserInterface $user): UserInterface
|
||||
{
|
||||
return $user; // noop
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\User;
|
||||
|
||||
use Lexik\Bundle\JWTAuthenticationBundle\Exception\UserNotFoundException;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||
|
||||
interface PayloadAwareUserProviderInterface extends UserProviderInterface
|
||||
{
|
||||
/**
|
||||
* Loads a user from an identifier and JWT token payload.
|
||||
*
|
||||
* @throws UserNotFoundException if the user is not found
|
||||
*/
|
||||
public function loadUserByIdentifierAndPayload(string $identifier, array $payload): UserInterface;
|
||||
}
|
||||
Reference in New Issue
Block a user