1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Console/Commands/UpdateKey.php

149 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use App\Models\AccountGateway;
use App\Models\BankAccount;
2017-12-14 16:43:30 +01:00
use App\Models\User;
use Artisan;
2017-12-14 16:43:30 +01:00
use Crypt;
use Illuminate\Encryption\Encrypter;
2017-11-14 12:08:04 +01:00
use Laravel\LegacyEncrypter\McryptEncrypter;
/**
2017-11-14 12:08:04 +01:00
* Class UpdateKey
*/
class UpdateKey extends Command
{
/**
* @var string
*/
2018-01-01 21:21:30 +01:00
protected $name = 'ninja:update-key {--database=} {--key=} {--legacy=}';
/**
* @var string
*/
protected $description = 'Update application key';
public function fire()
{
2017-10-24 09:59:26 +02:00
$this->info(date('r') . ' Running UpdateKey...');
2017-12-14 16:43:30 +01:00
if ($database = $this->option('database')) {
config(['database.default' => $database]);
}
if (! env('APP_KEY') || ! env('APP_CIPHER')) {
2017-10-24 09:59:26 +02:00
$this->info(date('r') . ' Error: app key and cipher are not set');
exit;
}
2017-11-14 12:08:04 +01:00
$legacy = false;
if ($this->option('legacy') == 'true') {
2018-01-01 21:21:30 +01:00
$legacy = new McryptEncrypter(env('APP_KEY'), env('APP_CIPHER'));
2017-11-14 12:08:04 +01:00
}
// load the current values
$gatewayConfigs = [];
$bankUsernames = [];
2017-12-14 16:43:30 +01:00
$twoFactorSecrets = [];
2018-01-01 21:21:30 +01:00
foreach (AccountGateway::withTrashed()->get() as $gateway) {
2017-11-14 12:08:04 +01:00
if ($legacy) {
$gatewayConfigs[$gateway->id] = json_decode($legacy->decrypt($gateway->config));
} else {
$gatewayConfigs[$gateway->id] = $gateway->getConfig();
}
}
2018-01-01 21:21:30 +01:00
foreach (BankAccount::withTrashed()->get() as $bank) {
2017-11-14 12:08:04 +01:00
if ($legacy) {
$bankUsernames[$bank->id] = $legacy->decrypt($bank->username);
} else {
$bankUsernames[$bank->id] = $bank->getUsername();
}
}
2018-01-01 21:21:30 +01:00
foreach (User::withTrashed()->where('google_2fa_secret', '!=', '')->get() as $user) {
2017-12-14 16:43:30 +01:00
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
$envPath = base_path() . '/.env';
$envWriteable = file_exists($envPath) && @fopen($envPath, 'a');
2018-01-01 21:21:30 +01:00
if ($key = $this->option('key')) {
$key = base64_decode(str_replace('base64:', '', $key));
} elseif ($envWriteable) {
Artisan::call('key:generate');
$key = base64_decode(str_replace('base64:', '', config('app.key')));
} else {
$key = str_random(32);
}
2017-12-03 10:18:33 +01:00
$cipher = $legacy ? 'AES-256-CBC' : config('app.cipher');
$crypt = new Encrypter($key, $cipher);
// update values using the new key/encrypter
2018-01-01 21:21:30 +01:00
foreach (AccountGateway::withTrashed()->get() as $gateway) {
$config = $gatewayConfigs[$gateway->id];
$gateway->config = $crypt->encrypt(json_encode($config));
$gateway->save();
}
2018-01-01 21:21:30 +01:00
foreach (BankAccount::withTrashed()->get() as $bank) {
$username = $bankUsernames[$bank->id];
$bank->username = $crypt->encrypt($username);
$bank->save();
}
2018-01-01 21:21:30 +01:00
foreach (User::withTrashed()->where('google_2fa_secret', '!=', '')->get() as $user) {
2017-12-14 16:43:30 +01:00
$secret = $twoFactorSecrets[$user->id];
$user->google_2fa_secret = $crypt->encrypt($secret);
$user->save();
}
2017-12-03 10:18:33 +01:00
$message = date('r') . ' Successfully updated ';
if ($envWriteable) {
2017-12-03 10:18:33 +01:00
if ($legacy) {
$message .= 'the key, set the cipher in the .env file to AES-256-CBC';
} else {
$message .= 'the key';
}
} else {
2017-12-03 10:18:33 +01:00
if ($legacy) {
$message .= 'the data, make sure to set the new cipher/key: AES-256-CBC/' . $key;
} else {
$message .= 'the data, make sure to set the new key: ' . $key;
}
}
2017-12-03 10:18:33 +01:00
$this->info($message);
}
/**
* @return array
*/
protected function getArguments()
{
return [];
}
/**
* @return array
*/
protected function getOptions()
{
2017-11-14 12:08:04 +01:00
return [
['legacy', null, InputOption::VALUE_OPTIONAL, 'Legacy', null],
2017-12-14 16:43:30 +01:00
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
2018-01-01 21:21:30 +01:00
['key', null, InputOption::VALUE_OPTIONAL, 'Key', null],
2017-11-14 12:08:04 +01:00
];
}
}