Better catch for key restauration problems.

This commit is contained in:
James Cole 2021-12-21 16:35:28 +01:00
parent 4d933e1ef7
commit 45fbf83971
No known key found for this signature in database
GPG Key ID: BDE6667570EADBD5
2 changed files with 29 additions and 8 deletions

View File

@ -74,8 +74,16 @@ class RestoreOAuthKeys extends Command
} }
if ($this->keysInDatabase() && !$this->keysOnDrive()) { if ($this->keysInDatabase() && !$this->keysOnDrive()) {
Log::debug('Keys are in DB and keys are not on the drive. Restore.'); Log::debug('Keys are in DB and keys are not on the drive. Restore.');
$this->restoreKeysFromDB(); $result = $this->restoreKeysFromDB();
$this->line('Restored OAuth keys from database.'); if(true === $result) {
$this->line('Restored OAuth keys from database.');
return;
}
Log::warning('Could not restore keys. Will create new ones.');
$this->generateKeys();
$this->storeKeysInDB();
$this->line('Generated and stored new keys.');
return; return;
} }
@ -124,8 +132,8 @@ class RestoreOAuthKeys extends Command
/** /**
* *
*/ */
private function restoreKeysFromDB(): void private function restoreKeysFromDB(): bool
{ {
OAuthKeys::restoreKeysFromDB(); return OAuthKeys::restoreKeysFromDB();
} }
} }

View File

@ -27,6 +27,7 @@ namespace FireflyIII\Support\System;
use Artisan; use Artisan;
use Crypt; use Crypt;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use Illuminate\Contracts\Encryption\DecryptException;
use Laravel\Passport\Console\KeysCommand; use Laravel\Passport\Console\KeysCommand;
use Log; use Log;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
@ -117,18 +118,30 @@ class OAuthKeys
} }
/** /**
* * @return bool
*/ */
public static function restoreKeysFromDB(): void public static function restoreKeysFromDB(): bool
{ {
$privateKey = (string)app('fireflyconfig')->get(self::PRIVATE_KEY)?->data; $privateKey = (string)app('fireflyconfig')->get(self::PRIVATE_KEY)?->data;
$publicKey = (string)app('fireflyconfig')->get(self::PUBLIC_KEY)?->data; $publicKey = (string)app('fireflyconfig')->get(self::PUBLIC_KEY)?->data;
$privateContent = Crypt::decrypt($privateKey); try {
$publicContent = Crypt::decrypt($publicKey); $privateContent = Crypt::decrypt($privateKey);
$publicContent = Crypt::decrypt($publicKey);
} catch(DecryptException $e) {
Log::error('Could not decrypt pub/private keypair.');
Log::error($e->getMessage());
// delete config vars from DB:
app('fireflyconfig')->delete(self::PRIVATE_KEY);
app('fireflyconfig')->delete(self::PUBLIC_KEY);
return false;
}
$private = storage_path('oauth-private.key'); $private = storage_path('oauth-private.key');
$public = storage_path('oauth-public.key'); $public = storage_path('oauth-public.key');
file_put_contents($private, $privateContent); file_put_contents($private, $privateContent);
file_put_contents($public, $publicContent); file_put_contents($public, $publicContent);
return true;
} }
} }