首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Agiletoolkit登录尝试失败次数最多

Agiletoolkit登录尝试失败次数最多
EN

Stack Overflow用户
提问于 2013-10-04 18:40:52
回答 2查看 186关注 0票数 1

agiletoolkit /basic类允许不受任何限制地尝试登录。我正在寻找一种方法来限制失败的登录尝试的次数,我试图覆盖这个类的一些方法,但是它使用ajax重新加载,所以php代码无法正确执行。

任何建议都是欢迎的。

非常感谢,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-31 17:19:58

在StackOverflow的启发下,我实现了以下保护:

对于每个用户,我们存储软/硬锁。硬锁将拒绝验证密码,即使它是正确的。软锁是一段时间后,我们将重置“失败的尝试”计数器。每次输入不正确的密码时,软锁和硬锁都会增加,使您等待的时间更长(但不是永远),渐进式的增加设置了一个合理的限制,因此如果攻击者试图破解您的帐户一个小时--他只会尝试几次,但您的帐户在几个小时后就可以登录了。我已经在我的一个项目中实现了这一点,尽管它不是一个控制器,而是一个内置代码。请看一看评论,我希望这将有助于您和其他人:

在用户模型中添加:

代码语言:javascript
复制
$this->addField('pwd_locked_until');
$this->addField('pwd_failure_count');
$this->addField('pwd_soft_unlock');

您还需要两种方法:

代码语言:javascript
复制
/* 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();
}

代码语言:javascript
复制
/* 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();
}

最后-您可以使用它作为登录表单:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-10-04 20:09:50

我认为您可以在会话中存储Model_User的使用数(通常在8月中使用),或者使用afterLoad钩子存储cookie的使用数,然后在需要的地方检查它。

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

代码语言:javascript
复制
    $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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19188514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档