['status'=>int, 'body'=>string]. */ public function __construct(?string $baseUrl = null, ?callable $transport = null) { $url = rtrim($baseUrl ?? Settings::get('IAM_SOURCE_API_URL'), '/'); $parts = parse_url($url); if (!$parts || !in_array($parts['scheme'] ?? '', ['http', 'https'], true) || empty($parts['host']) || isset($parts['user']) || isset($parts['pass']) || isset($parts['query']) || isset($parts['fragment']) || preg_match('/[\x00-\x20\x7f]/', $url)) { throw new ApiError(503, '共享数据源尚未正确配置'); } if ($parts['scheme'] === 'http' && !in_array(strtolower($parts['host']), ['localhost', '127.0.0.1', '[::1]', '::1'], true)) { throw new ApiError(503, '远程共享数据源必须使用HTTPS'); } $this->baseUrl = $url; $this->transport = $transport === null ? null : \Closure::fromCallable($transport); } public function sourceKey(): string { return hash('sha256', $this->baseUrl); } public function request(string $method, string $path, array $data = [], string $token = ''): array { if (!in_array($method . ' ' . $path, self::ALLOWED, true)) { throw new ApiError(404, '接口不存在'); } $url = $this->baseUrl . '/' . $path; $headers = ['Accept: application/json', 'Content-Type: application/json']; if ($token !== '') { if (strlen($token) > 2048 || preg_match('/[\x00-\x20\x7f]/', $token)) { throw new ApiError(502, '共享数据源响应异常'); } $headers[] = 'token: ' . $token; } $body = ''; if ($method === 'GET' && $data !== []) { $url .= '?' . http_build_query($data, '', '&', PHP_QUERY_RFC3986); } elseif ($method === 'POST') { $body = json_encode((object) $data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE); } try { $response = $this->transport ? ($this->transport)($method, $url, $headers, $body) : $this->curl($method, $url, $headers, $body); } catch (\Throwable $error) { throw new ApiError(502, '无法连接共享数据源,请稍后重试'); } $status = $response['status'] ?? 0; $raw = $response['body'] ?? null; if (!is_int($status) || !is_string($raw) || strlen($raw) > self::MAX_RESPONSE || $status < 200 || $status >= 300) { throw new ApiError($status === 401 ? 401 : 502, $status === 401 ? '登录已失效,请重新登录' : '共享数据源暂时不可用'); } try { $decoded = json_decode($raw, true, 64, JSON_THROW_ON_ERROR); } catch (\JsonException $error) { throw new ApiError(502, '共享数据源响应异常'); } if (!is_array($decoded) || !isset($decoded['code']) || !is_int($decoded['code']) || !array_key_exists('data', $decoded)) { throw new ApiError(502, '共享数据源响应异常'); } if ($decoded['code'] !== 1) { // Credential proof has no session state; never echo source diagnostics or account existence. if ($path === 'identity.session/verify') { if ($decoded['code'] === 10) { throw new ApiError(403, '请先到原后台完成企业微信绑定'); } throw new ApiError(401, '账号或密码错误,或账号已停用'); } if ($decoded['code'] === -1) { throw new ApiError(401, '登录已失效,请重新登录'); } if ($decoded['code'] === 10) { throw new ApiError(403, '请先到原后台完成企业微信绑定'); } if ($path === 'identity.session/login') { throw new ApiError(401, '账号或密码错误,或账号已停用'); } throw new ApiError(422, self::safeMessage($decoded['msg'] ?? null)); } if (!is_array($decoded['data'])) { throw new ApiError(502, '共享数据源响应异常'); } return $decoded['data']; } private static function safeMessage(mixed $message): string { if (!is_string($message) || $message === '' || mb_strlen($message) > 120 || preg_match('~(?:SQL|PDO|mysql|sqlite|exception|stack|trace|token|secret|password|\.env|https?://|[a-f0-9]{24,}|[A-Za-z]:[\\\\/]|/[A-Za-z0-9_.-]+/|[\x00-\x1f\x7f])~i', $message) || !preg_match('/[\x{4e00}-\x{9fff}]/u', $message)) { return '原系统未能完成此操作,请检查输入或联系管理员'; } return $message; } private function curl(string $method, string $url, array $headers, string $body): array { if (!function_exists('curl_init')) { throw new \RuntimeException('transport unavailable'); } $handle = curl_init($url); $raw = ''; curl_setopt_array($handle, [ CURLOPT_CUSTOMREQUEST => $method, CURLOPT_HTTPHEADER => $headers, CURLOPT_FOLLOWLOCATION => false, CURLOPT_MAXREDIRS => 0, CURLOPT_CONNECTTIMEOUT => 3, CURLOPT_TIMEOUT => 15, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, CURLOPT_WRITEFUNCTION => static function ($curl, string $chunk) use (&$raw): int { if (strlen($raw) + strlen($chunk) > self::MAX_RESPONSE) { return 0; } $raw .= $chunk; return strlen($chunk); }, ]); if ($method === 'POST') { curl_setopt($handle, CURLOPT_POSTFIELDS, $body); } try { if (curl_exec($handle) === false) { throw new \RuntimeException('transport failed'); } return ['status' => (int) curl_getinfo($handle, CURLINFO_RESPONSE_CODE), 'body' => $raw]; } finally { curl_close($handle); } } }