Class: RailsBase::Authentication::SendLoginMfaToUser

Inherits:
ServiceBase
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, VelocityLimiter
Defined in:
app/services/rails_base/authentication/send_login_mfa_to_user.rb

Defined Under Namespace

Classes: NoPhoneNumber

Constant Summary collapse

MAX_USE_COUNT =
1.freeze
DATA_USE =
:numeric

Instance Method Summary collapse

Methods included from VelocityLimiter

#_velocity_limiter_params_validator!, #cache_delineator, #velocity_limit_message, #velocity_limit_reached?, #vl_metadata, #vl_read, #vl_time, #vl_write!

Methods inherited from ServiceBase

inherited, #internal_validate, #service_base_logging

Methods included from ServiceLogging

#aletered_message, #class_name, #log, #log_prefix, #logger, #service_id

Instance Method Details

#cache_keyObject



64
65
66
# File 'app/services/rails_base/authentication/send_login_mfa_to_user.rb', line 64

def cache_key
  "#{self.class.name.downcase}.#{user.id}"
end

#callObject



17
18
19
20
21
22
23
24
# File 'app/services/rails_base/authentication/send_login_mfa_to_user.rb', line 17

def call
  velocity = velocity_limit_reached?
  context.fail!(message: velocity[:msg]) if velocity[:reached]

  data_point = create_short_lived_data
  send_twilio!(data_point.data)
  context.short_lived_data = data_point
end

#create_short_lived_dataObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/rails_base/authentication/send_login_mfa_to_user.rb', line 39

def create_short_lived_data
  params = {
    user: user,
    max_use: MAX_USE_COUNT,
    reason: Constants::MFA_REASON,
    data_use: DATA_USE,
    ttl: Constants::SLMTU_TTL,
    expires_at: expires_at,
    length: Constants::MFA_LENGTH,
  }
  ShortLivedData.create_data_key(**params)
end

#message(code) ⇒ Object



35
36
37
# File 'app/services/rails_base/authentication/send_login_mfa_to_user.rb', line 35

def message(code)
  "Hello #{user.full_name}. Here is your verification code #{code}."
end

#send_twilio!(code) ⇒ Object



26
27
28
29
30
31
32
33
# File 'app/services/rails_base/authentication/send_login_mfa_to_user.rb', line 26

def send_twilio!(code)
  TwilioJob.perform_later(message: message(code), to: user.phone_number)
  log(level: :info, msg: "Sent twilio message to #{user.phone_number}")
rescue StandardError => e
  log(level: :error, msg: "Error caught #{e.class.name}")
  log(level: :error, msg: "Failed to send sms to #{user.phone_number}")
  context.fail!(message: "Failed to send sms. Please retry logging in.")
end

#validate!Object

Raises:



68
69
70
71
72
73
74
75
# File 'app/services/rails_base/authentication/send_login_mfa_to_user.rb', line 68

def validate!
  raise "Expected user to be a User. Received #{user.class}" unless user.is_a? User
  if expires_at && !(expires_at.is_a?(ActiveSupport::TimeWithZone))
    raise "Expected expires_at to be a ActiveSupport::TimeWithZone. Given #{expires_at.class}"
  end

  raise NoPhoneNumber, "No phone for user [#{user.id}] [#{user.phone_number}]" if user.phone_number.nil?
end

#velocity_frameObject



60
61
62
# File 'app/services/rails_base/authentication/send_login_mfa_to_user.rb', line 60

def velocity_frame
  RailsBase.config.mfa.twilio_velocity_frame
end

#velocity_maxObject



56
57
58
# File 'app/services/rails_base/authentication/send_login_mfa_to_user.rb', line 56

def velocity_max
  RailsBase.config.mfa.twilio_velocity_max
end

#velocity_max_in_frameObject



52
53
54
# File 'app/services/rails_base/authentication/send_login_mfa_to_user.rb', line 52

def velocity_max_in_frame
  RailsBase.config.mfa.twilio_velocity_max_in_frame
end