Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions app/controllers/concerns/idv/verify_info_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def shared_update
pii[:uuid_prefix] = ServiceProvider.find_by(issuer: sp_session[:issuer])&.app_id
set_state_id_type

ssn_throttle.increment!
ssn_rate_limiter.increment!

document_capture_session = DocumentCaptureSession.create(
user_id: current_user.id,
Expand Down Expand Up @@ -61,17 +61,17 @@ def aamva_disallowed_for_service_provider?
banlist.include?(sp_session[:issuer])
end

def resolution_throttle
@resolution_throttle ||= Throttle.new(
def resolution_rate_limiter
@resolution_rate_limiter ||= RateLimiter.new(
user: current_user,
throttle_type: :idv_resolution,
rate_limit_type: :idv_resolution,
)
end

def ssn_throttle
@ssn_throttle ||= Throttle.new(
def ssn_rate_limiter
@ssn_rate_limiter ||= RateLimiter.new(
target: Pii::Fingerprinter.fingerprint(pii[:ssn]),
throttle_type: :proof_ssn,
rate_limit_type: :proof_ssn,
)
end

Expand All @@ -85,14 +85,14 @@ def idv_failure(result)
:mva_exception,
)

resolution_throttle.increment! if proofing_results_exception.blank?
resolution_rate_limiter.increment! if proofing_results_exception.blank?

if ssn_throttle.throttled?
idv_failure_log_throttled(:proof_ssn)
if ssn_rate_limiter.limited?
idv_failure_log_rate_limited(:proof_ssn)
redirect_to idv_session_errors_ssn_failure_url
elsif resolution_throttle.throttled?
idv_failure_log_throttled(:idv_resolution)
redirect_to throttled_url
elsif resolution_rate_limiter.limited?
idv_failure_log_rate_limited(:idv_resolution)
redirect_to rate_limited_url
elsif proofing_results_exception.present? && is_mva_exception
idv_failure_log_warning
redirect_to state_id_warning_url
Expand All @@ -105,14 +105,14 @@ def idv_failure(result)
end
end

def idv_failure_log_throttled(throttle_type)
if throttle_type == :proof_ssn
def idv_failure_log_rate_limited(rate_limit_type)
if rate_limit_type == :proof_ssn
irs_attempts_api_tracker.idv_verification_rate_limited(throttle_context: 'multi-session')
analytics.throttler_rate_limit_triggered(
throttle_type: :proof_ssn,
step_name: STEP_NAME,
)
elsif throttle_type == :idv_resolution
elsif rate_limit_type == :idv_resolution
irs_attempts_api_tracker.idv_verification_rate_limited(throttle_context: 'single-session')
analytics.throttler_rate_limit_triggered(
throttle_type: :idv_resolution,
Expand All @@ -124,18 +124,18 @@ def idv_failure_log_throttled(throttle_type)
def idv_failure_log_error
analytics.idv_doc_auth_exception_visited(
step_name: STEP_NAME,
remaining_attempts: resolution_throttle.remaining_count,
remaining_attempts: resolution_rate_limiter.remaining_count,
)
end

def idv_failure_log_warning
analytics.idv_doc_auth_warning_visited(
step_name: STEP_NAME,
remaining_attempts: resolution_throttle.remaining_count,
remaining_attempts: resolution_rate_limiter.remaining_count,
)
end

def throttled_url
def rate_limited_url
idv_session_errors_failure_url
end

Expand Down Expand Up @@ -203,7 +203,7 @@ def async_state_done(current_async_state)
)

form_response = form_response.merge(check_ssn) if form_response.success?
summarize_result_and_throttle_failures(form_response)
summarize_result_and_rate_limit_failures(form_response)
delete_async

if form_response.success?
Expand Down Expand Up @@ -231,10 +231,10 @@ def save_threatmetrix_status(form_response)
idv_session.threatmetrix_review_status = review_status
end

def summarize_result_and_throttle_failures(summary_result)
def summarize_result_and_rate_limit_failures(summary_result)
if summary_result.success?
add_proofing_components
ssn_throttle.reset!
ssn_rate_limiter.reset!
else
idv_failure(summary_result)
end
Expand Down
40 changes: 20 additions & 20 deletions app/controllers/concerns/rate_limit_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ module RateLimitConcern

def confirm_not_rate_limited
rate_limited = false
%i[idv_resolution idv_doc_auth proof_address proof_ssn].each do |throttle_type|
if rate_limit_redirect!(throttle_type)
%i[idv_resolution idv_doc_auth proof_address proof_ssn].each do |rate_limit_type|
if rate_limit_redirect!(rate_limit_type)
rate_limited = true
break
end
end
rate_limited
end

def rate_limit_redirect!(throttle_type)
if idv_attempter_rate_limited?(throttle_type)
track_rate_limited_event(throttle_type)
rate_limited_redirect(throttle_type)
def rate_limit_redirect!(rate_limit_type)
if idv_attempter_rate_limited?(rate_limit_type)
track_rate_limited_event(rate_limit_type)
rate_limited_redirect(rate_limit_type)
return true
end
end

def track_rate_limited_event(throttle_type)
analytics_args = { throttle_type: throttle_type }
def track_rate_limited_event(rate_limit_type)
analytics_args = { throttle_type: rate_limit_type }
throttle_context = 'single-session'

if throttle_type == :proof_address
if rate_limit_type == :proof_address
analytics_args[:step_name] = :phone
elsif throttle_type == :proof_ssn
elsif rate_limit_type == :proof_ssn
analytics_args[:step_name] = 'verify_info'
throttle_context = 'multi-session'
end
Expand All @@ -35,8 +35,8 @@ def track_rate_limited_event(throttle_type)
analytics.throttler_rate_limit_triggered(**analytics_args)
end

def rate_limited_redirect(throttle_type)
case throttle_type
def rate_limited_redirect(rate_limit_type)
case rate_limit_type
when :idv_resolution
redirect_to idv_session_errors_failure_url
when :idv_doc_auth
Expand All @@ -48,18 +48,18 @@ def rate_limited_redirect(throttle_type)
end
end

def idv_attempter_rate_limited?(throttle_type)
if throttle_type == :proof_ssn
def idv_attempter_rate_limited?(rate_limit_type)
if rate_limit_type == :proof_ssn
return unless pii_ssn
Throttle.new(
RateLimiter.new(
target: Pii::Fingerprinter.fingerprint(pii_ssn),
throttle_type: :proof_ssn,
).throttled?
rate_limit_type: :proof_ssn,
).limited?
else
Throttle.new(
RateLimiter.new(
user: idv_session_user,
throttle_type: throttle_type,
).throttled?
rate_limit_type: rate_limit_type,
).limited?
end
end

Expand Down
15 changes: 7 additions & 8 deletions app/controllers/idv/capture_doc_status_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def status
:unauthorized
elsif document_capture_session.cancelled_at
:gone
elsif throttled?
elsif rate_limiter.limited?
:too_many_requests
elsif confirmed_barcode_attention_result? || user_has_establishing_in_person_enrollment?
:ok
Expand All @@ -33,7 +33,7 @@ def status
def redirect_url
return unless flow_session && document_capture_session

if throttled?
if rate_limiter.limited?
idv_session_errors_throttled_url
elsif user_has_establishing_in_person_enrollment?
idv_in_person_url
Expand All @@ -59,12 +59,11 @@ def document_capture_session_uuid
flow_session[:document_capture_session_uuid]
end

def throttled?
Copy link
Copy Markdown
Contributor

@amirbey amirbey Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice to get rid of this 🥳

throttle.throttled?
end

def throttle
@throttle ||= Throttle.new(user: document_capture_session.user, throttle_type: :idv_doc_auth)
def rate_limiter
@rate_limiter ||= RateLimiter.new(
user: document_capture_session.user,
rate_limit_type: :idv_doc_auth,
)
end

def user_has_establishing_in_person_enrollment?
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/idv/gpo_verify_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def index
!gpo_mail.mail_spammed? &&
!gpo_mail.profile_too_old?

if throttle.throttled?
render_throttled
if rate_limiter.limited?
render_rate_limited
elsif pii_locked?
redirect_to capture_password_url
else
Expand All @@ -34,9 +34,9 @@ def pii
def create
@gpo_verify_form = build_gpo_verify_form

throttle.increment!
if throttle.throttled?
render_throttled
rate_limiter.increment!
if rate_limiter.limited?
render_rate_limited
return
end

Expand Down Expand Up @@ -85,20 +85,20 @@ def prepare_for_personal_key
idv_session.address_confirmed!
end

def throttle
@throttle ||= Throttle.new(
def rate_limiter
@rate_limiter ||= RateLimiter.new(
user: current_user,
throttle_type: :verify_gpo_key,
rate_limit_type: :verify_gpo_key,
)
end

def render_throttled
def render_rate_limited
irs_attempts_api_tracker.idv_gpo_verification_rate_limited
analytics.throttler_rate_limit_triggered(
throttle_type: :verify_gpo_key,
)

@expires_at = throttle.expires_at
@expires_at = rate_limiter.expires_at
render :throttled
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be changed in follow up ticket/PR?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was avoiding changing anything route-related. Could this be changed by changing just the name of the template file? I still think it should be in a followup PR.

end

Expand Down
14 changes: 7 additions & 7 deletions app/controllers/idv/hybrid_handoff_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def hybrid_flow_chosen?
end

def handle_phone_submission
throttle.increment!
return throttled_failure if throttle.throttled?
rate_limiter.increment!
return rate_limited_failure if rate_limiter.limited?
idv_session.phone_for_mobile_flow = params[:doc_auth][:phone]
idv_session.flow_path = 'hybrid'
flow_session[:flow_path] = 'hybrid' # temp addition for 50/50 remove in future deploy
Expand Down Expand Up @@ -147,10 +147,10 @@ def build_form
)
end

def throttle
@throttle ||= Throttle.new(
def rate_limiter
@rate_limiter ||= RateLimiter.new(
user: current_user,
throttle_type: :idv_send_link,
rate_limit_type: :idv_send_link,
)
end

Expand All @@ -175,15 +175,15 @@ def form_response(destination:)
)
end

def throttled_failure
def rate_limited_failure
analytics.throttler_rate_limit_triggered(
throttle_type: :idv_send_link,
)
message = I18n.t(
'errors.doc_auth.send_link_throttle',
timeout: distance_of_time_in_words(
Time.zone.now,
[throttle.expires_at, Time.zone.now].compact.max,
[rate_limiter.expires_at, Time.zone.now].compact.max,
except: :seconds,
),
)
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/idv/phone_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def new
async_state = step.async_state

# It's possible that create redirected here after a success and left the
# throttle maxed out. Check for success before checking throttle.
# rate_limiter maxed out. Check for success before checking rate_limiter.
return async_state_done(async_state) if async_state.done?

render 'shared/wait' and return if async_state.in_progress?
Expand Down Expand Up @@ -58,8 +58,8 @@ def create

private

def throttle
@throttle ||= Throttle.new(user: current_user, throttle_type: :proof_address)
def rate_limiter
@rate_limiter ||= RateLimiter.new(user: current_user, rate_limit_type: :proof_address)
end

def redirect_to_next_step
Expand Down Expand Up @@ -171,7 +171,7 @@ def async_state_done(async_state)
)

if async_state.result[:success]
throttle.reset!
rate_limiter.reset!
redirect_to_next_step and return
end
handle_proofing_failure
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/idv/phone_errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PhoneErrorsController < ApplicationController
before_action :ignore_form_step_wait_requests

def warning
@remaining_attempts = throttle.remaining_count
@remaining_attempts = rate_limiter.remaining_count

if idv_session.previous_phone_step_params
@phone = idv_session.previous_phone_step_params[:phone]
Expand All @@ -20,24 +20,24 @@ def warning
end

def timeout
@remaining_step_attempts = throttle.remaining_count
@remaining_step_attempts = rate_limiter.remaining_count
track_event(type: :timeout)
end

def jobfail
@remaining_attempts = throttle.remaining_count
@remaining_attempts = rate_limiter.remaining_count
track_event(type: :jobfail)
end

def failure
@expires_at = throttle.expires_at
@expires_at = rate_limiter.expires_at
track_event(type: :failure)
end

private

def throttle
Throttle.new(user: idv_session.current_user, throttle_type: :proof_address)
def rate_limiter
RateLimiter.new(user: idv_session.current_user, rate_limit_type: :proof_address)
end

def confirm_idv_phone_step_needed
Expand Down
Loading