Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/secure-mode-support #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use-strict';

module.exports = {
'root': true,
'env': {
'browser': true,
'jquery': true
Expand Down
20 changes: 20 additions & 0 deletions tawkto/assets/js/tawk.admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ jQuery(

jQuery( this ).addClass( 'reverse' );
});

if ( jQuery( '#enable-visitor-recognition' ).prop( 'checked' ) ) {
jQuery( '.tawk-selected-visitor' ).show();
jQuery( '#js-api-key' ).prop( 'disabled', false );
} else {
jQuery( '.tawk-selected-visitor' ).hide();
jQuery( '#js-api-key' ).prop( 'disabled', true );
}

jQuery( '#enable-visitor-recognition' ).change(
function() {
if ( this.checked ) {
jQuery( '.tawk-selected-visitor' ).fadeIn();
jQuery( '#js-api-key' ).prop( 'disabled', false );
} else {
jQuery( '.tawk-selected-visitor' ).fadeOut();
jQuery( '#js-api-key' ).prop( 'disabled', true );
}
}
);
}
);

Expand Down
1 change: 1 addition & 0 deletions tawkto/includes/default_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
'display_on_productpage' => 0,
'display_on_producttag' => 0,
'enable_visitor_recognition' => 1,
'js_api_key' => '',
),
);
83 changes: 83 additions & 0 deletions tawkto/tawkto.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class TawkTo_Settings {
const TAWK_VISIBILITY_OPTIONS = 'tawkto-visibility-options';
const TAWK_ACTION_SET_WIDGET = 'tawkto-set-widget';
const TAWK_ACTION_REMOVE_WIDGET = 'tawkto-remove-widget';
const CIPHER = 'AES-256-CBC';
const CIPHER_IV_LENGTH = 16;

/**
* @var $plugin_ver Plugin version
Expand Down Expand Up @@ -249,6 +251,7 @@ public function validate_options( $input ) {
$visibility_text_fields = array(
'excluded_url_list',
'included_url_list',
'js_api_key',
);

self::validate_visibility_toggle_fields( $input, $visibility_toggle_fields );
Expand Down Expand Up @@ -329,6 +332,11 @@ private static function validate_text_fields( &$fields, $field_names ) {
foreach ( $field_names as $field_name ) {
if ( isset( $fields[ $field_name ] ) ) {
$fields[ $field_name ] = sanitize_text_field( $fields[ $field_name ] );

if ( 'js_api_key' === $field_name && ! empty( $fields['js_api_key'] ) ) {
$fields['js_api_key'] = self::get_encrypted_data( $fields['js_api_key'] );
}

eug-L marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

Expand Down Expand Up @@ -365,6 +373,75 @@ public static function get_default_visibility_options() {
return $config['visibility'];
}

/**
* Encrypt data
*
* @param string $data - Data to be encrypted.
* @return string
*/
private static function get_encrypted_data( $data ) {
try {
$iv = random_bytes( self::CIPHER_IV_LENGTH );
} catch ( Exception $e ) {
return '';
}

$encrypted_data = openssl_encrypt( $data, self::CIPHER, SECURE_AUTH_KEY, 0, $iv );

if ( false === $encrypted_data ) {
return '';
}

// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$encrypted_data = base64_encode( $iv . $encrypted_data );

if ( false === $encrypted_data ) {
return '';
}

return $encrypted_data;
}

/**
* Decrypt data
*
* @param string $data - Data to be decrypted.
* @return string
*/
private static function get_decrypted_data( $data ) {
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$decoded_data = base64_decode( $data );

if ( false === $decoded_data ) {
return '';
}

$iv = substr( $decoded_data, 0, self::CIPHER_IV_LENGTH );
$encrypted_data = substr( $decoded_data, self::CIPHER_IV_LENGTH );

$decrypted_data = openssl_decrypt( $encrypted_data, self::CIPHER, SECURE_AUTH_KEY, 0, $iv );

if ( false === $decrypted_data ) {
return '';
}

return $decrypted_data;
}

/**
* Retrieves JS API Key
*
* @return string
*/
public static function get_js_api_key() {
$visibility = get_option( self::TAWK_VISIBILITY_OPTIONS );

if ( isset( $visibility['js_api_key'] ) ) {
return self::get_decrypted_data( $visibility['js_api_key'] );
}

return '';
}
}
}

Expand Down Expand Up @@ -463,6 +540,12 @@ public function get_current_customer_details() {
'name' => $current_user->display_name,
'email' => $current_user->user_email,
);

$js_api_key = TawkTo_Settings::get_js_api_key();
if ( ! empty( $user_info['email'] ) && ! empty( $js_api_key ) ) {
$user_info['hash'] = hash_hmac( 'sha256', $user_info['email'], $js_api_key );
}

return wp_json_encode( $user_info );
}
return null;
Expand Down
26 changes: 26 additions & 0 deletions tawkto/templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,32 @@ class="slider round"
</td>
</tr>
</table>

<div class="tawk-selected-visitor">
<p class='tawk-notice'>
<?php esc_html_e( 'Note: If ', 'tawk-to-live-chat' ); ?>
<b><?php esc_html_e( 'Secure Mode', 'tawk-to-live-chat' ); ?></b>
<?php esc_html_e( ' is enabled on your property, please enter your ', 'tawk-to-live-chat' ); ?>
<b><?php esc_html_e( 'Javascript API Key', 'tawk-to-live-chat' ); ?></b>
<?php esc_html_e( ' for visitor recognition to work correctly.', 'tawk-to-live-chat' ); ?>
</p>

<table class="form-table">
<tr valign="top">
<th class="tawk-setting" scope="row">
<?php esc_html_e( 'Javascript API Key', 'tawk-to-live-chat' ); ?>
</th>
<td>
<input type="password"
id="js-api-key"
name="tawkto-visibility-options[js_api_key]"
value="<?php echo esc_attr( $visibility['js_api_key'] ); ?>"
pattern="^[a-zA-Z0-9]+$"
autocomplete="off" />
</td>
</tr>
</table>
</div>
</div>
</form>
</div>
Expand Down