← All changes
|
app/Http/Controllers/ChatMessageParserController.php
+51
-13
1.10.3
→
2.4.5
View file →
| @@ -1,23 +1,22 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Http\Controllers; |
| 4 | 4 | |
| 5 | -use FluentSupport\Framework\Request\Request; | |
| 5 | +use FluentSupport\Framework\Http\Request\Request; | |
| 6 | +use FluentSupport\App\Services\Helper; | |
| 6 | 7 | use FluentSupport\App\Services\ThirdParty\HandleSlackEvent; |
| 7 | 8 | use FluentSupport\App\Services\ThirdParty\HandleTelegramEvent; |
| 8 | 9 | |
| 9 | 10 | /** |
| 10 | - * ChatMessageParserController class is responsible for getting information from integrated 3rd party request and response | |
| 11 | - * @package FluentSupport\App\Http\Controllers | |
| 12 | - * | |
| 13 | - * @version 1.0.0 | |
| 11 | + * Incoming webhooks from chat integrations (Telegram, Slack). | |
| 14 | 12 | */ |
| 15 | 13 | |
| 16 | 14 | class ChatMessageParserController extends Controller |
| 17 | 15 | { |
| 18 | 16 | /** |
| 19 | - * handleTelegramWebhook will get the content from telegram, check the data validity and create response in a ticket | |
| 17 | + * Records a Telegram reply as a ticket response. | |
| 18 | + * | |
| 20 | 19 | * @param Request $request |
| 21 | 20 | * @param HandleTelegramEvent $handler |
| 22 | 21 | * @param string $token |
| 23 | 22 | * @throws Exception |
| @@ -24,8 +23,15 @@ | ||
| 24 | 23 | * @return mixed |
| 25 | 24 | */ |
| 26 | 25 | public function handleTelegramWebhook(Request $request, HandleTelegramEvent $handler, $token) |
| 27 | 26 | { |
| 27 | + if (!$this->verifyWebhookSignature('telegram', $request)) { | |
| 28 | + return $this->sendError([ | |
| 29 | + 'message' => __('Invalid request signature.', 'fluent-support'), | |
| 30 | + 'status' => false | |
| 31 | + ], 403); | |
| 32 | + } | |
| 33 | + | |
| 28 | 34 | try { |
| 29 | 35 | return $this->sendSuccess([ |
| 30 | 36 | 'message' => __('Response has been successfully recorded', 'fluent-support'), |
| 31 | 37 | 'status' => true, |
| @@ -32,9 +38,9 @@ | ||
| 32 | 38 | 'data' => $handler->handleEvent($request->all(), $token) |
| 33 | 39 | ]); |
| 34 | 40 | } catch (\Exception $e) { |
| 35 | 41 | return $this->sendError([ |
| 36 | - 'message' => $e->getMessage(), | |
| 42 | + 'message' => Helper::getSafeErrorMessage($e), | |
| 37 | 43 | 'status' => false |
| 38 | 44 | ]); |
| 39 | 45 | } |
| 40 | 46 | } |
| @@ -39,24 +45,56 @@ | ||
| 39 | 45 | } |
| 40 | 46 | } |
| 41 | 47 | |
| 42 | 48 | /** |
| 43 | - * handleSlackEvent responsible for getting information from integrated slack request and response | |
| 49 | + * @param Request $request | |
| 44 | 50 | * @param HandleSlackEvent $handler |
| 45 | - * @param $token | |
| 51 | + * @param string $token | |
| 46 | 52 | * @return array |
| 47 | 53 | */ |
| 48 | - public function handleSlackEvent(HandleSlackEvent $handler, $token) | |
| 54 | + public function handleSlackEvent(Request $request, HandleSlackEvent $handler, $token) | |
| 49 | 55 | { |
| 50 | - try{ | |
| 51 | - $this->sendSuccess([ | |
| 56 | + if (!$this->verifyWebhookSignature('slack', $request)) { | |
| 57 | + return $this->sendError([ | |
| 58 | + 'message' => __('Invalid request signature.', 'fluent-support'), | |
| 59 | + 'status' => false | |
| 60 | + ], 403); | |
| 61 | + } | |
| 62 | + | |
| 63 | + if ($request->getSafe('type', 'sanitize_text_field') === 'url_verification') { | |
| 64 | + return new \WP_REST_Response($request->getSafe('challenge', 'sanitize_text_field'), 200, [ | |
| 65 | + 'Content-Type' => 'text/plain; charset=utf-8' | |
| 66 | + ]); | |
| 67 | + } | |
| 68 | + | |
| 69 | + try { | |
| 70 | + return $this->sendSuccess([ | |
| 52 | 71 | 'message' => 'received', |
| 53 | 72 | 'result' => $handler->handleEvent($token) |
| 54 | 73 | ]); |
| 55 | 74 | } catch (\Exception $e) { |
| 56 | 75 | return $this->sendError([ |
| 57 | - 'message' => $e->getMessage(), | |
| 76 | + 'message' => Helper::getSafeErrorMessage($e), | |
| 58 | 77 | 'status' => false |
| 59 | 78 | ]); |
| 60 | 79 | } |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Signature checks are opt-in via fluent_support/verify_webhook_signature_{platform}. | |
| 84 | + * With no verifier hooked, the handlers' token check is the only guard. | |
| 85 | + * | |
| 86 | + * @param string $platform 'telegram' or 'slack' | |
| 87 | + * @param Request $request | |
| 88 | + * @return bool | |
| 89 | + */ | |
| 90 | + private function verifyWebhookSignature($platform, Request $request) | |
| 91 | + { | |
| 92 | + $hookName = 'fluent_support/verify_webhook_signature_' . $platform; | |
| 93 | + | |
| 94 | + if (!has_filter($hookName)) { | |
| 95 | + return true; | |
| 96 | + } | |
| 97 | + | |
| 98 | + return (bool) apply_filters($hookName, false, $request); | |
| 61 | 99 | } |
| 62 | 100 | } |