From d6d84a76077d146354e2485a296396b86ca25846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Wed, 1 Nov 2017 14:39:54 +0100 Subject: [PATCH] Share code for error report in second factor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ...and test it. Signed-off-by: Michal Čihař --- .../Plugins/SecondFactor/Application.php | 13 ----------- .../classes/Plugins/SecondFactor/Key.php | 13 ----------- .../classes/Plugins/SecondFactorPlugin.php | 22 +++++++++++++++++++ libraries/classes/SecondFactor.php | 4 ++-- test/classes/SecondFactorTest.php | 12 ++++++++++ 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/libraries/classes/Plugins/SecondFactor/Application.php b/libraries/classes/Plugins/SecondFactor/Application.php index 6853eadd28..5181e1e8b7 100644 --- a/libraries/classes/Plugins/SecondFactor/Application.php +++ b/libraries/classes/Plugins/SecondFactor/Application.php @@ -7,7 +7,6 @@ */ namespace PhpMyAdmin\Plugins\SecondFactor; -use PhpMyAdmin\Message; use PhpMyAdmin\SecondFactor; use PhpMyAdmin\Template; use PhpMyAdmin\Plugins\SecondFactorPlugin; @@ -25,8 +24,6 @@ class Application extends SecondFactorPlugin */ public static $id = 'application'; - protected $_provided = false; - protected $_google2fa; /** @@ -83,11 +80,6 @@ class Application extends SecondFactorPlugin */ public function render() { - if ($this->_provided) { - Message::rawError( - __('Two-factor authentication failed.') - )->display(); - } return Template::get('login/second/application')->render(); } @@ -98,11 +90,6 @@ class Application extends SecondFactorPlugin */ public function setup() { - if ($this->_provided) { - Message::rawError( - __('Two-factor authentication failed.') - )->display(); - } $inlineUrl = $this->_google2fa->getQRCodeInline( 'phpMyAdmin', $this->_second->user, diff --git a/libraries/classes/Plugins/SecondFactor/Key.php b/libraries/classes/Plugins/SecondFactor/Key.php index f04f607c49..673e6df43b 100644 --- a/libraries/classes/Plugins/SecondFactor/Key.php +++ b/libraries/classes/Plugins/SecondFactor/Key.php @@ -8,7 +8,6 @@ namespace PhpMyAdmin\Plugins\SecondFactor; use PhpMyAdmin\Core; -use PhpMyAdmin\Message; use PhpMyAdmin\Response; use PhpMyAdmin\SecondFactor; use PhpMyAdmin\Template; @@ -27,8 +26,6 @@ class Key extends SecondFactorPlugin */ public static $id = 'key'; - protected $_provided = false; - /** * Creates object * @@ -131,11 +128,6 @@ class Key extends SecondFactorPlugin */ public function render() { - if ($this->_provided) { - Message::rawError( - __('Two-factor authentication failed.') - )->display(); - } $request = U2FServer::makeAuthentication( $this->getRegistrations(), $this->getAppId() @@ -154,11 +146,6 @@ class Key extends SecondFactorPlugin */ public function setup() { - if ($this->_provided) { - Message::rawError( - __('Two-factor authentication failed.') - )->display(); - } $registrationData = U2FServer::makeRegistration( $this->getAppId(), $this->getRegistrations() diff --git a/libraries/classes/Plugins/SecondFactorPlugin.php b/libraries/classes/Plugins/SecondFactorPlugin.php index fa22c580f5..3fb074ee8d 100644 --- a/libraries/classes/Plugins/SecondFactorPlugin.php +++ b/libraries/classes/Plugins/SecondFactorPlugin.php @@ -7,6 +7,7 @@ */ namespace PhpMyAdmin\Plugins; +use PhpMyAdmin\Message; use PhpMyAdmin\SecondFactor; /** @@ -28,6 +29,11 @@ class SecondFactorPlugin */ protected $_second; + /** + * @var boolean + */ + protected $_provided; + /** * Creates object * @@ -36,6 +42,22 @@ class SecondFactorPlugin public function __construct(SecondFactor $second) { $this->_second = $second; + $this->_provided = false; + } + + /** + * Returns authentication error message + * + * @return string + */ + public function getError() + { + if ($this->_provided) { + return Message::rawError( + __('Two-factor authentication failed.') + )->getDisplay(); + } + return ''; } /** diff --git a/libraries/classes/SecondFactor.php b/libraries/classes/SecondFactor.php index cb6d79b0de..d87188ce0f 100644 --- a/libraries/classes/SecondFactor.php +++ b/libraries/classes/SecondFactor.php @@ -166,7 +166,7 @@ class SecondFactor */ public function render() { - return $this->_backend->render(); + return $this->_backend->getError() . $this->_backend->render(); } /** @@ -176,7 +176,7 @@ class SecondFactor */ public function setup() { - return $this->_backend->setup(); + return $this->_backend->getError() . $this->_backend->setup(); } /** diff --git a/test/classes/SecondFactorTest.php b/test/classes/SecondFactorTest.php index 93bfcb1eac..9edf4aa454 100644 --- a/test/classes/SecondFactorTest.php +++ b/test/classes/SecondFactorTest.php @@ -71,6 +71,10 @@ class SecondFactorTest extends PmaTestCase $_POST['2fa_confirm'] = 1; $this->assertTrue($object->check()); unset($_POST['2fa_confirm']); + + /* Test rendering */ + $this->assertNotEquals('', $object->render()); + $this->assertEquals('', $object->setup()); } public function testLoad() @@ -117,6 +121,10 @@ class SecondFactorTest extends PmaTestCase ); $this->assertTrue($object->configure('application')); unset($_POST['2fa_code']); + + /* Test rendering */ + $this->assertNotEquals('', $object->render()); + $this->assertNotEquals('', $object->setup()); } public function testKey() @@ -132,6 +140,10 @@ class SecondFactorTest extends PmaTestCase /* Invalid code */ $_POST['u2f_registration_response'] = 'invalid'; $this->assertFalse($object->configure('key')); + + /* Test rendering */ + $this->assertNotEquals('', $object->render()); + $this->assertNotEquals('', $object->setup()); } /**