Skip to content

Commit 39d7744

Browse files
authored
feat: Verify server header. (#55)
1 parent 00f0ce6 commit 39d7744

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

src/Client.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function abortIn(float $seconds): void
3030
public function ping(): void
3131
{
3232
$response = $this->httpClient->get('/api/v1/ping');
33+
if (!$this->isValidServerHeader($response)) {
34+
throw new RuntimeException('Server must be EventSourcingDB.');
35+
}
3336
$status = $response->getStatusCode();
3437

3538
if ($status !== 200) {
@@ -60,6 +63,9 @@ public function verifyApiToken(): void
6063
'/api/v1/verify-api-token',
6164
$this->apiToken,
6265
);
66+
if (!$this->isValidServerHeader($response)) {
67+
throw new RuntimeException('Server must be EventSourcingDB.');
68+
}
6369
$status = $response->getStatusCode();
6470

6571
if ($status !== 200) {
@@ -98,6 +104,9 @@ public function writeEvents(array $events, array $preconditions = []): array
98104
$this->apiToken,
99105
$requestBody,
100106
);
107+
if (!$this->isValidServerHeader($response)) {
108+
throw new RuntimeException('Server must be EventSourcingDB.');
109+
}
101110
$status = $response->getStatusCode();
102111

103112
if ($status !== 200) {
@@ -150,6 +159,9 @@ public function readEvents(string $subject, ReadEventsOptions $readEventsOptions
150159
'options' => $readEventsOptions,
151160
],
152161
);
162+
if (!$this->isValidServerHeader($response)) {
163+
throw new RuntimeException('Server must be EventSourcingDB.');
164+
}
153165
$status = $response->getStatusCode();
154166

155167
if ($status !== 200) {
@@ -198,7 +210,9 @@ public function runEventQlQuery(string $query): iterable
198210
'query' => $query,
199211
],
200212
);
201-
213+
if (!$this->isValidServerHeader($response)) {
214+
throw new RuntimeException('Server must be EventSourcingDB.');
215+
}
202216
$status = $response->getStatusCode();
203217

204218
if ($status !== 200) {
@@ -235,7 +249,9 @@ public function observeEvents(string $subject, ObserveEventsOptions $observeEven
235249
'options' => $observeEventsOptions,
236250
],
237251
);
238-
252+
if (!$this->isValidServerHeader($response)) {
253+
throw new RuntimeException('Server must be EventSourcingDB.');
254+
}
239255
$status = $response->getStatusCode();
240256
if ($status !== 200) {
241257
throw new RuntimeException(sprintf(
@@ -286,7 +302,9 @@ public function registerEventSchema(string $eventType, array $schema): void
286302
'schema' => $schema,
287303
],
288304
);
289-
305+
if (!$this->isValidServerHeader($response)) {
306+
throw new RuntimeException('Server must be EventSourcingDB.');
307+
}
290308
$status = $response->getStatusCode();
291309
if ($status !== 200) {
292310
throw new RuntimeException(sprintf(
@@ -305,7 +323,9 @@ public function readSubjects(string $baseSubject): iterable
305323
'baseSubject' => $baseSubject,
306324
],
307325
);
308-
326+
if (!$this->isValidServerHeader($response)) {
327+
throw new RuntimeException('Server must be EventSourcingDB.');
328+
}
309329
$status = $response->getStatusCode();
310330
if ($status !== 200) {
311331
throw new RuntimeException(sprintf(
@@ -337,7 +357,9 @@ public function readEventTypes(): iterable
337357
'/api/v1/read-event-types',
338358
$this->apiToken,
339359
);
340-
360+
if (!$this->isValidServerHeader($response)) {
361+
throw new RuntimeException('Server must be EventSourcingDB.');
362+
}
341363
$status = $response->getStatusCode();
342364
if ($status !== 200) {
343365
throw new RuntimeException(sprintf(
@@ -376,7 +398,9 @@ public function readEventType(string $eventType): EventType
376398
'eventType' => $eventType,
377399
],
378400
);
379-
401+
if (!$this->isValidServerHeader($response)) {
402+
throw new RuntimeException('Server must be EventSourcingDB.');
403+
}
380404
$status = $response->getStatusCode();
381405
if ($status !== 200) {
382406
throw new RuntimeException(sprintf(
@@ -401,4 +425,14 @@ public function readEventType(string $eventType): EventType
401425
$data['schema'] ?? [],
402426
);
403427
}
428+
429+
private function isValidServerHeader(\Thenativeweb\Eventsourcingdb\Stream\Response $response): bool
430+
{
431+
$serverHeader = $response->getHeader('Server');
432+
433+
if ($serverHeader === []) {
434+
return false;
435+
}
436+
return str_starts_with($serverHeader[0], 'EventSourcingDB/');
437+
}
404438
}

0 commit comments

Comments
 (0)