Improve update key command

This commit is contained in:
Hillel Coren 2017-12-14 17:43:30 +02:00
parent 206c8a9ec0
commit 38b177b4d6

View File

@ -6,7 +6,9 @@ use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use App\Models\AccountGateway; use App\Models\AccountGateway;
use App\Models\BankAccount; use App\Models\BankAccount;
use App\Models\User;
use Artisan; use Artisan;
use Crypt;
use Illuminate\Encryption\Encrypter; use Illuminate\Encryption\Encrypter;
use Laravel\LegacyEncrypter\McryptEncrypter; use Laravel\LegacyEncrypter\McryptEncrypter;
@ -18,7 +20,7 @@ class UpdateKey extends Command
/** /**
* @var string * @var string
*/ */
protected $name = 'ninja:update-key'; protected $name = 'ninja:update-key {--database=}';
/** /**
* @var string * @var string
@ -29,6 +31,10 @@ class UpdateKey extends Command
{ {
$this->info(date('r') . ' Running UpdateKey...'); $this->info(date('r') . ' Running UpdateKey...');
if ($database = $this->option('database')) {
config(['database.default' => $database]);
}
if (! env('APP_KEY') || ! env('APP_CIPHER')) { if (! env('APP_KEY') || ! env('APP_CIPHER')) {
$this->info(date('r') . ' Error: app key and cipher are not set'); $this->info(date('r') . ' Error: app key and cipher are not set');
exit; exit;
@ -42,6 +48,7 @@ class UpdateKey extends Command
// load the current values // load the current values
$gatewayConfigs = []; $gatewayConfigs = [];
$bankUsernames = []; $bankUsernames = [];
$twoFactorSecrets = [];
foreach (AccountGateway::all() as $gateway) { foreach (AccountGateway::all() as $gateway) {
if ($legacy) { if ($legacy) {
@ -59,6 +66,14 @@ class UpdateKey extends Command
} }
} }
foreach (User::where('google_2fa_secret', '!=', '')->get() as $user) {
if ($legacy) {
$twoFactorSecrets[$user->id] = $legacy->decrypt($user->google_2fa_secret);
} else {
$twoFactorSecrets[$user->id] = Crypt::decrypt($user->google_2fa_secret);
}
}
// check if we can write to the .env file // check if we can write to the .env file
$envPath = base_path() . '/.env'; $envPath = base_path() . '/.env';
$envWriteable = file_exists($envPath) && @fopen($envPath, 'a'); $envWriteable = file_exists($envPath) && @fopen($envPath, 'a');
@ -86,6 +101,12 @@ class UpdateKey extends Command
$bank->save(); $bank->save();
} }
foreach (User::where('google_2fa_secret', '!=', '')->get() as $user) {
$secret = $twoFactorSecrets[$user->id];
$user->google_2fa_secret = $crypt->encrypt($secret);
$user->save();
}
$message = date('r') . ' Successfully updated '; $message = date('r') . ' Successfully updated ';
if ($envWriteable) { if ($envWriteable) {
if ($legacy) { if ($legacy) {
@ -118,6 +139,7 @@ class UpdateKey extends Command
{ {
return [ return [
['legacy', null, InputOption::VALUE_OPTIONAL, 'Legacy', null], ['legacy', null, InputOption::VALUE_OPTIONAL, 'Legacy', null],
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
]; ];
} }
} }