2017-07-25 20:48:36 +02:00
|
|
|
<?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;
|
2017-07-25 20:48:36 +02:00
|
|
|
use Artisan;
|
2017-12-14 16:43:30 +01:00
|
|
|
use Crypt;
|
2017-07-25 20:48:36 +02:00
|
|
|
use Illuminate\Encryption\Encrypter;
|
2017-11-14 12:08:04 +01:00
|
|
|
use Laravel\LegacyEncrypter\McryptEncrypter;
|
2017-07-25 20:48:36 +02:00
|
|
|
|
|
|
|
/**
|
2017-11-14 12:08:04 +01:00
|
|
|
* Class UpdateKey
|
2017-07-25 20:48:36 +02:00
|
|
|
*/
|
|
|
|
class UpdateKey extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-01-01 21:21:30 +01:00
|
|
|
protected $name = 'ninja:update-key {--database=} {--key=} {--legacy=}';
|
2017-07-25 20:48:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-07-25 20:48:36 +02:00
|
|
|
|
2017-12-14 16:43:30 +01:00
|
|
|
if ($database = $this->option('database')) {
|
|
|
|
config(['database.default' => $database]);
|
|
|
|
}
|
|
|
|
|
2017-08-18 08:36:35 +02:00
|
|
|
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');
|
2017-08-18 08:36:35 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-25 20:48:36 +02:00
|
|
|
// load the current values
|
|
|
|
$gatewayConfigs = [];
|
|
|
|
$bankUsernames = [];
|
2017-12-14 16:43:30 +01:00
|
|
|
$twoFactorSecrets = [];
|
2017-07-25 20:48:36 +02:00
|
|
|
|
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();
|
|
|
|
}
|
2017-07-25 20:48:36 +02:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
2017-07-25 20:48:36 +02:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 08:36:35 +02:00
|
|
|
// 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) {
|
2017-08-18 08:36:35 +02:00
|
|
|
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);
|
2017-07-25 20:48:36 +02:00
|
|
|
|
|
|
|
// update values using the new key/encrypter
|
2018-01-01 21:21:30 +01:00
|
|
|
foreach (AccountGateway::withTrashed()->get() as $gateway) {
|
2017-07-25 20:48:36 +02:00
|
|
|
$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) {
|
2017-07-25 20:48:36 +02:00
|
|
|
$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 ';
|
2017-08-18 08:36:35 +02:00
|
|
|
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';
|
|
|
|
}
|
2017-08-18 08:36:35 +02:00
|
|
|
} 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-08-18 08:36:35 +02:00
|
|
|
}
|
2017-12-03 10:18:33 +01:00
|
|
|
$this->info($message);
|
2017-07-25 20:48:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
];
|
2017-07-25 20:48:36 +02:00
|
|
|
}
|
|
|
|
}
|