bearer(); if ($token === '') { $this->json(null, 401, ['code' => 'unauthorized', 'message' => 'Missing bearer token']); } // First-party admin token (constant-time compare). if (defined('ADMIN_TOKEN') && hash_equals(ADMIN_TOKEN, $token)) { if ($this->throttle('admin', $max, $window)) { $this->json(null, 429, ['code' => 'rate_limited', 'message' => 'Too many requests']); } return; } // Programmatic api_auth key. $row = \Db::getRow( "SELECT `id`, `userid`, `active` FROM `api_auth` WHERE `apikey` = ? LIMIT 1", [$token] ); if (!$row || (int) $row['active'] !== 1) { $this->json(null, 401, ['code' => 'unauthorized', 'message' => 'Invalid API key']); } $this->apiUser = $row; if ($this->throttle('apikey:' . $row['id'], $max, $window)) { $this->json(null, 429, ['code' => 'rate_limited', 'message' => 'Too many requests']); } // NOTE: monthly/plan quota enforcement (api_plans/api_usage) is deferred to a later sub-project. } }