Skip to content

Commit

Permalink
rfe #1590 Recaptcha API v2
Browse files Browse the repository at this point in the history
Signed-off-by: Madhura Jayaratne <madhura.cj@gmail.com>
  • Loading branch information
madhuracj committed Jan 8, 2015
1 parent 1a4cc97 commit 743a528
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 354 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -12,6 +12,7 @@ phpMyAdmin - ChangeLog
+ rfe #1572 User prefs: Diff-friendly JSON for config
+ rfe #1567 Sever Variables Table UI Improvements
- bug #4675 phpMyAdmin should be able to work without 'examples' DIR - move SQL scripts to sql directory
+ rfe #1590 Recaptcha API v2

4.3.7.0 (not yet released)
- bug #4694 js error on marking table as favorite in Safari (in private mode)
Expand Down
2 changes: 1 addition & 1 deletion libraries/Header.class.php
Expand Up @@ -499,7 +499,7 @@ public function sendHttpHeaders()
if (!empty($GLOBALS['cfg']['CaptchaLoginPrivateKey'])
&& !empty($GLOBALS['cfg']['CaptchaLoginPublicKey'])
) {
$captcha_url = ' https://www.google.com ';
$captcha_url = ' https://www.google.com https://www.gstatic.com ';

This comment has been minimized.

Copy link
@milkomeda

milkomeda Apr 2, 2015

You need to add "https://apis.google.com https://ssl.gstatic.com" to $captcha_url if you want to see the checkbox reCAPTCHA widget.
Else you only get the fallback widget. See https://developers.google.com/recaptcha/docs/faq#content_security_policy for more details.

This comment has been minimized.

Copy link
@madhuracj

madhuracj Apr 2, 2015

Author Contributor
} else {
$captcha_url = '';
}
Expand Down
63 changes: 15 additions & 48 deletions libraries/plugins/auth/AuthenticationCookie.class.php
Expand Up @@ -233,39 +233,10 @@ public function auth()
&& !$skip
) {
// If enabled show captcha to the user on the login screen.
echo '<script type="text/javascript">
var RecaptchaOptions = {
theme : "white"
};
</script>
<script type="text/javascript"
src="https://www.google.com/recaptcha/api/challenge?'
. 'k=' . $GLOBALS['cfg']['CaptchaLoginPublicKey'] . '&amp;'
. 'hl=' . $GLOBALS['lang'] . '">
</script>
<noscript>
<iframe src="https://www.google.com/recaptcha/api/noscript?k='
. $GLOBALS['cfg']['CaptchaLoginPublicKey'] . '"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
<script type="text/javascript">
$(function() {
$(document).on(
"mouseover",
"#recaptcha_reload_btn," +
"#recaptcha_switch_audio_btn," +
"#recaptcha_switch_img_btn," +
"#recaptcha_whatsthis_btn," +
"#recaptcha_audio_play_again"
function() {
$(this).addClass("disableAjax");
});
});
</script>';
echo '<script src="https://www.google.com/recaptcha/api.js?hl='
. $GLOBALS['lang'] . '" async defer></script>';
echo '<div class="g-recaptcha" data-sitekey="'
. $GLOBALS['cfg']['CaptchaLoginPublicKey'] . '"></div>';
}

echo '</fieldset>
Expand Down Expand Up @@ -365,36 +336,32 @@ public function authCheck()
&& !empty($GLOBALS['cfg']['CaptchaLoginPublicKey'])
&& !$skip
) {
if ( !empty($_POST["recaptcha_challenge_field"])
&& !empty($_POST["recaptcha_response_field"])
) {
include_once 'libraries/plugins/auth/recaptchalib.php';
if (! empty($_POST["g-recaptcha-response"])) {

include_once 'libraries/plugins/auth/recaptcha/recaptchalib.php';
$reCaptcha = new ReCaptcha(
$GLOBALS['cfg']['CaptchaLoginPrivateKey']
);

// Use private key to verify captcha status.
$resp = recaptcha_check_answer(
$GLOBALS['cfg']['CaptchaLoginPrivateKey'],
// verify captcha status.
$resp = $reCaptcha->verifyResponse(
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]
$_POST["g-recaptcha-response"]
);

// Check if the captcha entered is valid, if not stop the login.
if ( !$resp->is_valid ) {
if ($resp == null || ! $resp->success) {
$conn_error = __('Entered captcha is wrong, try again!');
$_SESSION['last_valid_captcha'] = false;
return false;
} else {
$_SESSION['last_valid_captcha'] = true;
}
} elseif (! empty($_POST["recaptcha_challenge_field"])
&& empty($_POST["recaptcha_response_field"])
) {
$conn_error = __('Please enter correct captcha!');
return false;
} else {
if (! isset($_SESSION['last_valid_captcha'])
|| ! $_SESSION['last_valid_captcha']
) {
$conn_error = __('Please enter correct captcha!');
return false;
}
}
Expand Down
29 changes: 29 additions & 0 deletions libraries/plugins/auth/recaptcha/LICENSE
@@ -0,0 +1,29 @@
Copyright 2014, Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

140 changes: 140 additions & 0 deletions libraries/plugins/auth/recaptcha/recaptchalib.php
@@ -0,0 +1,140 @@
<?php
/**
* This is a PHP library that handles calling reCAPTCHA.
* - Documentation and latest version
* https://developers.google.com/recaptcha/docs/php
* - Get a reCAPTCHA API Key
* https://www.google.com/recaptcha/admin/create
* - Discussion group
* http://groups.google.com/group/recaptcha
*
* @copyright Copyright (c) 2014, Google Inc.
* @link http://www.google.com/recaptcha
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

/**
* A ReCaptchaResponse is returned from checkAnswer().
*/
class ReCaptchaResponse
{
public $success;
public $errorCodes;
}

class ReCaptcha
{
private static $_signupUrl = "https://www.google.com/recaptcha/admin";
private static $_siteVerifyUrl =
"https://www.google.com/recaptcha/api/siteverify?";
private $_secret;
private static $_version = "php_1.0";

/**
* Constructor.
*
* @param string $secret shared secret between site and ReCAPTCHA server.
*/
function ReCaptcha($secret)
{
if ($secret == null || $secret == "") {
die("To use reCAPTCHA you must get an API key from <a href='"
. self::$_signupUrl . "'>" . self::$_signupUrl . "</a>");
}
$this->_secret=$secret;
}

/**
* Encodes the given data into a query string format.
*
* @param array $data array of string elements to be encoded.
*
* @return string - encoded request.
*/
private function _encodeQS($data)
{
$req = "";
foreach ($data as $key => $value) {
$req .= $key . '=' . urlencode(stripslashes($value)) . '&';
}

// Cut the last '&'
$req=substr($req, 0, strlen($req)-1);
return $req;
}

/**
* Submits an HTTP GET to a reCAPTCHA server.
*
* @param string $path url path to recaptcha server.
* @param array $data array of parameters to be sent.
*
* @return array response
*/
private function _submitHTTPGet($path, $data)
{
$req = $this->_encodeQS($data);
$response = file_get_contents($path . $req);
return $response;
}

/**
* Calls the reCAPTCHA siteverify API to verify whether the user passes
* CAPTCHA test.
*
* @param string $remoteIp IP address of end user.
* @param string $response response string from recaptcha verification.
*
* @return ReCaptchaResponse
*/
public function verifyResponse($remoteIp, $response)
{
// Discard empty solution submissions
if ($response == null || strlen($response) == 0) {
$recaptchaResponse = new ReCaptchaResponse();
$recaptchaResponse->success = false;
$recaptchaResponse->errorCodes = 'missing-input';
return $recaptchaResponse;
}

$getResponse = $this->_submitHttpGet(
self::$_siteVerifyUrl,
array (
'secret' => $this->_secret,
'remoteip' => $remoteIp,
'v' => self::$_version,
'response' => $response
)
);
$answers = json_decode($getResponse, true);
$recaptchaResponse = new ReCaptchaResponse();

if (trim($answers ['success']) == true) {
$recaptchaResponse->success = true;
} else {
$recaptchaResponse->success = false;
$recaptchaResponse->errorCodes = $answers ['error-codes'];
}

return $recaptchaResponse;
}
}

?>

0 comments on commit 743a528

Please sign in to comment.