Skip to content

Commit

Permalink
Change default generate key env to single line
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Dec 2, 2024
1 parent 86d625b commit 0c1f06f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
15 changes: 11 additions & 4 deletions app/Console/Commands/SelfHost/SelfHostGenerateKeysCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SelfHostGenerateKeysCommand extends Command
*/
protected $signature = 'self-host:generate-keys
{ --length=4096 : The length of the passport private key }
{ --multi-line : Whether to output the keys in multiple lines }
{ --format=env : The format of the output (env, yaml) }';

/**
Expand All @@ -34,19 +35,25 @@ public function handle(): int
{
$format = $this->option('format');
$key = RSA::createKey((int) $this->option('length'));
$multiLine = (bool) $this->option('multi-line');

$publicKey = (string) $key->getPublicKey();
$privateKey = (string) $key;
$appKey = 'base64:'.base64_encode(Encrypter::generateKey(config('app.cipher')));

if ($format === 'env') {
$this->line('APP_KEY="'.$appKey.'"');
$this->line('PASSPORT_PRIVATE_KEY="'.$privateKey.'"');
$this->line('PASSPORT_PUBLIC_KEY="'.$publicKey.'"');
if ($multiLine) {
$this->line('PASSPORT_PRIVATE_KEY="'.Str::replace("\r\n", "\n", $privateKey).'"');
$this->line('PASSPORT_PUBLIC_KEY="'.Str::replace("\r\n", "\n", $publicKey).'"');
} else {
$this->line('PASSPORT_PRIVATE_KEY="'.Str::replace("\r\n", '\n', $privateKey).'"');
$this->line('PASSPORT_PUBLIC_KEY="'.Str::replace("\r\n", '\n', $publicKey).'"');
}
} elseif ($format === 'yaml') {
$this->line('APP_KEY: "'.$appKey.'"');
$this->line("PASSPORT_PRIVATE_KEY: |\n ".Str::replace("\n", "\n ", $privateKey));
$this->line("PASSPORT_PUBLIC_KEY: |\n ".Str::replace("\n", "\n ", $publicKey));
$this->line("PASSPORT_PRIVATE_KEY: |\n ".Str::replace("\r\n", "\n ", $privateKey));
$this->line("PASSPORT_PUBLIC_KEY: |\n ".Str::replace("\r\n", "\n ", $publicKey));
} else {
$this->error('Invalid format');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,23 @@ public function test_generates_app_key_and_passport_keys_per_default_in_env_form
$this->assertSame(Command::SUCCESS, $exitCode);
$output = Artisan::output();
$this->assertStringContainsString('APP_KEY="base64:', $output);
$this->assertStringContainsString('PASSPORT_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----', $output);
$this->assertStringContainsString('PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----', $output);
$this->assertStringContainsString('PASSPORT_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n', $output);
$this->assertStringContainsString('PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\n', $output);
}

public function test_generates_app_key_and_passport_keys_in_env_format_in_multiline_if_requested(): void
{
// Arrange

// Act
$exitCode = $this->withoutMockingConsoleOutput()->artisan('self-host:generate-keys --multi-line');

// Assert
$this->assertSame(Command::SUCCESS, $exitCode);
$output = Artisan::output();
$this->assertStringContainsString('APP_KEY="base64:', $output);
$this->assertStringContainsString("PASSPORT_PRIVATE_KEY=\"-----BEGIN PRIVATE KEY-----\n", $output);
$this->assertStringContainsString("PASSPORT_PUBLIC_KEY=\"-----BEGIN PUBLIC KEY-----\n", $output);
}

public function test_generates_app_key_and_passport_keys_in_yaml_format_if_requested(): void
Expand Down

0 comments on commit 0c1f06f

Please sign in to comment.