Skip to content

Commit

Permalink
bug #838 Fix wiring GenerateKeyPairCommand when key paths are null (c…
Browse files Browse the repository at this point in the history
…halasr)

This PR was submitted for the master branch but it was merged into the 2.x branch instead.

Discussion
----------

Fix wiring GenerateKeyPairCommand when key paths are null

Commits
-------

6600e87 Fix wiring GenerateKeyPairCommand when key paths are null
  • Loading branch information
chalasr committed Feb 15, 2021
2 parents 7a667c8 + 6600e87 commit bb1e6c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Command/GenerateKeyPairCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lexik\Bundle\JWTAuthenticationBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -34,12 +35,12 @@ final class GenerateKeyPairCommand extends Command
private $filesystem;

/**
* @var string
* @var string|null
*/
private $secretKey;

/**
* @var string
* @var string|null
*/
private $publicKey;

Expand All @@ -53,7 +54,7 @@ final class GenerateKeyPairCommand extends Command
*/
private $algorithm;

public function __construct(Filesystem $filesystem, string $secretKey, string $publicKey, ?string $passphrase, string $algorithm)
public function __construct(Filesystem $filesystem, ?string $secretKey, ?string $publicKey, ?string $passphrase, string $algorithm)
{
parent::__construct();
$this->filesystem = $filesystem;
Expand Down Expand Up @@ -95,9 +96,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

if (!$this->secretKey || !$this->publicKey) {
throw new LogicException(sprintf('The "lexik_jwt_authentication.secret_key" and "lexik_jwt_authentication.public_key" config options must not be empty for using the "%s" command.', self::$defaultName));
}

$alreadyExists = $this->filesystem->exists($this->secretKey) || $this->filesystem->exists($this->publicKey);

if (true === $alreadyExists) {
if ($alreadyExists) {
try {
$this->handleExistingKeys($input);
} catch (\RuntimeException $e) {
Expand Down
10 changes: 10 additions & 0 deletions Tests/Functional/Command/GenerateKeyPairCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@

class GenerateKeyPairCommandTest extends TestCase
{
public function testCannotGenerateKeysWhenPathsAreNull()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The "lexik_jwt_authentication.secret_key" and "lexik_jwt_authentication.public_key" config options must not be empty for using the "lexik:jwt:generate-keypair" command.');

$command = new GenerateKeyPairCommand(new Filesystem(), null, null, null, 'RS512');

(new CommandTester($command))->execute([]);
}

/**
* @dataProvider providePassphrase
*/
Expand Down

0 comments on commit bb1e6c3

Please sign in to comment.