Module: Authenticate::Model::BruteForce

Extended by:
ActiveSupport::Concern
Defined in:
lib/authenticate/model/brute_force.rb

Overview

Protect from brute force attacks. Lock accounts that have too many failed consecutive logins. Todo: email user to allow unlocking via a token.

To enable brute force protection, set the config params shown below. Example:

Authenticate.configure do |config|
  config. = 5.minutes
  config.max_consecutive_bad_logins_allowed = 3
end

Columns

  • failed_logins_count - each consecutive failed login increments this counter. Set back to 0 on successful login.

  • lock_expires_at - datetime a locked account will again become available.

Configuration

  • max_consecutive_bad_logins_allowed - how many failed logins are allowed?

  • bad_login_lockout_period - how long is the user locked out? nil indicates forever.

Methods

The following methods are added to your user model:

  • register_failed_login! - increment failed_logins_count, lock account if in violation

  • lock! - lock the account, setting the lock_expires_at attribute

  • unlock! - reset failed_logins_count to 0, lock_expires_at to nil

  • locked? - is the account locked? @return

  • unlocked? - is the account unlocked? @return

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.required_fields(klass) ⇒ Object



36
37
38
# File 'lib/authenticate/model/brute_force.rb', line 36

def self.required_fields(klass)
  [:failed_logins_count, :lock_expires_at]
end

Instance Method Details

#lock!Object



47
48
49
# File 'lib/authenticate/model/brute_force.rb', line 47

def lock!
  self.update_attribute(:lock_expires_at, Time.now.utc + lockout_period)
end

#locked?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/authenticate/model/brute_force.rb', line 55

def locked?
  !unlocked?
end

#register_failed_login!Object



41
42
43
44
45
# File 'lib/authenticate/model/brute_force.rb', line 41

def register_failed_login!
  self.failed_logins_count ||= 0
  self.failed_logins_count += 1
  lock! if self.failed_logins_count >= max_bad_logins
end

#unlock!Object



51
52
53
# File 'lib/authenticate/model/brute_force.rb', line 51

def unlock!
  self.update_attributes({failed_logins_count: 0, lock_expires_at: nil})
end

#unlocked?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/authenticate/model/brute_force.rb', line 59

def unlocked?
  self.lock_expires_at.nil?
end