Browse Source

updated captcha abilities & tests

v3-dev
REJack 6 years ago
parent
commit
89fd655074
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 29
      app/Libraries/Aauth.php
  2. 23
      app/Libraries/Aauth/CAPTCHA.php
  3. 8
      tests/Aauth/Libraries/Aauth/CAPTCHATest.php

29
app/Libraries/Aauth.php

@ -218,15 +218,26 @@ class Aauth
return false; return false;
} }
// if ($this->config->ddos_protection && $this->config->recaptcha_active && $loginAttempts->get() > $this->config->recaptcha_login_attempts){ if ($this->config->loginProtection && $this->config->captchaEnabled && $this->isCaptchaRequired())
// $this->CI->load->helper('recaptchalib'); {
// $reCaptcha = new ReCaptcha( $this->config->recaptcha_secret); $request = \Config\Services::request();
// $resp = $reCaptcha->verifyResponse( $this->CI->input->server("REMOTE_ADDR"), $this->CI->input->post("g-recaptcha-response") );
// if( ! $resp->success){ if ($this->config->captchaType === 'recaptcha')
// $this->error(lang('Aauth.aauth_error_recaptcha_not_correct')); {
// return false; $response = $request->getPostGet('g-recaptcha-response');
// } }
// } else if ($this->config->captchaType === 'recaptcha')
{
$response = $request->getPostGet('h-captcha-response');
}
if (! $this->verifyCaptchaResponse($response))
{
$this->error('Aauth.invalidCaptcha');
return false;
}
}
if ($this->config->loginUseUsername) if ($this->config->loginUseUsername)
{ {

23
app/Libraries/Aauth/CAPTCHA.php

@ -29,7 +29,7 @@ use \App\Models\Aauth\LoginAttemptModel;
class CAPTCHA extends \App\Libraries\Aauth class CAPTCHA extends \App\Libraries\Aauth
{ {
/** /**
* Verify Response * Verify CAPTCHA Response
* *
* Calls the CAPTCHA site verify API to verify whether the user passes * Calls the CAPTCHA site verify API to verify whether the user passes
* CAPTCHA test. * CAPTCHA test.
@ -38,7 +38,7 @@ class CAPTCHA extends \App\Libraries\Aauth
* *
* @return array * @return array
*/ */
public function verifyResponse($response) public function verifyCaptchaResponse($response)
{ {
if ($response === null || strlen($response) === 0) if ($response === null || strlen($response) === 0)
{ {
@ -97,11 +97,7 @@ class CAPTCHA extends \App\Libraries\Aauth
{ {
$content = ''; $content = '';
if ($this->config->loginProtection && $this->config->captchaEnabled) if ($this->config->loginProtection && $this->config->captchaEnabled && $this->isCaptchaRequired())
{
$loginAttemptModel = new LoginAttemptModel();
if ($loginAttemptModel->find() >= $this->config->captchaLoginAttempts)
{ {
$siteKey = $this->config->captchaSiteKey; $siteKey = $this->config->captchaSiteKey;
@ -116,11 +112,22 @@ class CAPTCHA extends \App\Libraries\Aauth
$content .= '<script src="https://hcaptcha.com/1/api.js" async defer></script>'; $content .= '<script src="https://hcaptcha.com/1/api.js" async defer></script>';
} }
} }
}
return $content; return $content;
} }
/**
* Is CAPTCHA Required
*
* @return boolean
*/
public function isCaptchaRequired()
{
$loginAttemptModel = new LoginAttemptModel();
return $loginAttemptModel->find() >= $this->config->captchaLoginAttempts;
}
/** /**
* Submit GET * Submit GET
* *

8
tests/Aauth/Libraries/Aauth/CAPTCHATest.php

@ -93,17 +93,17 @@ class CAPTCHATest extends CIDatabaseTestCase
$this->assertContains('https://hcaptcha.com/1', $this->library->generateCaptchaHtml()); $this->assertContains('https://hcaptcha.com/1', $this->library->generateCaptchaHtml());
} }
public function testVerifyResponse() public function testVerifyCaptchaResponse()
{ {
$config = new AauthConfig(); $config = new AauthConfig();
$config->captchaEnabled = true; $config->captchaEnabled = true;
$this->library = new Aauth($config, true); $this->library = new Aauth($config, true);
$this->assertContains('missing-input', $this->library->verifyResponse(null)['errorCodes']); $this->assertContains('missing-input', $this->library->verifyCaptchaResponse(null)['errorCodes']);
$this->assertContains('invalid-input-response', $this->library->verifyResponse('0123456789')['errorCodes']); $this->assertContains('invalid-input-response', $this->library->verifyCaptchaResponse('0123456789')['errorCodes']);
$config->captchaType = 'hcaptcha'; $config->captchaType = 'hcaptcha';
$this->library = new Aauth($config, true); $this->library = new Aauth($config, true);
$this->assertContains('invalid-input-response', $this->library->verifyResponse('0123456789')['errorCodes']); $this->assertContains('invalid-input-response', $this->library->verifyCaptchaResponse('0123456789')['errorCodes']);
} }
} }

Loading…
Cancel
Save