Share code for getting server URL and use it in 2FA as well

This way multiple phpMyAdmin installations can be identified.

Signed-off-by: Michal Čihař <michal@cihar.com>
This commit is contained in:
Michal Čihař 2017-11-01 16:27:23 +01:00
parent c5b0da38cd
commit 947c1ace03
4 changed files with 57 additions and 26 deletions

View File

@ -91,7 +91,7 @@ class Application extends SecondFactorPlugin
public function setup()
{
$inlineUrl = $this->_google2fa->getQRCodeInline(
'phpMyAdmin',
'phpMyAdmin (' . $this->getAppId(false) . ')',
$this->_second->user,
$this->_second->config['settings']['secret']
);

View File

@ -7,7 +7,6 @@
*/
namespace PhpMyAdmin\Plugins\SecondFactor;
use PhpMyAdmin\Core;
use PhpMyAdmin\Response;
use PhpMyAdmin\SecondFactor;
use PhpMyAdmin\Template;
@ -60,27 +59,6 @@ class Key extends SecondFactorPlugin
return $result;
}
/**
* Return FIDO U2F Application ID
*
* It has to be URL with hostname only, having https protocol
*
* @return string
*/
public function getAppId()
{
global $PMA_Config;
$url = $PMA_Config->get('PmaAbsoluteUri');
if (!empty($url)) {
$parsed = parse_url($url);
if (isset($parsed['scheme']) && isset($parsed['host'])) {
return $parsed['scheme'] . '://' . $parsed['host'] . (!empty($parsed['port']) ? ':' . $parsed['port'] : '');
}
}
return ($PMA_Config->isHttps() ? 'https://' : 'http://') . Core::getenv('HTTP_HOST');
}
/**
* Checks authentication, returns true on success
*
@ -134,7 +112,7 @@ class Key extends SecondFactorPlugin
{
$request = U2FServer::makeAuthentication(
$this->getRegistrations(),
$this->getAppId()
$this->getAppId(true)
);
$_SESSION['authenticationRequest'] = $request;
$this->loadScripts();
@ -151,7 +129,7 @@ class Key extends SecondFactorPlugin
public function setup()
{
$registrationData = U2FServer::makeRegistration(
$this->getAppId(),
$this->getAppId(true),
$this->getRegistrations()
);
$_SESSION['registrationRequest'] = $registrationData['request'];

View File

@ -7,6 +7,7 @@
*/
namespace PhpMyAdmin\Plugins;
use PhpMyAdmin\Core;
use PhpMyAdmin\Message;
use PhpMyAdmin\SecondFactor;
@ -130,4 +131,35 @@ class SecondFactorPlugin
{
return __('Login using password only.');
}
/**
* Return an applicaiton ID
*
* Either hostname or hostname with scheme.
*
* @param boolean $return_url Whether to generate URL
*
* @return string
*/
public function getAppId($return_url)
{
global $PMA_Config;
$url = $PMA_Config->get('PmaAbsoluteUri');
$parsed = [];
if (!empty($url)) {
$parsed = parse_url($url);
}
if (empty($parsed['scheme'])) {
$parsed['scheme'] = $PMA_Config->isHttps() ? 'https' : 'http';
}
if (empty($parsed['host'])) {
$parsed['host'] = Core::getenv('HTTP_HOST');
}
if ($return_url) {
return $parsed['scheme'] . '://' . $parsed['host'] . (!empty($parsed['port']) ? ':' . $parsed['port'] : '');
} else {
return $parsed['host'];
}
}
}

View File

@ -181,13 +181,34 @@ class SecondFactorTest extends PmaTestCase
$this->assertNotEquals('', $object->setup());
}
/**
* Test getting AppId
*/
public function testKeyAppId()
{
$object = new SecondFactor('user');
$GLOBALS['PMA_Config']->set('PmaAbsoluteUri', 'http://demo.example.com');
$this->assertEquals('http://demo.example.com', $object->backend->getAppId(true));
$this->assertEquals('demo.example.com', $object->backend->getAppId(false));
$GLOBALS['PMA_Config']->set('PmaAbsoluteUri', 'https://demo.example.com:123');
$this->assertEquals('https://demo.example.com:123', $object->backend->getAppId(true));
$this->assertEquals('demo.example.com', $object->backend->getAppId(false));
$GLOBALS['PMA_Config']->set('PmaAbsoluteUri', '');
$GLOBALS['PMA_Config']->set('is_https', true);
$_SERVER['HTTP_HOST'] = 'pma.example.com';
$this->assertEquals('https://pma.example.com', $object->backend->getAppId(true));
$this->assertEquals('pma.example.com', $object->backend->getAppId(false));
$GLOBALS['PMA_Config']->set('is_https', false);
$this->assertEquals('http://pma.example.com', $object->backend->getAppId(true));
$this->assertEquals('pma.example.com', $object->backend->getAppId(false));
}
/**
* Test based on upstream test data:
* https://github.com/Yubico/php-u2flib-server
*/
public function testKeyAuthentication()
{
$GLOBALS['PMA_Config']->set('PmaAbsoluteUri', 'http://demo.example.com');
$object = new SecondFactor('user');
if (! in_array('key', $object->available)) {
$this->markTestSkipped('u2f-php-server not available');