v0.8.0
This commit is contained in:
commit
bf5c31169a
221 files changed
+16292
No files matched your search
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support\Settings;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use IriksIT\Manager\Exceptions\MediaStorageException;
|
||||
use IriksIT\Manager\Support\IriksStorage\Client;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* The "try it" buttons of the settings screen: one check per group of settings that makes a connection (the IriksIT API, Turnstile).
|
||||
* A check uses what is saved, never what is typed but not yet saved, and says in one line whether it works. A site adds its own in
|
||||
* `manager.settings.checks` as `'Group name' => 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<string, class-string|callable> 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.');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user