Class: RailsBase::Mfa::Sms::Send

Inherits:
ServiceBase show all
Includes:
ActionView::Helpers::DateHelper, VelocityLimiter
Defined in:
app/services/rails_base/mfa/sms/send.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



70
71
72
# File 'app/services/rails_base/mfa/sms/send.rb', line 70

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

#callObject



19
20
21
22
23
24
25
26
# File 'app/services/rails_base/mfa/sms/send.rb', line 19

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



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/rails_base/mfa/sms/send.rb', line 45

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

#message(code) ⇒ Object



41
42
43
# File 'app/services/rails_base/mfa/sms/send.rb', line 41

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

#phone_numberObject



37
38
39
# File 'app/services/rails_base/mfa/sms/send.rb', line 37

def phone_number
  context.phone_number || user.phone_number
end

#send_twilio!(code) ⇒ Object



28
29
30
31
32
33
34
35
# File 'app/services/rails_base/mfa/sms/send.rb', line 28

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

#validate!Object

Raises:



74
75
76
77
78
79
80
81
# File 'app/services/rails_base/mfa/sms/send.rb', line 74

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}] [#{phone_number}]" if phone_number.nil?
end

#velocity_frameObject



66
67
68
# File 'app/services/rails_base/mfa/sms/send.rb', line 66

def velocity_frame
  RailsBase.config.twilio.twilio_velocity_frame
end

#velocity_maxObject



62
63
64
# File 'app/services/rails_base/mfa/sms/send.rb', line 62

def velocity_max
  RailsBase.config.twilio.twilio_velocity_max
end

#velocity_max_in_frameObject



58
59
60
# File 'app/services/rails_base/mfa/sms/send.rb', line 58

def velocity_max_in_frame
  RailsBase.config.twilio.twilio_velocity_max_in_frame
end