From 8c9abb9888b37a3550ec80e87b7bbdc122d5ccf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 31 Oct 2017 13:31:09 +0100 Subject: [PATCH] Add configuration for second authentication factor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michal Čihař --- libraries/user_preferences.inc.php | 8 ++++ prefs_second.php | 63 +++++++++++++++++++++++++++ templates/prefs_second.twig | 53 ++++++++++++++++++++++ templates/prefs_second_configure.twig | 13 ++++++ templates/prefs_second_confirm.twig | 12 +++++ 5 files changed, 149 insertions(+) create mode 100644 prefs_second.php create mode 100644 templates/prefs_second.twig create mode 100644 templates/prefs_second_configure.twig create mode 100644 templates/prefs_second_confirm.twig diff --git a/libraries/user_preferences.inc.php b/libraries/user_preferences.inc.php index 693c2538bc..eabe371b94 100644 --- a/libraries/user_preferences.inc.php +++ b/libraries/user_preferences.inc.php @@ -9,6 +9,7 @@ use PhpMyAdmin\Config\Forms\User\UserFormList; use PhpMyAdmin\Message; use PhpMyAdmin\Relation; use PhpMyAdmin\Sanitize; +use PhpMyAdmin\SecondFactor; if (!defined('PHPMYADMIN')) { exit; @@ -30,6 +31,13 @@ $content = PhpMyAdmin\Util::getHtmlTab( 'text' => __('Manage your settings') ) ) . "\n"; +/* Second authentication factor */ +$content .= PhpMyAdmin\Util::getHtmlTab( + array( + 'link' => 'prefs_second.php', + 'text' => __('Two-factor authentication') + ) +) . "\n"; $script_name = basename($GLOBALS['PMA_PHP_SELF']); foreach (UserFormList::getAll() as $formset) { $formset_class = UserFormList::get($formset); diff --git a/prefs_second.php b/prefs_second.php new file mode 100644 index 0000000000..27e626a387 --- /dev/null +++ b/prefs_second.php @@ -0,0 +1,63 @@ +check(true)) { + echo Template::get('prefs_second_confirm')->render([ + 'form' => $second_factor->render(), + ]); + exit; + } else { + $second_factor->configure(''); + Message::rawNotice(__('Two-factor authentication has been removed.'))->display(); + } +} elseif (isset($_POST['2fa_configure'])) { + if (! $second_factor->configure($_POST['2fa_configure'])) { + echo Template::get('prefs_second_configure')->render([ + 'form' => $second_factor->setup(), + 'configure' => $_POST['2fa_configure'], + ]); + exit; + } else { + Message::rawNotice(__('Two-factor authentication has been configured.'))->display(); + } +} + +$all = array_merge([''], $second_factor->available); +$backends = []; +foreach ($all as $name) { + $cls = $second_factor->getBackendClass($name); + $backends[] = [ + 'id' => $cls::$id, + 'name' => $cls::getName(), + 'description' => $cls::getDescription(), + ]; +} + + +$backend = $second_factor->backend; +echo Template::get('prefs_second')->render([ + 'enabled' => $second_factor->writable, + 'num_backends' => count($second_factor->available), + 'backend_id' => $backend::$id, + 'backend_name' => $backend::getName(), + 'backend_description' => $backend::getDescription(), + 'backends' => $backends, +]); diff --git a/templates/prefs_second.twig b/templates/prefs_second.twig new file mode 100644 index 0000000000..ef3b83204e --- /dev/null +++ b/templates/prefs_second.twig @@ -0,0 +1,53 @@ +
+

+{% trans "Two-factor authentication status" %} +{{ Util_showDocu('second_factor') }} +

+
+{% if enabled %} +{% if num_backends == 0 %} +

{% trans "Two-factor authentication is not available, please install optional dependencies to enable authentication backends." %}

+{% else %} +{% if backend_id %} +

{% trans "Two-factor authentication is available and configured for this account." %}

+{% else %} +

{% trans "Two-factor authentication is available, but not configured for this account." %}

+{% endif %} +{% endif %} +{% else %} +

{% trans "Two-factor authentication is not available, enable phpMyAdmin configuration storage to use it." %}

+{% endif %} +
+
+ +{% if backend_id %} +
+

{{ backend_name }}

+
+

{% trans "You have enabled two factor authentication." %}

+

{{ backend_description }}

+
+{{ Url_getHiddenInputs() }} + +
+
+
+{% else %} +
+

{% trans "Configure two-factor authentication" %}

+
+
+{{ Url_getHiddenInputs() }} +
+
+
+{% endif %} diff --git a/templates/prefs_second_configure.twig b/templates/prefs_second_configure.twig new file mode 100644 index 0000000000..cffc6c9cc2 --- /dev/null +++ b/templates/prefs_second_configure.twig @@ -0,0 +1,13 @@ +
+

{% trans "Configure two-factor authentication" %}

+
+
+{{ Url_getHiddenInputs() }} + +{{ form|raw }} + +
+
+
+ + diff --git a/templates/prefs_second_confirm.twig b/templates/prefs_second_confirm.twig new file mode 100644 index 0000000000..091983e205 --- /dev/null +++ b/templates/prefs_second_confirm.twig @@ -0,0 +1,12 @@ +
+

{% trans "Confirm disabling two-factor authentication" %}

+
+
+{{ Message_notice("By disabling two factor authentication you will be again able to login using password only."|trans) }} +{{ Url_getHiddenInputs() }} +{{ form|raw }} + + +
+
+