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

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-26 20:05:41 +00:00
parent c713194a83
commit c1117829c3
139 changed files with 2455 additions and 398 deletions

View 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;
}
}