agiletoolkit Auth / basic类允许尝试登录而没有任何限制。 我正在寻找一种方法来限制失败的登录尝试次数,我试图覆盖这个类的一些方法,但它使用ajax重新加载,因此php代码没有正确执行。
欢迎提出任何建议。
非常感谢,
答案 0 :(得分:1)
我认为您可以使用afterLoad挂钩在会话或cookie中存储Model_User(通常在Auth中使用)的使用次数,然后在需要的位置进行检查。
糟糕。失败登录。所以你没有加载模型。 您只需计算登录表单按钮上的点击次数并将其存储在某处(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();
}
}
我没有检查。也许需要一些限制。 只是一个想法。
希望有所帮助。
答案 1 :(得分: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');
}
}
}
}
这应该由某人转换为附加组件。