Pflege Adresse und Zugangsschlüssel der Shinkan-Anbindung in der Verwaltung
Werte aus der Umgebung des Servers haben Vorrang, sonst gilt der in der Verwaltung gepflegte. Der Schlüssel liegt verschlüsselt in site_settings und wird nie angezeigt. Änderung nur für Katalog- und Systemverwalter. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bcda189267
commit
26ca79cd22
8 files changed
+320
-17
No files matched your search
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use App\Domain\Design\SiteSetting;
|
||||
use App\Models\User;
|
||||
use Illuminate\Contracts\Encryption\DecryptException;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
|
||||
/**
|
||||
* Adresse und Zugangsschlüssel der Shinkan-Anbindung. Zwei Quellen mit fester Rangfolge: Ein Wert aus der Umgebung des
|
||||
* Servers (`SHINKAN_DISTRIBUTION_URL`, `SHINKAN_DISTRIBUTION_KEY`) hat Vorrang. Fehlt er dort, gilt der in der Verwaltung
|
||||
* gepflegte Wert. So lässt sich die Schnittstelle ändern, ohne die Umgebung des Servers anzufassen.
|
||||
*
|
||||
* Der in der Verwaltung gepflegte Schlüssel liegt verschlüsselt (Anwendungsschlüssel) in den Einstellungen. Er wird nie
|
||||
* angezeigt, nie protokolliert und nicht in Fehlermeldungen genannt.
|
||||
*/
|
||||
final class ShinkanConnection
|
||||
{
|
||||
public const SETTING = 'shinkan.anbindung';
|
||||
|
||||
public const SERVER = 'server';
|
||||
|
||||
public const ADMIN = 'verwaltung';
|
||||
|
||||
public function url(): ?string
|
||||
{
|
||||
$value = $this->fromServer('services.shinkan.url') ?? ($this->stored()['url'] ?? null);
|
||||
|
||||
return filled($value) ? rtrim(trim((string) $value), '/') : null;
|
||||
}
|
||||
|
||||
public function apiKey(): ?string
|
||||
{
|
||||
$server = $this->fromServer('services.shinkan.key');
|
||||
|
||||
if ($server !== null) {
|
||||
return $server;
|
||||
}
|
||||
|
||||
$encrypted = $this->stored()['key'] ?? null;
|
||||
|
||||
if (! is_string($encrypted) || $encrypted === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return Crypt::decryptString($encrypted);
|
||||
} catch (DecryptException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Woher kommt der Wert? `server`, `verwaltung` oder null.
|
||||
*/
|
||||
public function source(string $field): ?string
|
||||
{
|
||||
$config = $field === 'url' ? 'services.shinkan.url' : 'services.shinkan.key';
|
||||
|
||||
if ($this->fromServer($config) !== null) {
|
||||
return self::SERVER;
|
||||
}
|
||||
|
||||
return filled($this->stored()[$field] ?? null) && ($field === 'url' || $this->apiKey() !== null) ? self::ADMIN : null;
|
||||
}
|
||||
|
||||
public function configured(): bool
|
||||
{
|
||||
return $this->url() !== null && $this->apiKey() !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Speichert die in der Verwaltung gepflegten Werte. Ein leerer Schlüssel lässt den gespeicherten unverändert.
|
||||
* Werte aus der Umgebung des Servers bleiben davon unberührt und behalten den Vorrang.
|
||||
*/
|
||||
public function save(?string $url, ?string $key, ?User $by = null): void
|
||||
{
|
||||
$stored = $this->stored();
|
||||
$url = trim((string) $url);
|
||||
|
||||
if ($url !== '') {
|
||||
$parts = parse_url($url);
|
||||
|
||||
if ($parts === false || ! in_array($parts['scheme'] ?? '', ['http', 'https'], true) || empty($parts['host'])) {
|
||||
throw new DomainRuleException('Die Adresse ist keine gültige Internetadresse. Sie beginnt mit https:// (oder http://).');
|
||||
}
|
||||
|
||||
$stored['url'] = rtrim($url, '/');
|
||||
} else {
|
||||
unset($stored['url']);
|
||||
}
|
||||
|
||||
$key = trim((string) $key);
|
||||
|
||||
if ($key !== '') {
|
||||
$stored['key'] = Crypt::encryptString($key);
|
||||
}
|
||||
|
||||
$this->write($stored, $by);
|
||||
}
|
||||
|
||||
public function forgetKey(?User $by = null): void
|
||||
{
|
||||
$stored = $this->stored();
|
||||
unset($stored['key']);
|
||||
$this->write($stored, $by);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function stored(): array
|
||||
{
|
||||
$value = SiteSetting::query()->find(self::SETTING)?->value;
|
||||
|
||||
return is_array($value) ? $value : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $stored
|
||||
*/
|
||||
private function write(array $stored, ?User $by): void
|
||||
{
|
||||
SiteSetting::query()->updateOrCreate(['key' => self::SETTING], ['value' => $stored, 'updated_by' => $by?->id]);
|
||||
}
|
||||
|
||||
private function fromServer(string $config): ?string
|
||||
{
|
||||
$value = config($config);
|
||||
|
||||
return is_string($value) && trim($value) !== '' ? trim($value) : null;
|
||||
}
|
||||
}
|
||||
@@ -34,22 +34,25 @@ class ShinkanImport
|
||||
|
||||
public function configured(): bool
|
||||
{
|
||||
return filled(config('services.shinkan.url')) && filled(config('services.shinkan.key'));
|
||||
return app(ShinkanConnection::class)->configured();
|
||||
}
|
||||
|
||||
/**
|
||||
* Zustand der Anbindung ohne Werte: Adresse und Schlüssel stehen nur in der Umgebungsdatei des Servers.
|
||||
* Zustand der Anbindung ohne Werte: ob Adresse und Schlüssel vorhanden sind und woher sie kommen.
|
||||
*
|
||||
* @return array{url: bool, key: bool, host: string|null}
|
||||
* @return array{url: bool, key: bool, host: string|null, url_source: string|null, key_source: string|null}
|
||||
*/
|
||||
public function status(): array
|
||||
{
|
||||
$url = (string) config('services.shinkan.url');
|
||||
$connection = app(ShinkanConnection::class);
|
||||
$url = $connection->url();
|
||||
|
||||
return [
|
||||
'url' => filled($url),
|
||||
'key' => filled(config('services.shinkan.key')),
|
||||
'host' => filled($url) ? (parse_url($url, PHP_URL_HOST) ?: null) : null,
|
||||
'url' => $url !== null,
|
||||
'key' => $connection->apiKey() !== null,
|
||||
'host' => $url !== null ? (parse_url($url, PHP_URL_HOST) ?: null) : null,
|
||||
'url_source' => $connection->source('url'),
|
||||
'key_source' => $connection->source('key'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -62,7 +65,7 @@ public function status(): array
|
||||
public function check(): array
|
||||
{
|
||||
if (! $this->configured()) {
|
||||
return ['ok' => false, 'message' => 'Die Anbindung ist nicht eingerichtet. Adresse und Schlüssel fehlen in der Umgebung des Servers.'];
|
||||
return ['ok' => false, 'message' => 'Die Anbindung ist nicht eingerichtet. Adresse und Zugangsschlüssel fehlen.'];
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -81,7 +84,7 @@ public function fetch(): ExerciseImportRun
|
||||
$run = ExerciseImportRun::query()->create(['status' => 'laeuft', 'started_at' => now()]);
|
||||
|
||||
if (! $this->configured()) {
|
||||
return $this->finish($run, 'fehler', error: 'Die Anbindung ist nicht eingerichtet. Adresse und Schlüssel fehlen in der Umgebung.');
|
||||
return $this->finish($run, 'fehler', error: 'Die Anbindung ist nicht eingerichtet. Adresse und Zugangsschlüssel fehlen.');
|
||||
}
|
||||
|
||||
$previous = ExerciseImportRun::query()->where('status', 'ok')->orderByDesc('id')->value('cursor');
|
||||
@@ -340,10 +343,11 @@ private function createComposition(string $title, string $summary): Composition
|
||||
*/
|
||||
private function get(string $path, array $query = []): array
|
||||
{
|
||||
$response = Http::withToken((string) config('services.shinkan.key'))
|
||||
$connection = app(ShinkanConnection::class);
|
||||
$response = Http::withToken((string) $connection->apiKey())
|
||||
->acceptJson()
|
||||
->timeout(20)
|
||||
->get(rtrim((string) config('services.shinkan.url'), '/').'/api/distribution/v1'.$path, $query)
|
||||
->get((string) $connection->url().'/api/distribution/v1'.$path, $query)
|
||||
->throw();
|
||||
|
||||
$json = $response->json();
|
||||
|
||||
Reference in new issue
Block a user