|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) 2017-2018 Tobias Reich |
| 6 | + * Copyright (c) 2018-2025 LycheeOrg. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\Actions\Diagnostics\Pipes\Checks; |
| 10 | + |
| 11 | +use App\Contracts\DiagnosticPipe; |
| 12 | +use App\DTO\DiagnosticData; |
| 13 | +use App\Models\User; |
| 14 | +use App\Providers\AuthServiceProvider; |
| 15 | +use Illuminate\Support\Facades\Schema; |
| 16 | + |
| 17 | +class AuthDisabledCheck implements DiagnosticPipe |
| 18 | +{ |
| 19 | + public const INFO = 'You need to enable at least one authentication method to be able to use Lychee...'; |
| 20 | + |
| 21 | + /** |
| 22 | + * {@inheritDoc} |
| 23 | + */ |
| 24 | + public function handle(array &$data, \Closure $next): array |
| 25 | + { |
| 26 | + if (!Schema::hasTable('users') || !Schema::hasTable('oauth_credentials') || !Schema::hasTable('webauthn_credentials')) { |
| 27 | + // @codeCoverageIgnoreStart |
| 28 | + return $next($data); |
| 29 | + // @codeCoverageIgnoreEnd |
| 30 | + } |
| 31 | + |
| 32 | + if (AuthServiceProvider::isBasicAuthEnabled()) { |
| 33 | + // If basic auth is enabled, we do not need to check for Oauth or WebAuthn |
| 34 | + // as they are optional and can be used in addition to basic auth. |
| 35 | + return $next($data); |
| 36 | + } |
| 37 | + // From now on, we assume that basic auth is disabled. |
| 38 | + |
| 39 | + if (!AuthServiceProvider::isWebAuthnEnabled() && !AuthServiceProvider::isOauthEnabled()) { |
| 40 | + $data[] = DiagnosticData::error('All authentication methods are disabled. Really?', self::class, [self::INFO]); |
| 41 | + |
| 42 | + return $next($data); |
| 43 | + } |
| 44 | + |
| 45 | + $number_admin_with_oauth = AuthServiceProvider::isOauthEnabled() ? $this->oauthChecks($data) : 0; |
| 46 | + $number_admin_with_webauthn = AuthServiceProvider::isWebAuthnEnabled() ? $this->webauthnCheck($data) : 0; |
| 47 | + if (($number_admin_with_oauth === 0 && AuthServiceProvider::isOauthEnabled()) && |
| 48 | + ($number_admin_with_webauthn === 0 && AuthServiceProvider::isWebAuthnEnabled()) |
| 49 | + ) { |
| 50 | + $data[] = DiagnosticData::error('Basic auth is disabled and there are no admin user with Oauth or WebAuthn enabled.', self::class, [self::INFO]); |
| 51 | + } |
| 52 | + |
| 53 | + return $next($data); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param DiagnosticData[] &$data |
| 58 | + * |
| 59 | + * @return int |
| 60 | + */ |
| 61 | + private function oauthChecks(array &$data): int |
| 62 | + { |
| 63 | + $number_admin_with_oauth = User::query()->has('oauthCredentials')->where('may_administrate', '=', true)->count(); |
| 64 | + if (!AuthServiceProvider::isWebAuthnEnabled() && $number_admin_with_oauth === 0) { |
| 65 | + $data[] = DiagnosticData::error('Basic auth and Webauthn are disabled and there are no admin user with Oauth enabled.', self::class, [self::INFO]); |
| 66 | + } |
| 67 | + |
| 68 | + return $number_admin_with_oauth; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param DiagnosticData[] &$data |
| 73 | + * |
| 74 | + * @return int |
| 75 | + */ |
| 76 | + private function webauthnCheck(array &$data): int |
| 77 | + { |
| 78 | + $number_admin_with_webauthn = User::query()->has('webAuthnCredentials')->where('may_administrate', '=', true)->count(); |
| 79 | + if (!AuthServiceProvider::isOauthEnabled() && $number_admin_with_webauthn === 0) { |
| 80 | + $data[] = DiagnosticData::error('Basic auth is disabled and there are no admin user with WebAuthn enabled.', self::class, [self::INFO]); |
| 81 | + } |
| 82 | + |
| 83 | + return $number_admin_with_webauthn; |
| 84 | + } |
| 85 | +} |
0 commit comments