Skip to content

Commit

Permalink
Achieve 100% types coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fbourigault committed Apr 8, 2024
1 parent f66480c commit e42f740
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 51 deletions.
66 changes: 26 additions & 40 deletions src/Command/GenerateKeyPairCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,17 @@ final class GenerateKeyPairCommand extends Command
*/
protected static $defaultName = 'league:oauth2-server:generate-keypair';

/**
* @var Filesystem
*/
private $filesystem;
private Filesystem $filesystem;

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

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

/**
* @var string|null
*/
private $passphrase;
private ?string $passphrase;

/**
* @var string
*/
private $algorithm;
private string $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 All @@ -86,12 +71,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (!\in_array($this->algorithm, self::ACCEPTED_ALGORITHMS, true)) {
$io->error(sprintf('Cannot generate key pair with the provided algorithm `%s`.', $this->algorithm));

return 1;
return Command::FAILURE;
}

[$secretKey, $publicKey] = $this->generateKeyPair($this->passphrase);

if (true === $input->getOption('dry-run')) {
if ($input->getOption('dry-run')) {
$io->success('Your keys have been generated!');
$io->newLine();
$io->writeln(sprintf('Update your private key in <info>%s</info>:', $this->secretKey));
Expand All @@ -100,11 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->writeln(sprintf('Update your public key in <info>%s</info>:', $this->publicKey));
$io->writeln($publicKey);

return 0;
}

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

$alreadyExists = $this->filesystem->exists($this->secretKey) || $this->filesystem->exists($this->publicKey);
Expand All @@ -116,18 +97,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (0 === $e->getCode()) {
$io->comment($e->getMessage());

return 0;
return Command::SUCCESS;
}

$io->error($e->getMessage());

return 1;
return Command::FAILURE;
}

if (!$io->confirm('You are about to replace your existing keys. Are you sure you wish to continue?')) {
$io->comment('Your action was canceled.');

return 0;
return Command::SUCCESS;
}
}

Expand All @@ -136,25 +117,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$io->success('Done!');

return 0;
return Command::SUCCESS;
}

private function handleExistingKeys(InputInterface $input): void
{
if (true === $input->getOption('skip-if-exists') && true === $input->getOption('overwrite')) {
if ($input->getOption('skip-if-exists') && $input->getOption('overwrite')) {
throw new \RuntimeException('Both options `--skip-if-exists` and `--overwrite` cannot be combined.', 1);
}

if (true === $input->getOption('skip-if-exists')) {
throw new \RuntimeException('Your key files already exist, they won\'t be overriden.', 0);
if ($input->getOption('skip-if-exists')) {
throw new \RuntimeException('Your key files already exist, they won\'t be overridden.', 0);
}

if (false === $input->getOption('overwrite')) {
if (!$input->getOption('overwrite')) {
throw new \RuntimeException('Your keys already exist. Use the `--overwrite` option to force regeneration.', 1);
}
}

private function generateKeyPair($passphrase): array
/**
* @return array{0: string, 1: string}
*/
private function generateKeyPair(?string $passphrase): array
{
$config = $this->buildOpenSSLConfiguration();

Expand All @@ -171,13 +155,15 @@ private function generateKeyPair($passphrase): array

$publicKeyData = openssl_pkey_get_details($resource);

if (false === $publicKeyData) {
if (!\is_array($publicKeyData)) {
throw new \RuntimeException(openssl_error_string());
}

$publicKey = $publicKeyData['key'];
if (!\array_key_exists('key', $publicKeyData) || !\is_string($publicKeyData['key'])) {
throw new \RuntimeException('Invalid public key type.');
}

return [$privateKey, $publicKey];
return [$privateKey, $publicKeyData['key']];
}

private function buildOpenSSLConfiguration(): array
Expand Down
12 changes: 1 addition & 11 deletions tests/Functional/Command/GenerateKeyPairCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@

class GenerateKeyPairCommandTest extends TestCase
{
public function testCannotGenerateKeysWhenPathsAreNull()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The "league_oauth2_server.authorization_server.private_key" and "league_oauth2_server.resource_server.public_key" config options must not be empty for using the "league:oauth2-server:generate-keypair" command.');

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

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

/**
* @dataProvider providePassphrase
*/
Expand Down Expand Up @@ -219,7 +209,7 @@ public function testSkipIfExistsDoesNothingIfExists()

$this->assertSame(0, $tester->execute(['--skip-if-exists' => true], ['interactive' => false]));
$this->assertStringContainsString(
'Your key files already exist, they won\'t be overriden.',
'Your key files already exist, they won\'t be overridden.',
$tester->getDisplay(true)
);

Expand Down

0 comments on commit e42f740

Please sign in to comment.