<?php
require __DIR__ . '/vendor/autoload.php';
use ShieldLabs\Webhook;
function readIdentification(string $id): array
{
$domain = (string) getenv('SHIELDLABS_DOMAIN_ID');
$uuid = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i';
if (!preg_match($uuid, $domain) || !preg_match($uuid, $id)) {
throw new InvalidArgumentException('Domain and identification must be UUIDs');
}
$handle = curl_init('https://account.shieldlabs.ai/api/v1/domains/' . $domain . '/history/identifications/' . $id);
if ($handle === false) throw new RuntimeException('Cannot initialize HTTP client');
curl_setopt_array($handle, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTPHEADER => ['Accept: application/json', 'Authorization: Bearer ' . getenv('SHIELDLABS_API_KEY')],
]);
$body = curl_exec($handle);
$status = curl_getinfo($handle, CURLINFO_RESPONSE_CODE);
curl_close($handle);
if (!is_string($body) || $status !== 200) throw new RuntimeException('History status ' . $status);
$card = json_decode($body, true, 512, JSON_THROW_ON_ERROR)['data'];
if (($card['id'] ?? null) !== $id || ($card['domain_id'] ?? null) !== $domain || ($card['type'] ?? null) !== 'identification') {
throw new RuntimeException('History identity mismatch');
}
return $card;
}
function allowSignup(string $id, ?string $expectedUserHid = null): bool
{
$card = readIdentification($id);
$risk = $card['risk'] ?? [];
$signals = $card['signals'] ?? [];
if (!is_array($risk) || !is_array($signals)) return false;
$score = $risk['score'] ?? null;
if (($risk['state'] ?? null) !== 'ok' || ($signals['state'] ?? null) !== 'ok'
|| (!is_int($score) && !is_float($score)) || !is_finite((float) $score) || $score < 0 || $score > 100) return false;
$band = $score < 30 ? 'trusted' : ($score < 60 ? 'suspicious' : 'dangerous');
$items = $signals['items'] ?? null;
if (($risk['band'] ?? null) !== $band || !is_array($items) || $score >= 60) return false;
foreach ($items as $signal) {
if (!is_string($signal) || in_array($signal, ['banned_ip', 'browser_automation'], true)) return false;
}
$created = new DateTimeImmutable($card['created_at'] ?? 'invalid');
$age = microtime(true) - (float) $created->format('U.u');
if ($age < 0 || $age > 300 || ($expectedUserHid !== null && ($card['user_hid'] ?? null) !== $expectedUserHid)) return false;
// Demonstration only: replace this with a unique database key on domain + Identification ID.
$key = (string) getenv('SHIELDLABS_DOMAIN_ID') . ':' . $id;
$marker = @fopen(sys_get_temp_dir() . '/shieldlabs-used-' . hash('sha256', $key), 'x');
if ($marker === false) return false;
fclose($marker);
return true;
}
function verifyWebhook(string $rawBody, ?string $signature): array
{
$secret = (string) getenv('SHIELDLABS_WEBHOOK_SECRET');
if ($secret === '') throw new RuntimeException('Webhook secret is not configured');
if (!Webhook::verifySignature($rawBody, $signature, $secret)) throw new RuntimeException('Invalid signature');
$event = json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR);
if (!is_string($event['event_id'] ?? null) || $event['event_id'] === '') throw new RuntimeException('Missing event_id');
return $event;
}