From 29ce9d3df1091e4ac22b4fe784269a3c4bba8636 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Fri, 9 Aug 2019 11:20:36 +0800 Subject: [PATCH] Use built-in `key:generate` command --- .circleci/config.yml | 2 +- .env.example | 2 +- .env.testing | 2 +- app/Console/Commands/BsInstallCommand.php | 4 +- app/Console/Commands/KeyRandomCommand.php | 74 ----------------------- app/Console/Kernel.php | 1 - app/Http/Controllers/SetupController.php | 2 +- config/app.php | 2 +- tests/SetupControllerTest.php | 2 +- 9 files changed, 7 insertions(+), 84 deletions(-) delete mode 100644 app/Console/Commands/KeyRandomCommand.php diff --git a/.circleci/config.yml b/.circleci/config.yml index 27d62fdf..549b4ec7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -37,7 +37,7 @@ jobs: - vendor key: v1-dependencies-{{ checksum "composer.lock" }} - run: cp .env.testing .env - - run: php artisan key:random + - run: php artisan key:generate - run: php artisan salt:random - persist_to_workspace: root: ~/repo diff --git a/.env.example b/.env.example index 6b708f29..e37166bd 100644 --- a/.env.example +++ b/.env.example @@ -51,7 +51,7 @@ SALT = 2c5ca184f017a9a1ffbd198ef69b0c0e # # You can run [php artisan key:generate] to generate a new key. # -APP_KEY = base64:gkb/zouNF6UOSfnr/o+izVMS57WQS3+62YqZBuDyBhU= +APP_KEY=base64:gkb/zouNF6UOSfnr/o+izVMS57WQS3+62YqZBuDyBhU= # Mail Configuration # diff --git a/.env.testing b/.env.testing index d4686546..037189e5 100644 --- a/.env.testing +++ b/.env.testing @@ -16,7 +16,7 @@ DB_PREFIX = null PWD_METHOD = BCRYPT SALT = c67709dd8b7b733aca0d570681fe96cf -APP_KEY = base64:eVX/xzF5NhpGB2luswliFx9XSBsbbAP21wOi68X/P34= +APP_KEY=base64:eVX/xzF5NhpGB2luswliFx9XSBsbbAP21wOi68X/P34= MAIL_DRIVER = smtp MAIL_HOST = localhost diff --git a/app/Console/Commands/BsInstallCommand.php b/app/Console/Commands/BsInstallCommand.php index ba301480..022bd6d1 100644 --- a/app/Console/Commands/BsInstallCommand.php +++ b/app/Console/Commands/BsInstallCommand.php @@ -19,9 +19,7 @@ class BsInstallCommand extends Command return; } - if (config('app.env') != 'testing') { - $this->call('key:random'); - } + $this->call('key:generate'); $this->call('salt:random'); $this->call('migrate', ['--force' => true]); $this->call('jwt:secret', ['--no-interaction' => true]); diff --git a/app/Console/Commands/KeyRandomCommand.php b/app/Console/Commands/KeyRandomCommand.php deleted file mode 100644 index c2de4c1c..00000000 --- a/app/Console/Commands/KeyRandomCommand.php +++ /dev/null @@ -1,74 +0,0 @@ -generateRandomKey(); - - if ($this->option('show')) { - return $this->line(''.$key.''); - } - - // Next, we will replace the application key in the environment file so it is - // automatically setup for this developer. This key gets generated using a - // secure random byte generator and is later base64 encoded for storage. - $this->setKeyInEnvironmentFile($key); - - $this->laravel['config']['app.key'] = $key; - - $this->info("Application key [$key] set successfully."); - } - - /** - * Set the application key in the environment file. - * - * @param string $key - * @return void - */ - protected function setKeyInEnvironmentFile($key) - { - // Unlike Illuminate\Foundation\Console\KeyGenerateCommand, - // I add some spaces to the replace pattern. - file_put_contents($this->laravel->environmentFilePath(), str_replace( - 'APP_KEY = '.$this->laravel['config']['app.key'], - 'APP_KEY = '.$key, - file_get_contents($this->laravel->environmentFilePath()) - )); - } - - /** - * Generate a random key for the application. - * - * @return string - */ - protected function generateRandomKey() - { - return 'base64:'.base64_encode(random_bytes( - $this->laravel['config']['app.cipher'] == 'AES-128-CBC' ? 16 : 32 - )); - } -} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 8dadbaf3..8ccf2129 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -13,7 +13,6 @@ class Kernel extends ConsoleKernel */ protected $commands = [ \Laravel\Passport\Console\KeysCommand::class, - Commands\KeyRandomCommand::class, Commands\SaltRandomCommand::class, Commands\BsInstallCommand::class, Commands\PluginEnableCommand::class, diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 9555a6ab..1595d6c6 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -138,7 +138,7 @@ class SetupController extends Controller ]); if ($request->has('generate_random')) { - Artisan::call('key:random'); + Artisan::call('key:generate'); Artisan::call('salt:random'); } Artisan::call('jwt:secret', ['--no-interaction' => true]); diff --git a/config/app.php b/config/app.php index 3701b715..95a8b3b7 100644 --- a/config/app.php +++ b/config/app.php @@ -114,7 +114,7 @@ return [ | */ - 'key' => env('APP_KEY', 'base64:MfnScX0W/ViN8bZtRt0P481rWP3igcOK80QstjbXUxI='), + 'key' => env('APP_KEY'), 'cipher' => 'AES-256-CBC', diff --git a/tests/SetupControllerTest.php b/tests/SetupControllerTest.php index afe8a68e..ef275d54 100644 --- a/tests/SetupControllerTest.php +++ b/tests/SetupControllerTest.php @@ -160,7 +160,7 @@ class SetupControllerTest extends TestCase // Regenerate keys Artisan::shouldReceive('call') - ->with('key:random') + ->with('key:generate') ->once() ->andReturn(true); Artisan::shouldReceive('call')