Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m35s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m32s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m54s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m53s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m1s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m17s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m31s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m11s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m33s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
167 lines
5.6 KiB
PHP
167 lines
5.6 KiB
PHP
<?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
|
||
{
|
||
$decoded = $response->json();
|
||
|
||
// Eine HTML-Seite (falsche Adresse, Anmeldeseite …) ist keine leere Liste.
|
||
if ($response->successful() && ! is_array($decoded)) {
|
||
Log::warning('OpenNIT lieferte keine JSON-Antwort', ['body' => mb_substr($response->body(), 0, 300)]);
|
||
|
||
throw new OpenNitException('Der IT-Service hat keine gültige API-Antwort geliefert. Bitte die API-Adresse prüfen (sie endet auf „/api/ex“).', 502);
|
||
}
|
||
|
||
$data = (array) $decoded;
|
||
|
||
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;
|
||
}
|
||
}
|