feat: phase 1 — InsForge session auth, role gates, refresh middleware, auth test suites

This commit is contained in:
sean
2026-08-25 18:01:11 +07:00
parent 046cefc56c
commit aad2b5e256
15 changed files with 613 additions and 36 deletions
+12 -7
View File
@@ -13,8 +13,7 @@ class InsForgeClient
private readonly string $apiKey,
private readonly int $timeout = 30,
private readonly ?string $accessToken = null,
) {
}
) {}
public function withToken(?string $accessToken): self
{
@@ -48,7 +47,7 @@ class InsForgeClient
public function getRecord(string $table, string $id, array $params = []): ?array
{
$params['id'] = 'eq.' . $id;
$params['id'] = 'eq.'.$id;
$params['limit'] = 1;
return $this->listRecords($table, $params)[0] ?? null;
@@ -99,13 +98,13 @@ class InsForgeClient
$query = [];
foreach ($filters as $column => $value) {
$query[$column] = str_starts_with((string) $value, 'eq.') ? $value : 'eq.' . $value;
$query[$column] = str_starts_with((string) $value, 'eq.') ? $value : 'eq.'.$value;
}
return $query;
}
private function request(string $method, string $path, array $body = null, array $query = [], bool $asUser = false): Response
public function send(string $method, string $path, array $body = [], array $query = [], bool $asUser = false, array $headers = []): Response
{
if ($asUser && $this->accessToken === null) {
throw new InsForgeException(null, 401, 'No user access token available for this request.');
@@ -114,13 +113,14 @@ class InsForgeClient
try {
$http = Http::baseUrl(rtrim($this->baseUrl, '/'))
->timeout($this->timeout)
->withHeaders($headers)
->withToken($asUser ? $this->accessToken : $this->apiKey);
$verb = strtolower($method);
$response = in_array($verb, ['get', 'head'])
? $http->{$verb}($path, $query)
: $http->{$verb}($path, $body ?? []);
: $http->{$verb}($path, $body);
throw_unless($response->successful(), fn () => InsForgeException::fromResponse(
$response->status(),
@@ -129,7 +129,12 @@ class InsForgeClient
return $response;
} catch (ConnectionException $e) {
throw new InsForgeException(null, 0, 'InsForge connection failed: ' . $e->getMessage());
throw new InsForgeException(null, 0, 'InsForge connection failed: '.$e->getMessage());
}
}
private function request(string $method, string $path, ?array $body = null, array $query = [], bool $asUser = false): Response
{
return $this->send($method, $path, $body ?? [], $query, $asUser);
}
}