From eb05d392102fe63427539b27051f826eee5ec89f Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Mon, 10 Oct 2022 11:24:02 +0200 Subject: [PATCH] Add icon import from Aegis migration data --- app/Services/Migrators/AegisMigrator.php | 39 +++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/app/Services/Migrators/AegisMigrator.php b/app/Services/Migrators/AegisMigrator.php index a6e30b4d..b9489776 100644 --- a/app/Services/Migrators/AegisMigrator.php +++ b/app/Services/Migrators/AegisMigrator.php @@ -8,6 +8,8 @@ use Illuminate\Support\Facades\Log; use Illuminate\Support\Arr; use App\Exceptions\InvalidMigrationDataException; +use Illuminate\Support\Facades\Storage; +use App\Helpers\Helpers; class AegisMigrator extends Migrator { @@ -47,7 +49,7 @@ public function migrate(mixed $migrationPayload) : Collection } foreach ($json['db']['entries'] as $key => $otp_parameters) { - // Storage::put('file.jpg', $contents); + $parameters = array(); $parameters['otp_type'] = $otp_parameters['type'] == 'steam' ? TwoFAccount::STEAM_TOTP : $otp_parameters['type']; $parameters['service'] = $otp_parameters['issuer']; @@ -58,6 +60,41 @@ public function migrate(mixed $migrationPayload) : Collection $parameters['counter'] = $otp_parameters['info']['counter'] ?? null; $parameters['period'] = $otp_parameters['info']['period'] ?? null; + try { + // Aegis supports 3 image extensions for icons + // (see https://github.com/beemdevelopment/Aegis/blob/3c10b234ea70715776a09e3d200cb6e806a43f83/docs/iconpacks.md) + + if (Arr::has($otp_parameters, 'icon') && Arr::has($otp_parameters, 'icon_mime')) { + switch ($otp_parameters['icon_mime']) { + case 'image/svg+xml': + $extension = 'svg'; + break; + + case 'image/png': + $extension = 'png'; + break; + + case 'image/jpeg': + $extension = 'jpg'; + break; + + default: + throw new \Exception(); + break; + } + + $filename = Helpers::getUniqueFilename($extension); + + if (Storage::disk('icons')->put($filename, base64_decode($otp_parameters['icon']))) { + $parameters['icon'] = $filename; + Log::info(sprintf('Image %s successfully stored for import', $filename)); + } + } + } + catch (\Exception) { + // we do nothing + } + try { $twofaccounts[$key] = new TwoFAccount; $twofaccounts[$key]->fillWithOtpParameters($parameters);