$query * @param array|null $body * @return array */ public function get(string $path, array $query = []): array { return $this->json($this->send('GET', $path, $query)); } /** * @param array $body * @return array */ public function post(string $path, array $body = []): array { return $this->json($this->send('POST', $path, [], $body)); } /** * Binärantwort (z. B. Ticket-Anhang). * * @param array $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 */ 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 $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 $query * @param array|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 */ 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 $query * @return array */ private static function sorted(array $query): array { ksort($query); return $query; } }