Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Code for #2616
James Cole9ea1a495b8d6
diff --git a/.deploy/docker/entrypoint.sh b/.deploy/docker/entrypoint.shindex 7ab3171e45..30ee1c7500 100755--- a/.deploy/docker/entrypoint.sh+++ b/.deploy/docker/entrypoint.sh@@ -92,6 +92,7 @@ php artisan firefly-iii:fix-long-descriptions# report commandsphp artisan firefly-iii:report-empty-objectsphp artisan firefly-iii:report-sum+php artisan firefly-iii:restore-oauth-keysphp artisan passport:installphp artisan cache:cleardiff --git a/app/Console/Commands/Correction/CorrectDatabase.php b/app/Console/Commands/Correction/CorrectDatabase.phpindex 2205f8a932..0f692911d2 100644--- a/app/Console/Commands/Correction/CorrectDatabase.php+++ b/app/Console/Commands/Correction/CorrectDatabase.php@@ -71,7 +71,8 @@ class CorrectDatabase extends Command'firefly-iii:fix-account-types','firefly-iii:rename-meta-fields','firefly-iii:fix-ob-currencies',- 'firefly-iii:fix-long-descriptions'+ 'firefly-iii:fix-long-descriptions',+ 'firefly-iii:restore-oauth-keys'];foreach ($commands as $command) {$this->line(sprintf('Now executing %s', $command));diff --git a/app/Console/Commands/Correction/FixLongDescriptions.php b/app/Console/Commands/Correction/FixLongDescriptions.phpindex 15f861ab02..e15e63a871 100644--- a/app/Console/Commands/Correction/FixLongDescriptions.php+++ b/app/Console/Commands/Correction/FixLongDescriptions.php@@ -51,6 +51,7 @@ class FixLongDescriptions extends Command*/public function handle(): int{+ $start = microtime(true);$journals = TransactionJournal::get(['id', 'description']);/** @var TransactionJournal $journal */foreach ($journals as $journal) {@@ -70,7 +71,8 @@ class FixLongDescriptions extends Command$this->line(sprintf('Truncated description of transaction group #%d', $group->id));}}-+ $end = round(microtime(true) - $start, 2);+ $this->info(sprintf('Verified all transaction group and journal title lengths in %s seconds.', $end));return 0;}}diff --git a/app/Console/Commands/Integrity/RestoreOAuthKeys.php b/app/Console/Commands/Integrity/RestoreOAuthKeys.phpnew file mode 100644index 0000000000..58c9179983--- /dev/null+++ b/app/Console/Commands/Integrity/RestoreOAuthKeys.php@@ -0,0 +1,137 @@+<?php+/**+ * RestoreOAuthKeys.php+ * Copyright (c) 2019 [email protected]+ *+ * This file is part of Firefly III.+ *+ * Firefly III is free software: you can redistribute it and/or modify+ * it under the terms of the GNU General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * Firefly III is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU General Public License for more details.+ *+ * You should have received a copy of the GNU General Public License+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.+ */++namespace FireflyIII\Console\Commands\Integrity;++use Artisan;+use Crypt;+use Illuminate\Console\Command;++/**+ * Class RestoreOAuthKeys+ */+class RestoreOAuthKeys extends Command+{+ private const PRIVATE_KEY = 'oauth_private_key';+ private const PUBLIC_KEY = 'oauth_public_key';+ /**+ * The console command description.+ *+ * @var string+ */+ protected $description = 'Will restore the OAuth keys generated for the system.';+ /**+ * The name and signature of the console command.+ *+ * @var string+ */+ protected $signature = 'firefly-iii:restore-oauth-keys';++ /**+ * Execute the console command.+ *+ * @return int+ */+ public function handle(): int+ {+ $this->restoreOAuthKeys();++ return 0;+ }++ /**+ *+ */+ private function generateKeys(): void+ {+ Artisan::call('passport:keys');+ }++ /**+ * @return bool+ */+ private function keysInDatabase(): bool+ {+ return app('fireflyconfig')->has(self::PRIVATE_KEY) && app('fireflyconfig')->has(self::PUBLIC_KEY);+ }++ /**+ * @return bool+ */+ private function keysOnDrive(): bool+ {+ $private = storage_path('oauth-private.key');+ $public = storage_path('oauth-public.key');++ return file_exists($private) && file_exists($public);+ }++ /**+ *+ */+ private function restoreKeysFromDB(): void+ {+ $privateContent = Crypt::decrypt(app('fireflyconfig')->get(self::PRIVATE_KEY)->data);+ $publicContent = Crypt::decrypt(app('fireflyconfig')->get(self::PUBLIC_KEY)->data);+ $private = storage_path('oauth-private.key');+ $public = storage_path('oauth-public.key');+ file_put_contents($private, $privateContent);+ file_put_contents($public, $publicContent);+ }++ /**+ *+ */+ private function restoreOAuthKeys(): void+ {+ if (!$this->keysInDatabase() && !$this->keysOnDrive()) {+ $this->generateKeys();+ $this->storeKeysInDB();+ $this->line('Generated and stored new keys.');++ return;+ }+ if ($this->keysInDatabase() && !$this->keysOnDrive()) {+ $this->restoreKeysFromDB();+ $this->line('Restored OAuth keys from database.');++ return;+ }+ if (!$this->keysInDatabase() && $this->keysOnDrive()) {+ $this->storeKeysInDB();+ $this->line('Stored OAuth keys in database.');++ return;+ }+ $this->line('OAuth keys are OK');+ }++ /**+ *+ */+ private function storeKeysInDB(): void+ {+ $private = storage_path('oauth-private.key');+ $public = storage_path('oauth-public.key');+ app('fireflyconfig')->set(self::PRIVATE_KEY, Crypt::encrypt(file_get_contents($private)));+ app('fireflyconfig')->set(self::PUBLIC_KEY, Crypt::encrypt(file_get_contents($public)));+ }+}\ No newline at end of filediff --git a/app/Console/Commands/Upgrade/UpgradeDatabase.php b/app/Console/Commands/Upgrade/UpgradeDatabase.phpindex 38efafee2b..c3abd707d0 100644--- a/app/Console/Commands/Upgrade/UpgradeDatabase.php+++ b/app/Console/Commands/Upgrade/UpgradeDatabase.php@@ -93,6 +93,7 @@ class UpgradeDatabase extends Command// two report commands'firefly-iii:report-empty-objects','firefly-iii:report-sum',+ 'firefly-iii:restore-oauth-keys',// instructions'firefly:instructions update',diff --git a/app/Http/Controllers/System/InstallController.php b/app/Http/Controllers/System/InstallController.phpindex 4e0173115f..09e2c69756 100644--- a/app/Http/Controllers/System/InstallController.php+++ b/app/Http/Controllers/System/InstallController.php@@ -100,7 +100,8 @@ class InstallController extends Controller'firefly-iii:fix-account-types' => [],'firefly-iii:rename-meta-fields' => [],'firefly-iii:fix-ob-currencies' => [],- 'firefly-iii:fix-long-descriptions' => [],+ 'firefly-iii:fix-long-descriptions' => [],+ 'firefly-iii:restore-oauth-keys' => [],];}diff --git a/app/Support/FireflyConfig.php b/app/Support/FireflyConfig.phpindex 94a7c058ff..384578a6c0 100644--- a/app/Support/FireflyConfig.php+++ b/app/Support/FireflyConfig.php@@ -54,6 +54,16 @@ class FireflyConfig}}+ /**+ * @param string $name+ *+ * @return bool+ */+ public function has(string $name): bool+ {+ return Configuration::where('name', $name)->count() === 1;+ }+/*** @param string $name* @param mixed $defaultdiff --git a/composer.json b/composer.jsonindex 3d4e6c8af6..d8ea789398 100644--- a/composer.json+++ b/composer.json@@ -167,6 +167,7 @@"@php artisan firefly-iii:report-empty-objects","@php artisan firefly-iii:report-sum",+ "@php artisan firefly-iii:restore-oauth-keys","@php artisan firefly:instructions update","@php artisan passport:install"