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

Change default generate key env to single line #268

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
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
Loading