From 46500a0e7450ce03f2cf79bb1b3f4611ab71f873 Mon Sep 17 00:00:00 2001 From: eug-L Date: Wed, 5 Mar 2025 10:09:52 +0800 Subject: [PATCH 1/4] add js api key field & hashing for visitor recognition --- src/core/TawktoGenerator.php | 103 ++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 3 deletions(-) diff --git a/src/core/TawktoGenerator.php b/src/core/TawktoGenerator.php index cbd870a..b20c328 100644 --- a/src/core/TawktoGenerator.php +++ b/src/core/TawktoGenerator.php @@ -54,12 +54,22 @@ public function getWidget() { $display_opts = $options; // Default value. $enable_visitor_recognition = TRUE; + $secure_mode_enabled = FALSE; + $js_api_key = NULL; if (!is_null($display_opts)) { $display_opts = json_decode($display_opts); if (!is_null($display_opts->enable_visitor_recognition)) { $enable_visitor_recognition = $display_opts->enable_visitor_recognition; } + + if (!is_null($display_opts->secure_mode_enabled)) { + $secure_mode_enabled = $display_opts->secure_mode_enabled; + } + + if (!is_null($display_opts->js_api_key)) { + $js_api_key = $display_opts->js_api_key; + } } if ($enable_visitor_recognition) { @@ -67,11 +77,22 @@ public function getWidget() { if ($user) { $username = $user->get('name')->value; $usermail = $user->get('mail')->value; + $hash = NULL; + + $keyModule = $this->getKeyModule(); + $keys = $keyModule['keys']; + + if ($secure_mode_enabled) { + if (!is_null($js_api_key) && isset($keys[$js_api_key])) { + $hash = hash_hmac('sha256', $usermail, $keys[$js_api_key]->getKeyValue()); + } + } $apiString = 'Tawk_API.visitor = { name : "' . $username . '", - email : "' . $usermail . '", - };'; + email : "' . $usermail . '",' . + (!is_null($hash) ? PHP_EOL . 'hash : "' . $hash . '",' : '') . + '};'; } } @@ -95,6 +116,26 @@ public function getWidget() { return $output; } + /** + * Get keys. + * + * @return array + * Key module installed and keys + */ + private function getKeyModule() { + $installed = \Drupal::getContainer()->has('key.repository'); + $keys = []; + + if ($installed) { + $keys = \Drupal::service('key.repository')->getKeys(); + } + + return [ + 'installed' => $installed, + 'keys' => $keys, + ]; + } + /** * Check widget visibility based on set options. * @@ -214,6 +255,11 @@ public function getIframe() { if (!is_null($display_opts)) { $display_opts = json_decode($display_opts); } + + $keyModule = $this->getKeyModule(); + $keyModuleInstalled = $keyModule['installed']; + $keys = $keyModule['keys']; + ob_start(); ?> @@ -284,7 +330,7 @@ public function getIframe() {
-
Visibility Settings
+
Visibility Options
@@ -453,6 +499,52 @@ public function getIframe() {
+
+
+
Security Options
+
+
Note: If Secure Mode is enabled on your property, please enter your Javascript API Key to ensure visitor recognition works correctly.
+
+ +
+ secure_mode_enabled) { + $checked = ''; + } + ?> + /> +
+
+
+ +
+ + + The Key module is not installed. Please install it and create a key for JS API Key. + + + + Keys: + +
To create a new key, go to /admin/config/system/keys
+ + +
+
+