Neues Plugin IT-Service (OpenNIT); Abteilung bei „Geschrieben von“
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m41s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m32s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m30s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m35s
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m27s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m41s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m32s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m30s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m35s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
c713194a83
commit
c1117829c3
139 changed files with 2455 additions and 398 deletions
158
plugins-core/it-service/src/OpenNit/Client.php
Normal file
158
plugins-core/it-service/src/OpenNit/Client.php
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\ItService\OpenNit;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* Zugriff auf die OpenNIT-API (/api/ex) mit Bearer-Token und HMAC-Signatur v2:
|
||||
*
|
||||
* v2 \n METHODE \n PFAD \n QUERY (sortiert) \n EMAIL (klein) \n sha256(BODY) \n TIMESTAMP \n NONCE
|
||||
*
|
||||
* Die E-Mail-Zeile bindet personenbezogene Abfragen („my-tickets“ …) an das
|
||||
* Signing-Secret – so liefert OpenNIT nur Daten der angemeldeten Person.
|
||||
*/
|
||||
class Client
|
||||
{
|
||||
public function __construct(private readonly Settings $settings) {}
|
||||
|
||||
/**
|
||||
* @param array<string, scalar|null> $query
|
||||
* @param array<string, mixed>|null $body
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function get(string $path, array $query = []): array
|
||||
{
|
||||
return $this->json($this->send('GET', $path, $query));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $body
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function post(string $path, array $body = []): array
|
||||
{
|
||||
return $this->json($this->send('POST', $path, [], $body));
|
||||
}
|
||||
|
||||
/**
|
||||
* Binärantwort (z. B. Ticket-Anhang).
|
||||
*
|
||||
* @param array<string, scalar|null> $query
|
||||
*/
|
||||
public function download(string $path, array $query = []): Response
|
||||
{
|
||||
$response = $this->send('GET', $path, $query);
|
||||
|
||||
if (! $response->successful()) {
|
||||
throw OpenNitException::fromStatus($response->status(), (array) $response->json());
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stammdaten (Kategorien, Standorte, Gerätetypen) – kurz zwischengespeichert.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function cached(string $path, int $seconds = 600): array
|
||||
{
|
||||
return Cache::remember('it-service:'.md5($this->settings->url().$path), $seconds, fn () => $this->get($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Kanonische Zeichenkette der Signatur (öffentlich für Tests).
|
||||
*
|
||||
* @param array<string, scalar|null> $query
|
||||
*/
|
||||
public static function canonical(string $method, string $path, array $query, string $email, string $body, int $timestamp, string $nonce): string
|
||||
{
|
||||
ksort($query);
|
||||
|
||||
return implode("\n", [
|
||||
'v2',
|
||||
strtoupper($method),
|
||||
$path,
|
||||
http_build_query($query),
|
||||
mb_strtolower($email),
|
||||
hash('sha256', $body),
|
||||
(string) $timestamp,
|
||||
$nonce,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, scalar|null> $query
|
||||
* @param array<string, mixed>|null $body
|
||||
*/
|
||||
private function send(string $method, string $path, array $query = [], ?array $body = null): Response
|
||||
{
|
||||
if (! $this->settings->configured()) {
|
||||
throw new OpenNitException('Der IT-Service ist noch nicht eingerichtet. Bitte die Administration informieren.');
|
||||
}
|
||||
|
||||
$query = array_filter($query, fn ($value) => $value !== null && $value !== '');
|
||||
$base = $this->settings->url();
|
||||
$basePath = rtrim((string) parse_url($base, PHP_URL_PATH), '/');
|
||||
$fullPath = $basePath.'/'.ltrim($path, '/');
|
||||
$raw = $body === null ? '' : json_encode($body, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
$email = (string) ($query['email'] ?? (is_array($body) ? ($body['email'] ?? '') : ''));
|
||||
$timestamp = time();
|
||||
$nonce = bin2hex(random_bytes(16));
|
||||
$signature = hash_hmac('sha256', self::canonical($method, $fullPath, $query, $email, $raw, $timestamp, $nonce), $this->settings->secret());
|
||||
|
||||
$request = Http::withToken($this->settings->token())
|
||||
->withHeaders([
|
||||
'X-OpenNIT-Timestamp' => (string) $timestamp,
|
||||
'X-OpenNIT-Nonce' => $nonce,
|
||||
'X-OpenNIT-Signature' => $signature,
|
||||
'Accept' => 'application/json',
|
||||
])
|
||||
->timeout(15)
|
||||
->connectTimeout(5);
|
||||
|
||||
$url = rtrim((string) preg_replace('#(https?://[^/]+).*#', '$1', $base), '/').$fullPath.($query ? '?'.http_build_query(self::sorted($query)) : '');
|
||||
|
||||
try {
|
||||
return $body === null
|
||||
? $request->send($method, $url)
|
||||
: $request->withBody($raw, 'application/json')->send($method, $url);
|
||||
} catch (ConnectionException $e) {
|
||||
Log::warning('OpenNIT nicht erreichbar: '.$e->getMessage());
|
||||
|
||||
throw new OpenNitException('Der IT-Service ist gerade nicht erreichbar. Bitte später erneut versuchen.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function json(Response $response): array
|
||||
{
|
||||
$data = (array) $response->json();
|
||||
|
||||
if (! $response->successful()) {
|
||||
Log::info("OpenNIT antwortete {$response->status()}", ['body' => mb_substr($response->body(), 0, 500)]);
|
||||
|
||||
throw OpenNitException::fromStatus($response->status(), $data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, scalar|null> $query
|
||||
* @return array<string, scalar|null>
|
||||
*/
|
||||
private static function sorted(array $query): array
|
||||
{
|
||||
ksort($query);
|
||||
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
32
plugins-core/it-service/src/OpenNit/OpenNitException.php
Normal file
32
plugins-core/it-service/src/OpenNit/OpenNitException.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\ItService\OpenNit;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Fehler von OpenNIT – mit einer Meldung, die man Mitarbeitenden zeigen kann.
|
||||
*/
|
||||
class OpenNitException extends RuntimeException
|
||||
{
|
||||
public function __construct(string $message, public readonly int $status = 0, public readonly array $body = [])
|
||||
{
|
||||
parent::__construct($message, $status);
|
||||
}
|
||||
|
||||
public static function fromStatus(int $status, array $body): self
|
||||
{
|
||||
$detail = is_string($body['message'] ?? null) ? $body['message'] : (is_string($body['error'] ?? null) ? $body['error'] : null);
|
||||
|
||||
$message = match ($status) {
|
||||
400 => 'Die Angaben sind unvollständig oder ungültig.'.($detail ? " ({$detail})" : ''),
|
||||
401 => 'Die Verbindung zum IT-Service ist nicht richtig eingerichtet (Anmeldung abgelehnt). Bitte die Administration informieren.',
|
||||
403 => 'Der IT-Service erlaubt diesen Zugriff nicht.',
|
||||
404 => 'Nicht gefunden – vielleicht gehört der Eintrag zu einer anderen Person.',
|
||||
409 => $detail ?: 'Das Gerät ist in diesem Zeitraum nicht verfügbar.',
|
||||
default => 'Der IT-Service ist gerade nicht erreichbar. Bitte später erneut versuchen.',
|
||||
};
|
||||
|
||||
return new self($message, $status, $body);
|
||||
}
|
||||
}
|
||||
94
plugins-core/it-service/src/OpenNit/Settings.php
Normal file
94
plugins-core/it-service/src/OpenNit/Settings.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
namespace Inhaus\Plugins\ItService\OpenNit;
|
||||
|
||||
use App\Support\Settings as PortalSettings;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Zugangsdaten zu OpenNIT. Token und Signing-Secret liegen verschlüsselt in den Einstellungen.
|
||||
*/
|
||||
class Settings
|
||||
{
|
||||
public const URL = 'it_service.url';
|
||||
|
||||
public const TOKEN = 'it_service.token';
|
||||
|
||||
public const SECRET = 'it_service.secret';
|
||||
|
||||
/** null = Einstellung in OpenNIT, true = sofort reservieren, false = immer Anfrage */
|
||||
public const AUTO_RESERVE = 'it_service.auto_reserve';
|
||||
|
||||
public const INTRO = 'it_service.intro';
|
||||
|
||||
public function __construct(private readonly PortalSettings $settings) {}
|
||||
|
||||
public function url(): string
|
||||
{
|
||||
return rtrim((string) $this->settings->get(self::URL, ''), '/');
|
||||
}
|
||||
|
||||
public function token(): string
|
||||
{
|
||||
return $this->decrypt(self::TOKEN);
|
||||
}
|
||||
|
||||
public function secret(): string
|
||||
{
|
||||
return $this->decrypt(self::SECRET);
|
||||
}
|
||||
|
||||
public function configured(): bool
|
||||
{
|
||||
return $this->url() !== '' && $this->token() !== '' && $this->secret() !== '';
|
||||
}
|
||||
|
||||
public function autoReserve(): ?bool
|
||||
{
|
||||
$value = $this->settings->get(self::AUTO_RESERVE);
|
||||
|
||||
return $value === null ? null : (bool) $value;
|
||||
}
|
||||
|
||||
public function intro(): ?string
|
||||
{
|
||||
return $this->settings->get(self::INTRO) ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{url: string, token?: ?string, secret?: ?string, auto_reserve: ?string, intro: ?string} $data leere Token/Secret = unverändert
|
||||
*/
|
||||
public function save(array $data): void
|
||||
{
|
||||
$this->settings->set(self::URL, rtrim(trim($data['url']), '/'));
|
||||
|
||||
foreach ([self::TOKEN => $data['token'] ?? null, self::SECRET => $data['secret'] ?? null] as $key => $value) {
|
||||
if (filled($value)) {
|
||||
$this->settings->set($key, Crypt::encryptString(trim($value)));
|
||||
}
|
||||
}
|
||||
|
||||
$this->settings->set(self::AUTO_RESERVE, match ($data['auto_reserve'] ?? '') {
|
||||
'yes' => true,
|
||||
'no' => false,
|
||||
default => null,
|
||||
});
|
||||
$this->settings->set(self::INTRO, $data['intro'] ?? null);
|
||||
}
|
||||
|
||||
private function decrypt(string $key): string
|
||||
{
|
||||
$value = $this->settings->get($key);
|
||||
|
||||
if (! is_string($value) || $value === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
return Crypt::decryptString($value);
|
||||
} catch (Throwable) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue