diff --git a/app/Providers/TwoFAuthServiceProvider.php b/app/Providers/TwoFAuthServiceProvider.php index d0be0cf6..d7c4db38 100644 --- a/app/Providers/TwoFAuthServiceProvider.php +++ b/app/Providers/TwoFAuthServiceProvider.php @@ -74,7 +74,7 @@ class TwoFAuthServiceProvider extends ServiceProvider implements DeferrableProvi return [ IconService::class, IconStoreService::class, - LogoLibManager::class, + 'logolib', QrReader::class, ReleaseRadarService::class, ]; diff --git a/app/Services/LogoLib/AbstractLogoLib.php b/app/Services/LogoLib/AbstractLogoLib.php index 91aa332d..dabae16b 100644 --- a/app/Services/LogoLib/AbstractLogoLib.php +++ b/app/Services/LogoLib/AbstractLogoLib.php @@ -10,15 +10,70 @@ use Illuminate\Support\Facades\Storage; abstract class AbstractLogoLib implements LogoLibInterface { + /** + * The prefix to be aplied to cached filename. + */ + protected string $cachePrefix = ''; + + /** + * Base url of the icon collection + */ + protected string $libUrl = ''; + + /** + * Fetch a logo for the given service and save it as an icon + * + * @param string|null $serviceName Name of the service to fetch a logo for + * @return string|null The icon filename or null if no logo has been found + */ + public function getIcon(?string $serviceName) : string|null + { + $logoFilename = $this->getLogo(strval($serviceName)); + + if ($logoFilename) { + // $iconFilename = IconService::getRandomName('svg'); + $iconFilename = \Illuminate\Support\Str::random(40) . '.svg'; + + return $this->copyToIconStore($logoFilename, $iconFilename) ? $iconFilename : null; + } else { + return null; + } + } + + /** + * Return the logo's filename for a given service + * + * @param string $serviceName Name of the service to fetch a logo for + * @return string|null The logo filename or null if no logo has been found + */ + protected function getLogo(string $serviceName) + { + $referenceName = $this->sanitizeServiceName(strval($serviceName)); + $logoFilename = $referenceName . '.svg'; + $cachedFilename = $this->cachePrefix . $logoFilename; + + if ($referenceName && ! Storage::disk('logos')->exists($cachedFilename)) { + $this->fetchLogo($logoFilename); + } + + return Storage::disk('logos')->exists($cachedFilename) ? $cachedFilename : null; + } + /** * Url to use in http request to get a specific logo from the logo lib */ - abstract protected function logoUrl(string $logoFilename) : string; + protected function logoUrl(string $logoFilename) : string + { + return $this->libUrl . $logoFilename; + } /** * Prepare service name to match logo libs naming convention */ - abstract protected function sanitizeServiceName(string $service) : string; + protected function sanitizeServiceName(string $service) : string + { + return preg_replace('/[^0-9a-z]+/', '-', strtolower($service)); + } /** * Fetch and cache a logo from the logo library @@ -33,9 +88,11 @@ abstract class AbstractLogoLib implements LogoLibInterface ])->retry(3, 100)->get($this->logoUrl($logoFilename)); if ($response->successful()) { - Storage::disk('logos')->put($logoFilename, $response->body()) - ? Log::info(sprintf('Logo "%s" saved to logos dir.', $logoFilename)) - : Log::notice(sprintf('Cannot save logo "%s" to logos dir', $logoFilename)); + $filename = $this->cachePrefix . $logoFilename; + + Storage::disk('logos')->put($filename, $response->body()) + ? Log::info(sprintf('Logo file "%s" saved to logos dir.', $filename)) + : Log::notice(sprintf('Cannot save logo file "%s" to logos dir', $filename)); } } catch (\Exception $exception) { Log::error(sprintf('Fetching of logo "%s" failed.', $logoFilename)); diff --git a/app/Services/LogoLib/DashboardiconsLogoLib.php b/app/Services/LogoLib/DashboardiconsLogoLib.php new file mode 100644 index 00000000..8b6231aa --- /dev/null +++ b/app/Services/LogoLib/DashboardiconsLogoLib.php @@ -0,0 +1,21 @@ +setTfaCollection(); - } - - /** - * Fetch a logo for the given service and save it as an icon - * - * @param string|null $serviceName Name of the service to fetch a logo for - * @return string|null The icon filename or null if no logo has been found - */ - public function getIcon(?string $serviceName) : string|null - { - $logoFilename = $this->getLogo(strval($serviceName)); - - if ($logoFilename) { - // $iconFilename = IconService::getRandomName('svg'); - $iconFilename = \Illuminate\Support\Str::random(40) . '.svg'; - - return $this->copyToIconStore($logoFilename, $iconFilename) ? $iconFilename : null; - } else { - return null; - } - } + } /** * Return the logo's filename for a given service @@ -67,14 +47,15 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface */ protected function getLogo(string $serviceName) { - $domain = $this->tfas->get($this->sanitizeServiceName(strval($serviceName))); - $logoFilename = $domain . '.svg'; + $referenceName = $this->tfas->get($this->sanitizeServiceName(strval($serviceName))); + $logoFilename = $referenceName . '.svg'; + $cachedFilename = $this->cachePrefix . $logoFilename; - if ($domain && ! Storage::disk('logos')->exists($logoFilename)) { - $this->fetchLogo($logoFilename); + if ($referenceName && ! Storage::disk('logos')->exists($cachedFilename)) { + $this->fetchLogo($cachedFilename); } - return Storage::disk('logos')->exists($logoFilename) ? $logoFilename : null; + return Storage::disk('logos')->exists($cachedFilename) ? $cachedFilename : null; } /** @@ -126,7 +107,7 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface */ protected function logoUrl(string $logoFilename) : string { - return self::IMG_URL . $logoFilename[0] . '/' . $logoFilename; + return $this->libUrl . $logoFilename[0] . '/' . $logoFilename; } /** diff --git a/config/app.php b/config/app.php index c4e50142..7079436e 100644 --- a/config/app.php +++ b/config/app.php @@ -1,5 +1,6 @@ App\Facades\TwoFAccounts::class, 'Settings' => App\Facades\Settings::class, 'Helpers' => App\Helpers\Helpers::class, + 'logolib' => LogoLibManager::class, ])->toArray(), ];