Merge #16299 - Fixes #16269 - Support ReCaptcha v2 checkbox

Signed-off-by: William Desportes <williamdes@wdes.fr>
This commit is contained in:
William Desportes 2020-08-07 18:05:51 +02:00
commit 0a3024460d
No known key found for this signature in database
GPG Key ID: 90A0EF1B8251A889
4 changed files with 113 additions and 3 deletions

View File

@ -1872,6 +1872,18 @@ Cookie authentication options
bypass regular expression by the suffix, while connecting to another
server.
.. config:option:: $cfg['CaptchaMethod']
:type: string
:default: ``'invisible'``
Valid values are:
* ``'invisible'`` Use an invisible captcha checking method;
* ``'checkbox'`` Use a checkbox to confirm the user is not a robot.
.. versionadded:: 5.0.3
.. config:option:: $cfg['CaptchaLoginPublicKey']
:type: string

View File

@ -250,9 +250,15 @@ class AuthenticationCookie extends AuthenticationPlugin
} else {
echo '<script src="https://www.google.com/recaptcha/api.js?hl='
, $GLOBALS['lang'] , '" async defer></script>';
echo '<input class="btn btn-primary g-recaptcha" data-sitekey="'
, htmlspecialchars($GLOBALS['cfg']['CaptchaLoginPublicKey']),'"'
. ' data-callback="Functions_recaptchaCallback" value="' , __('Go') , '" type="submit" id="input_go">';
if (isset($GLOBALS['cfg']['CaptchaMethod']) && $GLOBALS['cfg']['CaptchaMethod'] === 'checkbox') {
echo '<div class="g-recaptcha" data-sitekey="'
. htmlspecialchars($GLOBALS['cfg']['CaptchaLoginPublicKey']) . '"></div>';
echo '<input class="btn btn-primary" value="' , __('Go') , '" type="submit" id="input_go">';
} else {
echo '<input class="btn btn-primary g-recaptcha" data-sitekey="'
, htmlspecialchars($GLOBALS['cfg']['CaptchaLoginPublicKey']),'"'
. ' data-callback="Functions_recaptchaCallback" value="' , __('Go') , '" type="submit" id="input_go">';
}
}
$_form_params = [];
if (! empty($GLOBALS['target'])) {

View File

@ -835,6 +835,13 @@ $cfg['AllowArbitraryServer'] = false;
*/
$cfg['ArbitraryServerRegexp'] = '';
/**
* To enable reCaptcha v2 checkbox mode if necessary
*
* @global string $cfg['CaptchaMethod']
*/
$cfg['CaptchaMethod'] = 'invisible';
/**
* if reCaptcha is enabled it needs public key to connect with the service
*

View File

@ -351,6 +351,91 @@ class AuthenticationCookieTest extends PmaTestCase
);
}
/**
* Test for PhpMyAdmin\Plugins\Auth\AuthenticationConfig::showLoginForm
*
* @return void
* @group medium
*/
public function testAuthCaptchaCheckbox()
{
$mockResponse = $this->mockResponse();
$mockResponse->expects($this->once())
->method('isAjax')
->with()
->will($this->returnValue(false));
$mockResponse->expects($this->once())
->method('getFooter')
->with()
->will($this->returnValue(new Footer()));
$mockResponse->expects($this->once())
->method('getHeader')
->with()
->will($this->returnValue(new Header()));
$_REQUEST['old_usr'] = '';
$GLOBALS['cfg']['LoginCookieRecall'] = false;
$GLOBALS['pmaThemeImage'] = 'test';
$GLOBALS['cfg']['Lang'] = '';
$GLOBALS['cfg']['AllowArbitraryServer'] = false;
$GLOBALS['cfg']['Servers'] = [1];
$GLOBALS['cfg']['CaptchaLoginPrivateKey'] = 'testprivkey';
$GLOBALS['cfg']['CaptchaLoginPublicKey'] = 'testpubkey';
$GLOBALS['cfg']['CaptchaMethod'] = 'checkbox';
$GLOBALS['server'] = 0;
$GLOBALS['error_handler'] = new ErrorHandler();
ob_start();
$this->object->showLoginForm();
$result = ob_get_clean();
// assertions
$this->assertStringContainsString('id="imLogo"', $result);
// Check for language selection if locales are there
$loc = LOCALE_PATH . '/cs/LC_MESSAGES/phpmyadmin.mo';
if (is_readable($loc)) {
$this->assertStringContainsString(
'<select name="lang" class="autosubmit" lang="en" dir="ltr" ' .
'id="sel-lang">',
$result
);
}
$this->assertStringContainsString(
'<form method="post" id="login_form" action="index.php" name="login_form" ' .
'autocomplete="off" class="disableAjax hide login js-show">',
$result
);
$this->assertStringContainsString(
'<input type="hidden" name="server" value="0">',
$result
);
$this->assertStringContainsString(
'<script src="https://www.google.com/recaptcha/api.js?hl=en"'
. ' async defer></script>',
$result
);
$this->assertStringContainsString(
'<div class="g-recaptcha" data-sitekey="testpubkey"></div>',
$result
);
$this->assertStringContainsString(
'<input class="btn btn-primary" value="Go" type="submit" id="input_go">',
$result
);
}
/**
* Test for PhpMyAdmin\Plugins\Auth\AuthenticationConfig::showLoginForm with headers
*