Skip to content

Commit

Permalink
Allow changing the name of a key
Browse files Browse the repository at this point in the history
  • Loading branch information
bartnv committed Jan 16, 2023
1 parent ba2b682 commit 9deff94
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 2 additions & 0 deletions localization/en_US.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ $labels['authentication_cancelled'] = 'Authentication cancelled';
$labels['test_key'] = 'Test key';
$labels['key_checked'] = 'Successfully checked key';
$labels['request_key_name'] = 'Please enter a name for this key';
$labels['edit_key_name'] = 'Please enter a new name for key';
$labels['key_renamed'] = 'Key renamed to';

// Messages used for the different portions of the plugin
$messages = array();
Expand Down
2 changes: 2 additions & 0 deletions localization/nl_NL.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ $labels['authentication_cancelled'] = 'Authenticatie geannuleerd';
$labels['test_key'] = 'Sleutel testen';
$labels['key_checked'] = 'Sleutel gecontroleerd met ID';
$labels['request_key_name'] = 'Voer een naam in voor deze sleutel';
$labels['edit_key_name'] = 'Voer een nieuwe naam in voor sleutel';
$labels['key_renamed'] = 'Sleutel hernoemd naar';

// Messages used for the different portions of the plugin
$messages = array();
Expand Down
4 changes: 3 additions & 1 deletion settings.css
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#twofactor_webauthn_keylist SPAN { font-size: 16px; color: red; cursor: pointer; margin-left: 5px; }
#twofactor_webauthn_keylist SPAN { font-size: 16px; cursor: pointer; margin-left: 5px; }
#twofactor_webauthn_keylist SPAN.rename { color: orange; }
#twofactor_webauthn_keylist SPAN.delete { color: red; }
11 changes: 9 additions & 2 deletions twofactor_webauthn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
if (window.rcmail) {
rcmail.addEventListener('init', function(evt) {
rcmail.register_command('plugin.twofactor_webauthn_prepare', twofactor_webauthn_prepare, true);
rcmail.register_command('plugin.twofactor_webauthn_rename', twofactor_webauthn_rename, true);
rcmail.register_command('plugin.twofactor_webauthn_delete', twofactor_webauthn_delete, true);
rcmail.register_command('plugin.twofactor_webauthn_save', twofactor_webauthn_save, true);
rcmail.register_command('plugin.twofactor_webauthn_test', twofactor_webauthn_test);
Expand All @@ -14,6 +15,9 @@ if (window.rcmail) {
function twofactor_webauthn_prepare() {
rcmail.http_post('plugin.twofactor_webauthn_prepare');
}
function twofactor_webauthn_rename(id, name) {
rcmail.http_post('plugin.twofactor_webauthn_rename', { id: id, name: name });
}
function twofactor_webauthn_delete(id) {
rcmail.http_post('plugin.twofactor_webauthn_delete', { id: id });
}
Expand Down Expand Up @@ -55,8 +59,11 @@ function twofactor_webauthn_list(data) {
rcmail.enable_command('plugin.twofactor_webauthn_test', true);
for (key of data) {
ul.append('<li title="' + key.id + '">' + (key.name??key.id) +
' <span onclick="if (confirm(\'' + rcmail.gettext('confirm_delete_key', 'twofactor_webauthn') + ' ' + key.id + (key.name?' ('+key.name+')':'') +
'?\')) { return rcmail.command(\'plugin.twofactor_webauthn_delete\', \'' + key.id + '\'); } else return false;">✖</span>');
' <span class="rename" onclick="var name = prompt(\'' + rcmail.gettext('edit_key_name', 'twofactor_webauthn') + ' ' + (key.name?key.name:key.id) + '\');' +
'if (name) { return rcmail.command(\'plugin.twofactor_webauthn_rename\', \'' + key.id + '\', name); }">✎</span>' +
'<span class="delete" onclick="if (confirm(\'' + rcmail.gettext('confirm_delete_key', 'twofactor_webauthn') + ' ' + key.id + (key.name?' ('+key.name+')':'') +
'?\')) { return rcmail.command(\'plugin.twofactor_webauthn_delete\', \'' + key.id + '\'); } else return false;">✖</span>'
);
}
}

Expand Down
26 changes: 26 additions & 0 deletions twofactor_webauthn.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function init() {
$this->register_action('plugin.twofactor_webauthn_save', array($this, 'twofactor_webauthn_save'));
$this->register_action('plugin.twofactor_webauthn_prepare', array($this, 'twofactor_webauthn_prepare'));
$this->register_action('plugin.twofactor_webauthn_register', array($this, 'twofactor_webauthn_register'));
$this->register_action('plugin.twofactor_webauthn_rename', array($this, 'twofactor_webauthn_rename'));
$this->register_action('plugin.twofactor_webauthn_delete', array($this, 'twofactor_webauthn_delete'));
$this->register_action('plugin.twofactor_webauthn_test', array($this, 'twofactor_webauthn_test'));
$this->register_action('plugin.twofactor_webauthn_check', array($this, 'twofactor_webauthn_check'));
Expand Down Expand Up @@ -158,6 +159,31 @@ function twofactor_webauthn_register() {
$rcmail->output->command('plugin.twofactor_webauthn_list', $this->getList($config));
}

function twofactor_webauthn_rename() {
$id = rcube_utils::get_input_value('id', rcube_utils::INPUT_POST);
if (empty($id)) {
error_log('Received empty id on webauthn rename');
return;
}
$name = rcube_utils::get_input_value('name', rcube_utils::INPUT_POST);
if (empty($name)) {
error_log('Received empty name on webauthn rename');
return;
}
$rcmail = rcmail::get_instance();
$config = $this->getConfig();
$keys = json_decode($config['keys']);
foreach ($keys as &$key) {
if (dechex(crc32(implode('', $key->id))) === $id) {
$key->name = $name;
}
}
$config['keys'] = json_encode($keys);
$this->saveConfig($config);
$rcmail->output->show_message($this->gettext('key_renamed') . ' ' . $name, 'confirmation');
$rcmail->output->command('plugin.twofactor_webauthn_list', $this->getList($config));
}

function twofactor_webauthn_delete() {
$id = rcube_utils::get_input_value('id', rcube_utils::INPUT_POST);
if (empty($id)) {
Expand Down

0 comments on commit 9deff94

Please sign in to comment.