Browse Source

added totp ability & updated tests

v3-dev
REJack 6 years ago
parent
commit
83d10aceb3
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 121
      app/Libraries/Aauth.php
  2. 12
      tests/Aauth/Libraries/Aauth/AccessTest.php

121
app/Libraries/Aauth.php

@ -285,60 +285,62 @@ class Aauth
return false;
}
// if ($this->config->totpEnabled && ! $this->config->totpOnIpChange && $this->config->totpLogin)
// {
// if ($this->config->totpLogin == true)
// {
// $this->session->set('totp_required', true);
// }
// $totp_secret = $userVariableModel->find($user['id'], 'totp_secret', true);
// if ( ! empty($totp_secret) && ! $totp_code) {
// $this->error(lang('Aauth.requiredTOTPCode'));
// return false;
// } else {
// if( ! empty($totp_secret)){
// $this->CI->load->helper('googleauthenticator');
// $ga = new PHPGangsta_GoogleAuthenticator();
// $checkResult = $ga->verifyCode($totp_secret, $totp_code, 0);
// if ( ! $checkResult) {
// $this->error(lang('Aauth.invalidTOTPCode'));
// return false;
// }
// }
// }
// }
// else if ($this->config->totpEnabled && $this->config->totpOnIpChange)
// {
// $query = null;
// $query = $this->aauth_db->where($db_identifier, $identifier);
// $query = $this->aauth_db->get($this->config->users);
// $totp_secret = $query->row()->totp_secret;
// $ip_address = $query->row()->ip_address;
// $current_ip_address = $this->CI->input->ip_address();
// if ($query->num_rows() > 0 AND !$totp_code) {
// if($ip_address != $current_ip_address ){
// if($this->config->totpLogin == false){
// $this->error(lang('Aauth.aauth_error_totp_code_required'));
// return false;
// } else if($this->config->totpLogin == true){
// $this->session->set('totp_required', true);
// }
// }
// }else {
// if(!empty($totp_secret)){
// if($ip_address != $current_ip_address ){
// $this->CI->load->helper('googleauthenticator');
// $ga = new PHPGangsta_GoogleAuthenticator();
// $checkResult = $ga->verifyCode($totp_secret, $totp_code, 0);
// if (!$checkResult) {
// $this->error(lang('Aauth.aauth_error_totp_code_invalid'));
// return false;
// }
// }
// }
// }
// }
if ($this->config->totpEnabled)
{
$totpSecret = $userVariableModel->find($user['id'], 'totp_secret', true);
$request = \Config\Services::request();
if ($this->config->totpLogin)
{
if (! $this->config->totpOnIpChange)
{
if (! empty($totpSecret) && ! $totpCode)
{
$this->error(lang('Aauth.requiredTOTPCode'));
return false;
}
else if (! $this->verifyUserTotpCode($totpCode, $user['id']))
{
$this->error(lang('Aauth.invalidTOTPCode'));
return false;
}
}
else if ($this->config->totpOnIpChange)
{
if ($request->getIPAddress() !== $lastIpAddress)
{
if (! empty($totpSecret) && ! $totpCode)
{
$this->error(lang('Aauth.requiredTOTPCode'));
return false;
}
else if (! $this->verifyUserTotpCode($totpCode, $user['id']))
{
$this->error(lang('Aauth.invalidTOTPCode'));
return false;
}
}
}
}
else if (! $this->config->totpLogin)
{
if (! $this->config->totpOnIpChange)
{
$this->session->set('totp_required', true);
}
else if ($this->config->totpOnIpChange)
{
if ($request->getIPAddress() !== $lastIpAddress)
{
$this->session->set('totp_required', true);
}
}
}
}
if (password_verify($password, $user['password']))
{
@ -592,10 +594,13 @@ class Aauth
*/
public function isAllowed($permPar, int $userId = null)
{
// if($this->CI->session->userdata('totp_required')){
// $this->error($this->CI->lang->line('aauth_error_totp_verification_required'));
// redirect($this->config_vars['totp_two_step_login_redirect']);
// }
if ($this->config->totpEnabled && ! $this->config->totpLogin)
{
if ($this->isTotpRequired())
{
return redirect()->to($this->config->totpLink);
}
}
$userModel = new UserModel();

12
tests/Aauth/Libraries/Aauth/AccessTest.php

@ -159,6 +159,18 @@ class AccessTest extends CIDatabaseTestCase
$this->assertTrue($this->library->isAllowed('testPerm1'));
$session->remove('user');
$config->totpEnabled = true;
$session = $this->getInstance();
$this->library = new Aauth($config, $session);
$session->set('user', [
'id' => 1,
'loggedIn' => true,
'totp_required' => true,
]);
$this->assertTrue($this->library->isAllowed('testPerm1') instanceof \CodeIgniter\HTTP\RedirectResponse);
$session->remove('user');
$this->assertFalse($this->library->isAllowed('testPerm99', 2));
$this->assertFalse($this->library->isAllowed('testPerm1', 99));
}

Loading…
Cancel
Save