diff --git a/Command/GenerateTokenCommand.php b/Command/GenerateTokenCommand.php index 38378ae1..6fcb1c54 100644 --- a/Command/GenerateTokenCommand.php +++ b/Command/GenerateTokenCommand.php @@ -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 ':'") ; } @@ -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([ '', diff --git a/Services/JWSProvider/LcobucciJWSProvider.php b/Services/JWSProvider/LcobucciJWSProvider.php index 50383078..688beaad 100644 --- a/Services/JWSProvider/LcobucciJWSProvider.php +++ b/Services/JWSProvider/LcobucciJWSProvider.php @@ -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'])) {