Skip to content
Permalink
Browse files

Add Google reCAPTCHA

Simple integration of Google reCAPTCHA (visible).
Implemented without any state => Captcha has to be solved for every request.
php-curl is required.

Reported with issue #30.
  • Loading branch information...
uhei committed Jun 28, 2018
1 parent 5bc4183 commit f850ed06ef8fb2a8537dbd1ae4a621b925a875c1
Showing with 64 additions and 0 deletions.
  1. +5 −0 config.php.example
  2. +17 −0 docs/configuration.md
  3. +22 −0 execute.php
  4. +7 −0 includes/config.defaults.php
  5. +7 −0 index.php
  6. +6 −0 js/looking-glass.js
@@ -27,6 +27,11 @@ $config['filters'][] = '/^NotToShow/';
// If telnet is used in combination with extreme_netiron, uncomment the following filter
//$config['filters'][] = '/([^\x20-\x7E]|User|Please|Disable|telnet|^\s*$)/';

// Google reCaptcha integration
$config['recaptcha']['enabled'] = false;
$config['recaptcha']['apikey'] = 'foobar';
$config['recaptcha']['secret'] = 'foobar';

// Routers definitions

// Authentication based on SSH or Telnet with password
@@ -250,6 +250,23 @@ $config['filters'][] = '/^NotToShow/';
Defines filters to eliminate some lines from the output. Do not define any
filters if there is no nothing to filter.

### Google reCAPTCHA

```php
$config['recaptcha']['enabled'] = true;
```
If set to true, allows the use of Google reCAPTCHA. Sitekey and secret must be
configured to use reCAPTCHA.

php-curl is required.

```php
$config['recaptcha']['apikey'] = "foobar";
$config['recaptcha']['secret'] = "foobar";
```
Defines Google reCAPTCHA sitekey and secret.


### Misc.

```php
@@ -57,6 +57,28 @@
return;
}
if ($config['recaptcha']['enabled'] && isset($config['recaptcha']['apikey']) && isset($config['recaptcha']['secret'])) {
$remoteip = $_SERVER['REMOTE_ADDR'];
$response = $_POST['g-recaptcha-response'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $config['recaptcha']['url']);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'secret' => $config['recaptcha']['secret'],
'response' => $response,
'remoteip' => $remoteip
));
$curlData = curl_exec($curl);
curl_close($curl);
$recaptcha = json_decode($curlData, true);
if ($recaptcha["success"] == false) {
$error = 'Are you a robot?';
print(json_encode(array('error' => $error)));
return;
}
}
// Do the processing
$router = Router::instance($hostname, $requester);
$router_config = $router->get_config();
@@ -94,6 +94,13 @@ function set_defaults_for_routers(&$parsed_config) {
// Filters
'filters' => array(),
// Google reCaptcha
'recaptcha' => array(
// Disabled by default
'enabled' => false,
'url' => 'https://www.google.com/recaptcha/api/siteverify'
),
// Logs
'logs' => array(
// Logs file where commands will be written
@@ -38,6 +38,7 @@ function __construct($config) {
$this->misc = $config['misc'];
$this->routers = $config['routers'];
$this->doc = $config['doc'];
$this->recaptcha = $config['recaptcha'];
}
private function router_count() {
@@ -121,6 +122,9 @@ private function render_buttons() {
print('<button class="col-md-6 btn btn-primary" id="send" type="submit">Enter</button>');
print('<button class="col-md-6 btn btn-danger" id="clear" type="reset">Reset</button>');
print('</div>');
if ($this->recaptcha['enabled'] && isset($this->recaptcha['apikey']) && isset($this->recaptcha['secret'])) {
print('<div class="g-recaptcha" data-sitekey="'.$this->recaptcha['apikey'].'"></div>');
}
print('</div>');
}
@@ -298,6 +302,9 @@ public function render() {
print('<script src="libs/bootstrap-4.1.1/js/bootstrap.min.js"></script>');
print('<script src="libs/fontawesome-5.0.13/js/fontawesome-all.min.js"></script>');
print('<script src="js/looking-glass.js"></script>');
if ($this->recaptcha['enabled'] && isset($this->recaptcha['apikey']) && isset($this->recaptcha['secret'])) {
print('<script src="https://www.google.com/recaptcha/api.js" async defer></script>');
}
print('</html>');
}
}
@@ -37,12 +37,18 @@ $(document).ready(function() {
// reset the form and update the doc modal
$(this).closest('form').get(0).reset();
request_doc($('#query').val());
if (typeof grecaptcha.reset === "function") {
grecaptcha.reset();
}
});

// reset the view to the default one
$('#backhome').click(function() {
$('.content').slideDown();
$('.result').slideUp();
if (typeof grecaptcha.reset === "function") {
grecaptcha.reset();
}
});

// initialize the help modal

0 comments on commit f850ed0

Please sign in to comment.
You can’t perform that action at this time.