Class: RailsBase::Authentication::UpdatePhoneSendVerification

Inherits:
ServiceBase
  • Object
show all
Defined in:
app/services/rails_base/authentication/update_phone_send_verification.rb

Constant Summary collapse

EXPECTED_LENGTH =
10

Instance Method Summary collapse

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

#callObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/rails_base/authentication/update_phone_send_verification.rb', line 8

def call
  if sanitized_phone_number.nil?
    context.fail!(message: "Unexpected params passed")
  end

  # should be after successful twilio MFA send
  # requires a bit of a restructure that I dont have time for
  update_user_number!

  twilio_sms = RailsBase::Mfa::Sms::Send.call(user: user.reload)

  if twilio_sms.failure?
    log(level: :error, msg: "Failed with #{twilio_sms.message}")
    context.fail!(message: twilio_sms.message)
  end
  context.expires_at = twilio_sms.short_lived_data.death_time
  context.mfa_randomized_token =
    RailsBase::Mfa::EncryptToken.(user: user, expires_at: context.expires_at, purpose: Constants::MSET_PURPOSE).encrypted_val
end

#sanitized_phone_numberObject



33
34
35
36
37
38
39
# File 'app/services/rails_base/authentication/update_phone_send_verification.rb', line 33

def sanitized_phone_number
  @sanitized_phone_number ||= begin
    sanitized = phone_number.tr('^0-9', '')
    log(level: :debug, msg: "Sanitized phone number to: #{sanitized}. Given: #{sanitized.length} Expected? #{EXPECTED_LENGTH} ")
    sanitized.length == EXPECTED_LENGTH ? sanitized : nil
  end
end

#update_user_number!Object



28
29
30
31
# File 'app/services/rails_base/authentication/update_phone_send_verification.rb', line 28

def update_user_number!
  log(level: :info, msg: "Received: #{phone_number}. Sanitized to #{sanitized_phone_number}")
  user.update!(phone_number: sanitized_phone_number)
end

#validate!Object



41
42
43
44
# File 'app/services/rails_base/authentication/update_phone_send_verification.rb', line 41

def validate!
  raise "Expected user to be a User. Received #{user.class}" unless user.is_a? User
  raise "Expected phone_number to be a String. Received #{phone_number.class}" unless phone_number.is_a? String
end