-
Notifications
You must be signed in to change notification settings - Fork 167
Lock users out temporarily after too many OTPs #1464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class OtpRequestsTracker < ActiveRecord::Base | ||
| include EncryptableAttribute | ||
|
|
||
| encrypted_attribute_without_setter(name: :phone) | ||
|
|
||
| def self.find_or_create_with_phone(phone) | ||
| tries ||= 1 | ||
| phone ||= phone.strip | ||
| phone_fingerprint ||= Pii::Fingerprinter.fingerprint(phone) | ||
|
|
||
| where(phone_fingerprint: phone_fingerprint). | ||
| first_or_create(phone: phone, otp_send_count: 0, otp_last_sent_at: Time.zone.now) | ||
| rescue ActiveRecord::RecordNotUnique | ||
| retry unless (tries -= 1).zero? | ||
| raise | ||
| end | ||
|
|
||
| def phone=(phone) | ||
| set_encrypted_attribute(name: :phone, value: phone) | ||
| self.phone_fingerprint = phone.present? ? encrypted_attributes[:phone].fingerprint : '' | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| class OtpRateLimiter | ||
| def initialize(phone:, user:) | ||
| @phone = phone | ||
| @user = user | ||
| end | ||
|
|
||
| def exceeded_otp_send_limit? | ||
| return false if entry_for_current_phone.blank? | ||
|
|
||
| if rate_limit_period_expired? | ||
| reset_count_and_otp_last_sent_at | ||
| return false | ||
| end | ||
|
|
||
| max_requests_reached? | ||
| end | ||
|
|
||
| def max_requests_reached? | ||
| entry_for_current_phone.otp_send_count >= otp_maxretry_times | ||
| end | ||
|
|
||
| def rate_limit_period_expired? | ||
| otp_last_sent_at.present? && (otp_last_sent_at + otp_findtime) < Time.zone.now | ||
| end | ||
|
|
||
| def reset_count_and_otp_last_sent_at | ||
| entry_for_current_phone.update(otp_last_sent_at: Time.zone.now, otp_send_count: 0) | ||
| end | ||
|
|
||
| def lock_out_user | ||
| UpdateUser.new(user: user, attributes: { second_factor_locked_at: Time.zone.now }).call | ||
| end | ||
|
|
||
| def increment | ||
| now = Time.zone.now | ||
|
|
||
| entry_for_current_phone.otp_send_count += 1 | ||
| entry_for_current_phone.otp_last_sent_at = now | ||
| entry_for_current_phone.save! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :phone, :user | ||
|
|
||
| def entry_for_current_phone | ||
| @entry ||= OtpRequestsTracker.find_or_create_with_phone(phone) | ||
| end | ||
|
|
||
| def otp_last_sent_at | ||
| entry_for_current_phone.otp_last_sent_at | ||
| end | ||
|
|
||
| def otp_findtime | ||
| Figaro.env.otp_delivery_blocklist_findtime.to_i.minutes | ||
| end | ||
|
|
||
| def otp_maxretry_times | ||
| Figaro.env.otp_delivery_blocklist_maxretry.to_i | ||
| end | ||
| end |
19 changes: 19 additions & 0 deletions
19
app/views/two_factor_authentication/shared/max_otp_requests_reached.html.erb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <% lockout_time_in_words = decorator.lockout_time_remaining_in_words %> | ||
| <% lockout_time_remaining = decorator.lockout_time_remaining %> | ||
| <% title t('titles.account_locked') %> | ||
|
|
||
| <h1 class="h3 my0"> | ||
| <%= t('titles.account_locked') %> | ||
| </h1> | ||
| <p> | ||
| <%= t("devise.two_factor_authentication.max_otp_requests_reached") %> | ||
| </p> | ||
| <p> | ||
| <%= t('devise.two_factor_authentication.please_try_again_html', | ||
| time_remaining: content_tag(:span, lockout_time_in_words, id: 'countdown')) %> | ||
| </p> | ||
|
|
||
| <%= nonced_javascript_tag do %> | ||
| var test = <%= lockout_time_remaining %> * 1000; | ||
| window.LoginGov.countdownTimer(document.getElementById('countdown'), test); | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class CreateOtpRequestsTracker < ActiveRecord::Migration | ||
| def change | ||
| create_table :otp_requests_trackers do |t| | ||
| t.text :encrypted_phone | ||
| t.timestamp :otp_last_sent_at | ||
| t.integer :otp_send_count, default: 0 | ||
| t.string :attribute_cost | ||
| t.string :phone_fingerprint, default: '', null: false | ||
|
|
||
| t.timestamps | ||
|
|
||
| t.index :phone_fingerprint, unique: true | ||
| t.index :updated_at | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to
raisewhen we're not retrying otherwise this will just returnniland we'll get aNoMethodErrorrather than a duplicate key errorso like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet! Thanks!!