Share code for error report in second factor

...and test it.

Signed-off-by: Michal Čihař <michal@cihar.com>
This commit is contained in:
Michal Čihař 2017-11-01 14:39:54 +01:00
parent 0d14ba065c
commit d6d84a7607
5 changed files with 36 additions and 28 deletions

View File

@ -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,

View File

@ -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()

View File

@ -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 '';
}
/**

View File

@ -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();
}
/**

View File

@ -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());
}
/**