initializeApi($request); if ($response !== null) { return $response; } $signature = $request->post('signature', $request->get('signature', '')); $secret = $request->post('secret', $request->get('secret', '')); $agentId = $request->post('agent_id', $request->get('agent_id', '')); $time = $request->post('time', $request->get('time', '')); if ($signature === '' || $secret === '' || $agentId === '' || $time === '') { return $this->error(__('Parameter %s can not be empty', ['signature/secret/agent_id/time'])); } $timestamp = (int) $time; if ($timestamp <= 0) { return $this->error(__('Invalid timestamp')); } $now = time(); if ($timestamp < $now - $this->timeTolerance || $timestamp > $now + $this->timeTolerance) { return $this->error(__('Timestamp expired')); } $admin = Admin::where('agent_id', $agentId)->find(); if (!$admin) { return $this->error(__('Agent not found')); } $channelId = (int) ($admin->channel_id ?? 0); if ($channelId <= 0) { return $this->error(__('Agent not found')); } $channel = ChannelManage::where('id', $channelId)->find(); if (!$channel || $channel->secret === '') { return $this->error(__('Agent not found')); } if ($channel->secret !== $secret) { return $this->error(__('Invalid agent or secret')); } $expectedSignature = hash_hmac('sha256', $agentId . $time, $channel->secret); if (!hash_equals($expectedSignature, $signature)) { return $this->error(__('Invalid signature')); } $expire = (int) config('buildadmin.agent_auth.token_expire', 86400); $payload = [ 'agent_id' => $agentId, 'channel_id' => $channel->id, 'admin_id' => $admin->id, ]; $authtoken = AgentJwt::encode($payload, $expire); return $this->success('', [ 'authtoken' => $authtoken, ]); } }