. */ declare(strict_types=1); namespace FireflyIII\Support\System; use Artisan; use Crypt; use Laravel\Passport\Console\KeysCommand; /** * Class OAuthKeys */ class OAuthKeys { private const PRIVATE_KEY = 'oauth_private_key'; private const PUBLIC_KEY = 'oauth_public_key'; /** * */ public static function verifyKeysRoutine(): void { if (!self::keysInDatabase() && !self::hasKeyFiles()) { self::generateKeys(); self::storeKeysInDB(); return; } if (self::keysInDatabase() && !self::hasKeyFiles()) { self::restoreKeysFromDB(); return; } if (!self::keysInDatabase() && self::hasKeyFiles()) { self::storeKeysInDB(); } } /** * @return bool */ public static function keysInDatabase(): bool { return app('fireflyconfig')->has(self::PRIVATE_KEY) && app('fireflyconfig')->has(self::PUBLIC_KEY); } /** * @return bool */ public static function hasKeyFiles(): bool { $private = storage_path('oauth-private.key'); $public = storage_path('oauth-public.key'); return file_exists($private) && file_exists($public); } /** * */ public static function generateKeys(): void { Artisan::registerCommand(new KeysCommand()); Artisan::call('passport:keys'); } /** * */ public static 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))); } /** * */ public static 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); } }