biig-io / usercomponent Goto Github PK
View Code? Open in Web Editor NEWToolbox for users management (mainly JWT) with Symfony security
Toolbox for users management (mainly JWT) with Symfony security
The README & documentation isn't friendly enough, we should add some sentences to explain it more nicely
This component is supposed to help you bootstrap a project with basic auth. But it only provides a guard that supports basic authentication.
We need to add this controller and its route:
public function __construct(JwtGeneratorInterface $generateJwtTokenForAuthentication, AuthenticationSuccessHandlerInterface $authenticationSuccessHandler)
{
$this->generateJwtTokenForAuthentication = $generateJwtTokenForAuthentication;
$this->authenticationSuccessHandler = $authenticationSuccessHandler;
}
public function __invoke()
{
$user = $this->getUser();
$user->setLoginAt(new \DateTimeImmutable());
$user->setToken($this->generateJwtTokenForAuthentication->create($user));
$response = $this->authenticationSuccessHandler->handleAuthenticationSuccess($user, $user->getToken());
$decodedResponse = json_decode($response->getContent(), true);
$user->setRefreshToken($decodedResponse['refresh_token']);
return $user;
}
This controller (and some other classes of this package) needs a JwtGenerator that the user must define. No doc about (#1) and weird errors. Here is an example of implementation.
class StandardJwtGenerator implements JwtGeneratorInterface
{
/**
* @var JWTEncoderInterface
*/
private $encoder;
/**
* @var string|\DateTime
*/
private $expiration;
/**
* GenerateJwtTokenForAuthentication constructor.
*
* @param JWTEncoderInterface $encoder
*/
public function __construct(JWTEncoderInterface $encoder, $expiration = '+1 day')
{
$this->encoder = $encoder;
$this->expiration = $expiration;
}
/**
* @param UserInterface $user
*
* @return string
*/
public function create(UserInterface $user)
{
$expiration = $this->expiration;
if (!$expiration instanceof \DateTimeInterface) {
$expiration = new \DateTime($this->expiration);
}
$payload = [
'id' => $user->getId(),
'email' => $user->getEmail(),
'exp' => $expiration->getTimestamp(),
];
return $this->encoder->encode($payload);
}
}
Aliasing interfaces is also required:
services:
Biig\Component\User\Jwt\JwtGeneratorInterface: '@App\Domain\User\Security\StandardJwtGenerator'
Biig\Component\User\Persistence\RepositoryInterface: '@App\Infrastructure\ORM\User\Repository\UserRepository'
This is a problem because it removes the possibility to have many services within the interface. It should be done via configuration and automatic creation of services.
This is absolutely not documented. And it could be added automatically by the bundle with a little line of configuration... That modifies the behavior of the guard which is a little bit too much hardcoded. See here:
So, to summarize here is what to do:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.