Skip to content
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

Allow for creation of tokens without EXP as well as additional payload #1029

Open
wants to merge 6 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Command/GenerateTokenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ protected function configure()
{
$this
->setName(static::$defaultName)
->setDescription('Generates a JWT token')
->addArgument('username', InputArgument::REQUIRED)
->addOption('user-class', 'c', InputOption::VALUE_REQUIRED)
->setDescription('Generates a JWT token with optional payload')
->addArgument('username', InputArgument::REQUIRED, 'Username of user to be retreived from user provider')
->addOption('ttl', 't', InputOption::VALUE_REQUIRED, 'Ttl in seconds to be added to current time. If not provided, the ttl configured in the bundle will be used. Use 0 to generate token without exp')
->addOption('user-class', 'c', InputOption::VALUE_REQUIRED, 'Userclass is used to determine which user provider to use')
->addOption('payload', 'p', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, "Payload as key-value pair separated by ':'")
;
}

Expand Down Expand Up @@ -89,7 +91,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
$user = $userProvider->loadUserByUsername($input->getArgument('username'));
}

$token = $this->tokenManager->create($user);
$payload = [];

if(null !== $input->getOption('ttl') && ((int) $input->getOption('ttl')) == 0) {
$payload['exp'] = 0;
}
elseif(null !== $input->getOption('ttl') && ((int) $input->getOption('ttl')) > 0) {
$payload['exp'] = time() + $input->getOption('ttl');
}

foreach($input->getOption('payload') as $key => $payloadOptions) {
if(false !== stristr($payloadOptions, ':')) {
$payloadOption = explode(':', $payloadOptions);
$payload[$payloadOption[0]] = $payloadOption[1];
} else {
throw new \RuntimeException('Payload must use a : as a separator between key and value.');
}
}

$token = $this->tokenManager->createFromPayload($user, $payload);

$output->writeln([
'',
Expand Down
4 changes: 3 additions & 1 deletion Services/JWSProvider/LcobucciJWSProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public function create(array $payload, array $header = [])
$exp = isset($payload['exp']) ? $payload['exp'] : $now + $this->ttl;
unset($payload['exp']);

$jws->expiresAt($exp instanceof \DateTimeImmutable ? $exp : ($this->useDateObjects ? new \DateTimeImmutable("@$exp") : $exp));
if($exp) {
$jws->expiresAt($exp instanceof \DateTimeImmutable ? $exp : ($this->useDateObjects ? new \DateTimeImmutable("@$exp") : $exp));
}
}

if (isset($payload['sub'])) {
Expand Down