Browse Source
- updated Copyright PHPDocs - updated Libraries Aauth, Aauth/CAPTCHA & Aauth/TOTP - updated LoginAttemptModel & UserSessionModel - updated Tests LoginAttemptModelTest, UserSessionModelTest, CAPTCHATest, LoginTest & TOTPTestv3-dev
22 changed files with 342 additions and 106 deletions
@ -83,13 +83,16 @@ class CAPTCHATest extends CIDatabaseTestCase |
|||||||
$this->library->login('[email protected]', 'password123456'); |
$this->library->login('[email protected]', 'password123456'); |
||||||
$this->library->login('[email protected]', 'password123456'); |
$this->library->login('[email protected]', 'password123456'); |
||||||
$this->library->login('[email protected]', 'password123456'); |
$this->library->login('[email protected]', 'password123456'); |
||||||
|
$_POST['g-recaptcha-response'] = '0123456789'; |
||||||
$this->library->login('[email protected]', 'password123456'); |
$this->library->login('[email protected]', 'password123456'); |
||||||
|
|
||||||
$this->assertContains('https://www.google.com/recaptcha', $this->library->generateCaptchaHtml()); |
$this->assertContains('https://www.google.com/recaptcha', $this->library->generateCaptchaHtml()); |
||||||
|
|
||||||
$config->captchaType = 'hcaptcha'; |
$config->captchaType = 'hcaptcha'; |
||||||
$this->library = new Aauth($config, true); |
$this->library = new Aauth($config, true); |
||||||
|
$_POST['h-recaptcha-response'] = '0123456789'; |
||||||
|
$this->library->login('[email protected]', 'password123456'); |
||||||
|
$this->assertEquals(lang('Aauth.invalidCaptcha'), $this->library->getErrorsArray()[0]); |
||||||
$this->assertContains('https://hcaptcha.com/1', $this->library->generateCaptchaHtml()); |
$this->assertContains('https://hcaptcha.com/1', $this->library->generateCaptchaHtml()); |
||||||
} |
} |
||||||
|
|
||||||
@ -105,5 +108,9 @@ class CAPTCHATest extends CIDatabaseTestCase |
|||||||
$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->verifyCaptchaResponse('0123456789')['errorCodes']); |
$this->assertContains('invalid-input-response', $this->library->verifyCaptchaResponse('0123456789')['errorCodes']); |
||||||
|
|
||||||
|
$config->captchaType = 'hcaptcha'; |
||||||
|
$this->library = new Aauth($config, true); |
||||||
|
$this->assertTrue($this->library->verifyCaptchaResponse('testing')['success']); |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -86,8 +86,25 @@ class LoginTest extends CIDatabaseTestCase |
|||||||
$this->seeInDatabase($config->dbTableLoginTokens, [ |
$this->seeInDatabase($config->dbTableLoginTokens, [ |
||||||
'user_id' => 1, |
'user_id' => 1, |
||||||
]); |
]); |
||||||
|
|
||||||
$this->assertTrue($this->response->hasCookie('remember')); |
$this->assertTrue($this->response->hasCookie('remember')); |
||||||
|
|
||||||
|
$this->hasInDatabase($config->dbTableUserSessions, [ |
||||||
|
'id' => md5(time()), |
||||||
|
'ip_address' => '127.0.0.1', |
||||||
|
'timestamp' => time(), |
||||||
|
'data' => '__ci_last_regenerate|i:' . time() . ';user|a:4:{s:2:"id";s:1:"1";s:8:"username";s:5:"admin";s:5:"email";s:17:"[email protected]";s:8:"loggedIn";b:1;}', |
||||||
|
'data' => '__ci_last_regenerate|i:1551553466;user|a:4:{s:2:"id";s:1:"1";s:8:"username";s:5:"admin";s:5:"email";s:17:"[email protected]";s:8:"loggedIn";b:1;}', |
||||||
|
]); |
||||||
|
|
||||||
|
$config->loginSingleMode = true; |
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->login('admin', 'password123456')); |
||||||
|
|
||||||
|
$config->loginSingleMode = false; |
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
|
||||||
$this->assertFalse($this->library->login('admin', 'passwor')); |
$this->assertFalse($this->library->login('admin', 'passwor')); |
||||||
$this->assertEquals(lang('Aauth.loginFailedUsername'), $this->library->getErrorsArray()[0]); |
$this->assertEquals(lang('Aauth.loginFailedUsername'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
@ -95,6 +112,7 @@ class LoginTest extends CIDatabaseTestCase |
|||||||
$this->assertFalse($this->library->login('admin', 'password1234')); |
$this->assertFalse($this->library->login('admin', 'password1234')); |
||||||
$this->assertEquals(lang('Aauth.loginFailedAll'), $this->library->getErrorsArray()[0]); |
$this->assertEquals(lang('Aauth.loginFailedAll'), $this->library->getErrorsArray()[0]); |
||||||
|
|
||||||
|
$config->loginSingleMode = false; |
||||||
$config->loginAccurateErrors = true; |
$config->loginAccurateErrors = true; |
||||||
$this->library = new Aauth($config, $session); |
$this->library = new Aauth($config, $session); |
||||||
$this->library->clearErrors(); |
$this->library->clearErrors(); |
||||||
@ -104,7 +122,6 @@ class LoginTest extends CIDatabaseTestCase |
|||||||
$this->library->clearErrors(); |
$this->library->clearErrors(); |
||||||
$this->assertFalse($this->library->login('user99', 'password123456')); |
$this->assertFalse($this->library->login('user99', 'password123456')); |
||||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||||
// $config->loginUseUsername = false; |
|
||||||
|
|
||||||
$this->library = new Aauth(null, $session); |
$this->library = new Aauth(null, $session); |
||||||
$this->assertTrue($this->library->login('[email protected]', 'password123456')); |
$this->assertTrue($this->library->login('[email protected]', 'password123456')); |
||||||
|
@ -8,6 +8,7 @@ use Tests\Support\Session\MockSession; |
|||||||
use CodeIgniter\Session\Handlers\FileHandler; |
use CodeIgniter\Session\Handlers\FileHandler; |
||||||
use CodeIgniter\Test\CIDatabaseTestCase; |
use CodeIgniter\Test\CIDatabaseTestCase; |
||||||
use App\Libraries\Aauth; |
use App\Libraries\Aauth; |
||||||
|
use App\Models\Aauth\UserModel; |
||||||
use App\Models\Aauth\UserVariableModel; |
use App\Models\Aauth\UserVariableModel; |
||||||
use OTPHP\TOTP; |
use OTPHP\TOTP; |
||||||
|
|
||||||
@ -60,6 +61,65 @@ class TOTPTest extends CIDatabaseTestCase |
|||||||
|
|
||||||
//-------------------------------------------------------------------- |
//-------------------------------------------------------------------- |
||||||
|
|
||||||
|
/** |
||||||
|
* @runInSeparateProcess |
||||||
|
* @preserveGlobalState disabled |
||||||
|
*/ |
||||||
|
|
||||||
|
public function testLogin() |
||||||
|
{ |
||||||
|
$config = new AauthConfig(); |
||||||
|
$config->totpEnabled = true; |
||||||
|
$session = $this->getInstance(); |
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
|
||||||
|
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||||
|
'user_id' => 1, |
||||||
|
'data_key' => 'totp_secret', |
||||||
|
'data_value' => 'JBSWY3DPEHPK3PXP', |
||||||
|
'system' => true, |
||||||
|
]); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->login('[email protected]', 'password123456')); |
||||||
|
|
||||||
|
$config->totpLogin = true; |
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
|
||||||
|
$this->assertFalse($this->library->login('[email protected]', 'password123456', null, '000001')); |
||||||
|
$this->assertEquals(lang('Aauth.invalidTOTPCode'), $this->library->getErrorsArray()[0]); |
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
$this->assertFalse($this->library->login('[email protected]', 'password123456', null)); |
||||||
|
$this->assertEquals(lang('Aauth.requiredTOTPCode'), $this->library->getErrorsArray()[0]); |
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
|
||||||
|
$totp = TOTP::create('JBSWY3DPEHPK3PXP'); |
||||||
|
$totpCode = $totp->now(); |
||||||
|
usleep(1000); |
||||||
|
$this->assertTrue($this->library->login('[email protected]', 'password123456', null, $totpCode)); |
||||||
|
|
||||||
|
$userModel = new UserModel(); |
||||||
|
$userModel->protect(false)->update(1, ['last_ip_address' => '99.99.99.99']); |
||||||
|
|
||||||
|
$config->totpOnIpChange = true; |
||||||
|
|
||||||
|
$this->assertFalse($this->library->login('[email protected]', 'password123456', null, '000001')); |
||||||
|
$this->assertEquals(lang('Aauth.invalidTOTPCode'), $this->library->getErrorsArray()[0]); |
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
$this->assertFalse($this->library->login('[email protected]', 'password123456', null)); |
||||||
|
$this->assertEquals(lang('Aauth.requiredTOTPCode'), $this->library->getErrorsArray()[0]); |
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
|
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
$this->assertTrue($this->library->login('[email protected]', 'password123456', null, $totpCode)); |
||||||
|
|
||||||
|
$userModel->protect(false)->update(1, ['last_ip_address' => '99.99.99.99']); |
||||||
|
$config->totpOnIpChange = true; |
||||||
|
$config->totpLogin = false; |
||||||
|
$this->library = new Aauth($config, $session); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->login('[email protected]', 'password123456')); |
||||||
|
} |
||||||
|
|
||||||
/** |
/** |
||||||
* @runInSeparateProcess |
* @runInSeparateProcess |
||||||
* @preserveGlobalState disabled |
* @preserveGlobalState disabled |
||||||
@ -124,16 +184,6 @@ class TOTPTest extends CIDatabaseTestCase |
|||||||
$config->totpEnabled = true; |
$config->totpEnabled = true; |
||||||
$this->library = new Aauth($config, $session); |
$this->library = new Aauth($config, $session); |
||||||
|
|
||||||
$this->assertTrue($this->library->verifyUserTotpCode('999000', 1)); |
|
||||||
|
|
||||||
$this->library = new Aauth($config, $session); |
|
||||||
$session->set('user', [ |
|
||||||
'id' => 1, |
|
||||||
'loggedIn' => true, |
|
||||||
]); |
|
||||||
|
|
||||||
$this->assertTrue($this->library->verifyUserTotpCode('999000')); |
|
||||||
|
|
||||||
$session = $this->getInstance(); |
$session = $this->getInstance(); |
||||||
$this->library = new Aauth($config, $session); |
$this->library = new Aauth($config, $session); |
||||||
$session->set('user', [ |
$session->set('user', [ |
||||||
@ -142,9 +192,6 @@ class TOTPTest extends CIDatabaseTestCase |
|||||||
'totp_required' => true, |
'totp_required' => true, |
||||||
]); |
]); |
||||||
|
|
||||||
$this->assertTrue($this->library->verifyUserTotpCode('999000')); |
|
||||||
$this->assertTrue($this->library->verifyUserTotpCode('999000', 1)); |
|
||||||
|
|
||||||
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||||
'user_id' => 1, |
'user_id' => 1, |
||||||
'data_key' => 'totp_secret', |
'data_key' => 'totp_secret', |
||||||
@ -157,6 +204,7 @@ class TOTPTest extends CIDatabaseTestCase |
|||||||
$totpCode = $totp->now(); |
$totpCode = $totp->now(); |
||||||
usleep(1000); |
usleep(1000); |
||||||
|
|
||||||
|
$this->assertTrue($this->library->verifyUserTotpCode($totpCode)); |
||||||
$this->assertTrue($this->library->verifyUserTotpCode($totpCode, 1)); |
$this->assertTrue($this->library->verifyUserTotpCode($totpCode, 1)); |
||||||
} |
} |
||||||
|
|
||||||
|
Loading…
Reference in new issue