-
-
Notifications
You must be signed in to change notification settings - Fork 614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a lexik:jwt:generate-token
command
#485
Conversation
3b44d10
to
ced252d
Compare
Command/GenerateTokenCommand.php
Outdated
|
||
$output->writeln([ | ||
'', | ||
' <info>'.$token.'</info>', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you put a space before the <info>
tag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it looks nice :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding extra chars would prevent redirecting the output (e.g. to clipboard), I would be very raw here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meh, fair point.
Command/GenerateTokenCommand.php
Outdated
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$userClass = $input->getOption('user-class'); | ||
$token = $this->tokenManager->create(new $userClass($input->getArgument('username'), null)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my User entity does not accept any arguments in its constructor, so this will most likely fail with my specific user-class
FYI, when I make the same command in my projects, this is how I retrieve the token:
$user = $this->userProvider->loadUserByUsername($input->getArgument('username'));
$token = $this->tokenManager->create($user);
where userProvider
is a UserProviderInterface
which I map to my user provider service like this:
services:
_defaults:
autowire: true
# ...
Symfony\Component\Security\Core\User\UserProviderInterface: '@security.user.provider.concrete.entity_provider'
# note: "entity_provider" is the name of my provider in "security.yaml"
So, maybe this would be safer to have a user-provider-class
option instead of user-class
(or even configuring this to the lexik_jwt_authentication.yaml
file)
, since we can check that the given class implements the UserProviderInterface
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that the current solution is not ideal, but I'm not fond of user-provider-class
either as it implies to have a service named accordingly, whereas you could have several providers based on the same class (e.g. an entity
provider for two different entities).
What about giving all configured user providers to the command (as a service locator, indexed by their short name) and change the option name to user-provider
?
In case no --user-provider
is passed, if there is only one configured provider then use it, fail otherwise (saying that the option must be passed since multiple providers exist).
Providers can be retrieved from the context listener definition, see https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php#L180.
I can help if you like it
Definitely 👍 for adding this. |
I've updated the PR to use user providers. Though, I can't see how we can easily get their short names so I've kept the service ID as name. I think that's OK as we have a default one used when only one provider is registered. |
26261c1
to
9bfd605
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, seems nice 👍
Resources/config/console.xml
Outdated
@@ -9,6 +9,17 @@ | |||
<argument type="service" id="lexik_jwt_authentication.key_loader" /> | |||
<tag name="console.command" command="lexik:jwt:check-config" /> | |||
</service> | |||
|
|||
<service id="lexik_jwt_authentication.user_provider_locator"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be explicitly public="false"
to not lose DI performance in older symfony versions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can yep. Changed 👍
711ca4d
to
fb5da72
Compare
Command/GenerateTokenCommand.php
Outdated
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
if ($this->userProviders instanceof \Countable && 0 === \count($this->userProviders)) { | ||
throw new \RuntimeException('You must have at least 1 configured user provider for generating a token.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must have at least one configured user provider to generate a token
?
fb5da72
to
c687029
Compare
Thank you @sroze. |
This PR was merged into the 2.x-dev branch. Discussion ---------- Add a `lexik:jwt:generate-token` command For debugging purposes (and more depending on how the tokens are used - i.e. as API keys for example) it's very useful to be able to generate tokens... therefore let's ship a command that does that 👍 Commits ------- c687029 Add a `lexik:jwt:generate-token` command
For debugging purposes (and more depending on how the tokens are used - i.e. as API keys for example) it's very useful to be able to generate tokens... therefore let's ship a command that does that 👍