-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
232 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Security\Factory; | ||
|
||
use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUser; | ||
use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface; | ||
use Symfony\Component\Config\Definition\Builder\NodeDefinition; | ||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface; | ||
|
||
/** | ||
* JWTUserFactory. | ||
* | ||
* @author Robin Chalas <[email protected]> | ||
*/ | ||
class JWTUserFactory implements UserProviderFactoryInterface | ||
{ | ||
public function create(ContainerBuilder $container, $id, $config) | ||
{ | ||
$definition = $container->setDefinition($id, new DefinitionDecorator('lexik_jwt_authentication.security.jwt_user_provider')); | ||
$definition->replaceArgument(0, $config['class']); | ||
} | ||
|
||
public function getKey() | ||
{ | ||
return 'lexik_jwt'; | ||
} | ||
|
||
public function addConfiguration(NodeDefinition $node) | ||
{ | ||
$node | ||
->children() | ||
->scalarNode('class') | ||
->cannotBeEmpty() | ||
->defaultValue(JWTUser::class) | ||
->validate() | ||
->ifTrue(function ($class) { return !is_subclass_of($class, JWTUserInterface::class); }) | ||
->thenInvalid('The %s class must implement JWTUserInterface.') | ||
->end() | ||
->end() | ||
->end() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\User; | ||
|
||
class JWTUser implements JWTUserInterface | ||
{ | ||
private $username; | ||
private $roles; | ||
|
||
public function __construct($username, array $roles = []) | ||
{ | ||
$this->username = $username; | ||
$this->roles = $roles; | ||
} | ||
|
||
/** | ||
* Creates a new instance from a given JWT payload. | ||
* | ||
* @param string $username | ||
* @param array $payload | ||
* | ||
* @return static | ||
*/ | ||
public static function createFromPayload($username, array $payload) | ||
{ | ||
if (isset($payload['roles'])) { | ||
return new self($username, (array) $payload['roles']); | ||
} | ||
|
||
return new self($username); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getUsername() | ||
{ | ||
return $this->username; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRoles() | ||
{ | ||
return $this->roles; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPassword() | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSalt() | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function eraseCredentials() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?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 | ||
* @param array $payload | ||
* | ||
* @return JWTUserInterface | ||
*/ | ||
public static function createFromPayload($username, array $payload); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\User; | ||
|
||
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* JWT User provider. | ||
* | ||
* @author Robin Chalas <[email protected]> | ||
*/ | ||
final class JWTUserProvider implements UserProviderInterface | ||
{ | ||
private $class; | ||
|
||
// FIXME | ||
private $cache; | ||
|
||
/** | ||
* @param string $class The {@link JWTUserInterface} implementation FQCN for which to provide instances. | ||
*/ | ||
public function __construct($class) | ||
{ | ||
$this->class = $class; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* FIXME | ||
*/ | ||
public function loadUserByUsername($username, array $payload = null) | ||
{ | ||
if (null === $payload) { | ||
return; | ||
} | ||
|
||
$class = $this->class; | ||
|
||
return $class::createFromPayload($username, $payload); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsClass($class) | ||
{ | ||
return $class === $this->class || is_subclass_of($class, $this->class); | ||
} | ||
|
||
public function refreshUser(UserInterface $user) | ||
{ | ||
// no datastore => no refresh | ||
// only one JWT and the corresponding instances | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters