Skip to content

Commit

Permalink
Add 'reset_password_attempt_expired?' instance method to check if tim…
Browse files Browse the repository at this point in the history
…e between emails has not passed since last email
  • Loading branch information
tanraya committed Aug 10, 2016
1 parent 5344880 commit 73d6929
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ User.load_from_reset_password_token(token)
@user.generate_reset_password_token! # if you want to send the email by youself
@user.deliver_reset_password_instructions! # generates the token and sends the email
@user.change_password!(new_password)
@user.reset_password_attempt_expired? # check if time between emails has not passed since last email
```

### user activation
Expand Down
14 changes: 11 additions & 3 deletions lib/sorcery/model/submodules/reset_password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ def generate_reset_password_token!
# generates a reset code with expiration and sends an email to the user.
def deliver_reset_password_instructions!
mail = false
config = sorcery_config

# hammering protection
return false if config.reset_password_time_between_emails.present? && self.send(config.reset_password_email_sent_at_attribute_name) && self.send(config.reset_password_email_sent_at_attribute_name) > config.reset_password_time_between_emails.seconds.ago.utc
return false if reset_password_attempt_expired?

self.class.sorcery_adapter.transaction do
generate_reset_password_token!
mail = send_reset_password_email! unless config.reset_password_mailer_disabled
mail = send_reset_password_email! unless sorcery_config.reset_password_mailer_disabled
end
mail
end
Expand All @@ -113,6 +114,13 @@ def change_password!(new_password)
sorcery_adapter.save
end

def reset_password_attempt_expired?
sorcery_config.reset_password_time_between_emails.present? &&
self.send(sorcery_config.reset_password_email_sent_at_attribute_name) &&
self.send(sorcery_config.reset_password_email_sent_at_attribute_name) >
sorcery_config.reset_password_time_between_emails.seconds.ago.utc
end

protected

def send_reset_password_email!
Expand Down
7 changes: 7 additions & 0 deletions spec/shared_examples/user_reset_password_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@
expect(user.deliver_reset_password_instructions!).to be false
end

it "'reset_password_attempt_expired?' returns false if time between emails has not passed since last email" do
sorcery_model_property_set(:reset_password_time_between_emails, 10000)
user.deliver_reset_password_instructions!

expect(user.reset_password_attempt_expired?).to be false
end

it "encrypts properly on reset" do
user.deliver_reset_password_instructions!
user.change_password!("blagu")
Expand Down

0 comments on commit 73d6929

Please sign in to comment.