Skip to content

Commit

Permalink
Namespace login locking methods (#31)
Browse files Browse the repository at this point in the history
* Rename #lock! to #login_lock!

* Rename #unlock! to #login_unlock!

* Rename #locked? to #login_locked?

* Rename #unlocked? to #login_unlocked?

* Fix spec

* Update CHANGELOG.md

* Change CHANGELOG.md wording
  • Loading branch information
kyuden authored and joshbuker committed Dec 29, 2016
1 parent 0b83add commit 6ccbec9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Added support for Rails 5 by @kyuden
* Added WeChat provider to external submodule.
* Added support for Ruby 2.4 by @kyuden
* Namespace login lock/unlock methods to fix conflicts with Rails lock/unlock (thanks to @kyuden)

## 0.9.1

Expand Down
22 changes: 11 additions & 11 deletions lib/sorcery/model/submodules/brute_force_protection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,36 @@ def define_brute_force_protection_fields

module InstanceMethods
# Called by the controller to increment the failed logins counter.
# Calls 'lock!' if login retries limit was reached.
# Calls 'login_lock!' if login retries limit was reached.
def register_failed_login!
config = sorcery_config
return unless unlocked?
return unless login_unlocked?

sorcery_adapter.increment(config.failed_logins_count_attribute_name)

if send(config.failed_logins_count_attribute_name) >= config.consecutive_login_retries_amount_limit
lock!
login_lock!
end
end

# /!\
# Moved out of protected for use like activate! in controller
# /!\
def unlock!
def login_unlock!
config = sorcery_config
attributes = { config.lock_expires_at_attribute_name => nil,
config.failed_logins_count_attribute_name => 0,
config.unlock_token_attribute_name => nil }
sorcery_adapter.update_attributes(attributes)
end

def locked?
!unlocked?
def login_locked?
!login_unlocked?
end

protected

def lock!
def login_lock!
config = sorcery_config
attributes = { config.lock_expires_at_attribute_name => Time.now.in_time_zone + config.login_lock_time_period,
config.unlock_token_attribute_name => TemporaryToken.generate_random_token }
Expand All @@ -98,7 +98,7 @@ def lock!
end
end

def unlocked?
def login_unlocked?
config = sorcery_config
send(config.lock_expires_at_attribute_name).nil?
end
Expand All @@ -113,10 +113,10 @@ def send_unlock_token_email!
# Runs as a hook before authenticate.
def prevent_locked_user_login
config = sorcery_config
if !unlocked? && config.login_lock_time_period != 0
unlock! if send(config.lock_expires_at_attribute_name) <= Time.now.in_time_zone
if !login_unlocked? && config.login_lock_time_period != 0
login_unlock! if send(config.lock_expires_at_attribute_name) <= Time.now.in_time_zone
end
unlocked?
login_unlocked?
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
expect(config.login_lock_time_period).to eq 2.hours
end

describe '#locked?' do
describe '#login_locked?' do
it 'is locked' do
user.send("#{config.lock_expires_at_attribute_name}=", Time.now + 5.days)
expect(user).to be_locked
expect(user).to be_login_locked
end

it "isn't locked" do
user.send("#{config.lock_expires_at_attribute_name}=", nil)
expect(user).not_to be_locked
expect(user).not_to be_login_locked
end
end
end
Expand Down Expand Up @@ -130,7 +130,7 @@
end
end

describe '#unlock!' do
describe '#login_unlock!' do
it 'unlocks after entering unlock token' do
sorcery_model_property_set(:consecutive_login_retries_amount_limit, 2)
sorcery_model_property_set(:login_lock_time_period, 0)
Expand All @@ -144,7 +144,7 @@

expect(user).not_to be_nil

user.unlock!
user.login_unlock!
expect(User.load_from_unlock_token(user.unlock_token)).to be_nil
end
end
Expand Down

0 comments on commit 6ccbec9

Please sign in to comment.