Files
firefly-iii/app/Console/Commands/Integrity/RestoreOAuthKeys.php

127 lines
3.0 KiB
PHP
Raw Normal View History

2019-09-21 11:03:00 +02:00
<?php
2020-06-30 19:05:35 +02:00
2019-09-21 11:03:00 +02:00
/**
* RestoreOAuthKeys.php
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 james@firefly-iii.org
2019-09-21 11:03:00 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2019-09-21 11:03:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-09-21 11:03:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2019-09-21 11:03:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-09-21 11:03:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-09-21 11:03:00 +02:00
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2019-09-21 11:03:00 +02:00
namespace FireflyIII\Console\Commands\Integrity;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Support\System\OAuthKeys;
2019-09-21 11:03:00 +02:00
use Illuminate\Console\Command;
/**
* Class RestoreOAuthKeys
*/
class RestoreOAuthKeys extends Command
{
use ShowsFriendlyMessages;
2019-09-21 11:03:00 +02:00
protected $description = 'Will restore the OAuth keys generated for the system.';
protected $signature = 'firefly-iii:restore-oauth-keys';
2019-09-21 11:03:00 +02:00
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$this->restoreOAuthKeys();
2020-03-21 15:43:41 +01:00
2019-09-21 11:03:00 +02:00
return 0;
}
2023-05-07 20:17:29 +02:00
/**
*
*/
private function generateKeys(): void
{
OAuthKeys::generateKeys();
}
/**
* @return bool
*/
private function keysInDatabase(): bool
{
return OAuthKeys::keysInDatabase();
}
/**
* @return bool
*/
private function keysOnDrive(): bool
{
return OAuthKeys::hasKeyFiles();
}
/**
*
*/
private function restoreKeysFromDB(): bool
{
return OAuthKeys::restoreKeysFromDB();
}
2019-09-21 11:03:00 +02:00
/**
*
*/
2021-03-12 06:30:40 +01:00
private function restoreOAuthKeys(): void
2019-09-21 11:03:00 +02:00
{
2021-03-12 06:30:40 +01:00
if (!$this->keysInDatabase() && !$this->keysOnDrive()) {
$this->generateKeys();
$this->storeKeysInDB();
$this->friendlyInfo('Generated and stored new keys.');
2021-03-12 06:30:40 +01:00
return;
}
if ($this->keysInDatabase() && !$this->keysOnDrive()) {
$result = $this->restoreKeysFromDB();
2022-03-29 14:56:27 +02:00
if (true === $result) {
$this->friendlyInfo('Restored OAuth keys from database.');
return;
}
$this->generateKeys();
$this->storeKeysInDB();
$this->friendlyInfo('Generated and stored new keys.');
2021-03-12 06:30:40 +01:00
return;
}
if (!$this->keysInDatabase() && $this->keysOnDrive()) {
$this->storeKeysInDB();
$this->friendlyInfo('Stored OAuth keys in database.');
2021-03-12 06:30:40 +01:00
return;
}
$this->friendlyPositive('OAuth keys are OK');
2019-09-21 11:03:00 +02:00
}
/**
*
*/
2021-03-12 06:30:40 +01:00
private function storeKeysInDB(): void
2019-09-21 11:03:00 +02:00
{
2021-03-12 06:30:40 +01:00
OAuthKeys::storeKeysInDB();
2019-09-21 11:03:00 +02:00
}
2020-03-15 08:16:16 +01:00
}