2013-10-04 75 views
1

agiletoolkit Auth/basic类允许尝试登录,没有任何限制。 而我正在寻找一种方法来限制失败的登录尝试次数,我试着重写这个类的一些方法,但它使用Ajax重新加载,所以php代码不能正确执行。Agiletoolkit失败登录尝试的最大次数

任何建议是值得欢迎的。

非常感谢,

回答

1

通过StackOverflow的启发我已实现了以下保障:

为我们存储软/硬锁定每个用户。即使它是正确的,硬锁也会拒绝验证密码。 软锁是一段时间后,我们将重置“不成功的尝试”计数器。 每当你输入不正确的密码时,软锁和硬锁的增加都会让你等待更长的时间(但不会永远) 逐步增加提供合理的限制,所以如果你的攻击者试图破解你的帐户一个小时 - 他只会尝试多次,但您的帐户在几个小时后即可登录。 我已经在我的一个项目中实现了它,但它不是控制器,而是内置代码。请通过意见看,我希望这将帮助您和他人:

在用户添加到模型:

$this->addField('pwd_locked_until'); 
$this->addField('pwd_failure_count'); 
$this->addField('pwd_soft_unlock'); 

你还需要两个方法:

/* Must be called when user unsuccessfully tried to log-in */ 
function passwordIncorrect(){ 
    $su=strtotime($this['pwd_soft_unlock']); 
    if($su && $su>time()){ 
     // aw, they repeatedly typed password in, lets teach them power of two! 
     $this['pwd_failure_count']=$this['pwd_failure_count']+1; 
     if($this['pwd_failure_count']>3){ 
      $this['pwd_locked_until']=date('Y-m-d H:i:s',time() 
       +pow(2,min($this['pwd_failure_count'],20))); 

      $this['pwd_soft_unlock']=date('Y-m-d H:i:s',time() 
       +max(2*pow(2,min($this['pwd_failure_count'],20)),60*5)); 
     } 
    }else{ 
     $this['pwd_failure_count']=1; 
     $this['pwd_soft_unlock']=date('Y-m-d H:i:s',time() +60*5); 
    } 
    $this->save(); 
} 

/* Must be called when user logs in successfully */ 
function loginSuccessful(){ 
    $this['last_seen']=date('Y-m-d H:i:s'); 
    $this['pwd_soft_unlock']=null; 
    $this->save(); 
} 

最后 - 你可以用这个作为登录表单:

class Form_Login extends Form { 
    function init(){ 
    parent::init(); 

    $form=$this; 
    $form->setModel('User',array('email','password')); 
    $form->addSubmit('Login'); 

    if($form->isSubmitted()){ 

     $auth=$this->api->auth; 

     $l=$form->get('email'); 
     $p=$form->get('password'); 

     // check to see if user with such email exist 
     $u=$this->add('Model_User'); 
     $u->tryLoadBy('email',$form->get('email')); 

     // user may have also typed his username 
     if(!$u->loaded()){ 
      $u->tryLoadBy('user_name',$form->get('email')); 
     } 

     // incorrect email - but say that password is wrong 
     if(!$u->loaded())$form->getElement('password') 
      ->displayFieldError('Incorrect Login'); 

     // if login is locked, don't verify password at all 
     $su=strtotime($u['pwd_locked_until']); 
     if($su>time()){ 
      $form->getElement('password') 
       ->displayFieldError('Account is locked for '. 
       $this->add('Controller_Fancy') 
      ->fancy_datetime($u['pwd_locked_until'])); 
     } 

     // check account 
     if($auth->verifyCredentials($u['email'],$p)){ 
      // resets incorrect login statistics 
      $u->loginSuccessful(); 
      $auth->login($l); 

      // redirect user 
      $form->js()->univ()->location($this->api->url('/'))->execute(); 
     }else{ 
      // incorrect password, register failed attempt 
      $u->passwordIncorrect(); 
      $form->getElement('password')->displayFieldError('Incorrect Login'); 
     } 
    } 
    } 
} 

这应该由某人转换成插件。

1

我想你可以存储在会话或使用后负荷钩饼干Model_User的使用(通常在验证中使用)的数字,然后检查它,你需要。

糟糕。失败登录。所以你模型没有加载。 您只需要对登录表单按钮的点击次数进行计数并将其存储在某个地方(Cookie,数据库)。因此,创建自己的登录表单并在提交表单时添加一些条件,例如:

$m = $this->add('Model_User'); 

    $f = $this->add("Form"); 
    $f->setModel($m, array('email', 'password')); 
    $f->addSubmit("Log In"); 

    if ($f->isSubmitted()){ 
     //Limmiting 
     if($_COOKIE[$this->api->name."_count_failed_login"] >= 5/*Here's you limit number*/){ 
      /*redirect or something else*/ 
     } 

     if (/*here should be you condition*/){ 
      $_COOKIE[$this->api->name."_count_failed_login"] = 0; 
      $this->api->auth->login($f->get("email")); 
      $f->js()->univ()->redirect("index")->execute(); 
     }else{ 
      /* when login is failed*/ 
      if($_COOKIE[$this->api->name."_count_failed_login"]){ 
       $_COOKIE[$this->api->name."_count_failed_login"]++; 
      }else{ 
       $_COOKIE[$this->api->name."_count_failed_login"] = 1; 
      } 
      $this->js()->univ()->alert('Wrong username or password')->execute(); 
     } 
    } 

我没有检查它。也许需要一些辅助。 只是一个想法。

希望有所帮助。