SomeClass::class`, a class with an `__invoke(): string` that returns what worked * (for example "Connected") and throws when it did not (the message is shown as it is, so keep secrets out of it). */ class Checks { /** @return array group name (as in the definitions) => check */ public function all(): array { return [ 'IriksIT API' => fn () => $this->iriksit(), 'Login' => fn () => $this->turnstile(), ...array_filter((array) config('manager.settings.checks', []), fn ($check) => is_string($check) && class_exists($check)), ]; } public function has(string $group): bool { return array_key_exists($group, $this->all()); } /** @return array{ok: bool, message: string} */ public function run(string $group): array { $check = $this->all()[$group] ?? null; if ($check === null) { return ['ok' => false, 'message' => __('There is nothing to try here.')]; } try { return ['ok' => true, 'message' => (string) (is_string($check) ? app($check)() : $check())]; } catch (Throwable $e) { return ['ok' => false, 'message' => $e->getMessage()]; } } private function iriksit(): string { $client = new Client((array) config('manager.media.iriksit', [])); if (! $client->isConfigured()) { throw new \RuntimeException(__('The address, the account and the password are not all filled in.')); } try { $client->check(); } catch (MediaStorageException $e) { throw new \RuntimeException(__('Logging in at the IriksIT API failed:').' '.$e->getMessage(), 0, $e); } return __('Logged in at the IriksIT API.'); } /** * Cloudflare answers a known secret with "invalid-input-response" for a made-up token, and an unknown one with "invalid-input-secret": * that tells the secret without needing a real visitor. */ private function turnstile(): string { $secret = (string) config('manager.login.turnstile.secret'); if ($secret === '' || (string) config('manager.login.turnstile.site_key') === '') { throw new \RuntimeException(__('Turnstile is off: the site key and the secret are not both filled in.')); } $answer = Http::asForm()->timeout(8)->post('https://challenges.cloudflare.com/turnstile/v0/siteverify', ['secret' => $secret, 'response' => 'manager-check']); $codes = (array) $answer->json('error-codes', []); if (in_array('invalid-input-secret', $codes, true) || in_array('missing-input-secret', $codes, true)) { throw new \RuntimeException(__('Cloudflare does not know this secret key.')); } if (! $answer->ok()) { throw new \RuntimeException(__('Cloudflare did not answer properly.')); } return __('Cloudflare knows this secret key.'); } }