Class: RailsBase::Authentication::SendVerificationEmail

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

Constant Summary collapse

DATA_USE =
:alphanumeric
VELOCITY_MAX =
5
VELOCITY_MAX_IN_FRAME =
10.minutes
VELOCITY_FRAME =
1.hour
REASON_MAPPER =
{
  Constants:: => {
    method: :email_verification,
    url_method: :email_verification_url,
    max_use: RailsBase.config..email_max_use_verification
  },
  Constants::SVE_FORGOT_REASON => {
    method: :forgot_password,
    url_method: :forgot_password_auth_url,
    max_use: RailsBase.config..email_max_use_forgot
  }
}

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

#assign_url(data) ⇒ Object



60
61
62
63
64
65
66
67
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 60

def assign_url(data)
  params = {
    data: data,
    host: Constants::BASE_URL,
  }
  params[:port] = Constants::BASE_URL_PORT if Constants::BASE_URL_PORT
  Constants::URL_HELPER.public_send(url_method, params)
end

#cache_keyObject



93
94
95
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 93

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

#callObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 29

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

  data_point = create_short_lived_data
  begin
    url = assign_url(data_point.data)
  rescue StandardError => e
    log(level: :error, msg: "Error: #{e.class}")
    log(level: :error, msg: "Error: #{e.message}")
    log(level: :error, msg: "Failed to get url for #{url_method}")
    log(level: :error, msg: "Swallowing Error. Returning to user")
    context.fail!(message: "Unknown error occurred. Please log in with credentials to restart process")
    return
  end
  log(level: :info, msg: "SSO url for user #{user.id}: #{url}")

  begin
    RailsBase::EmailVerificationMailer.public_send(method, user: user, url: url).deliver_me
  rescue StandardError => e
    log(level: :error, msg: "Unkown error occured when sending EmailVerificationMailer.#{method}")
    log(level: :error, msg: "Params: #{method}, #{reason}, user: #{user.id}")
    log(level: :error, msg: e.message)
    context.fail!(message: "Unknown error occurred. Please log in with credentials to restart process")
    return
  end
  log(level: :info, msg: "Succesfully sent EmailVerificationMailer.#{method} to #{user.id} @ #{user.email}")
end

#create_short_lived_dataObject



69
70
71
72
73
74
75
76
77
78
79
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 69

def create_short_lived_data
  params = {
    user: user,
    max_use: REASON_MAPPER[reason][:max_use],
    reason: reason,
    data_use: DATA_USE,
    ttl: Constants::SVE_TTL,
    length: Constants::EMAIL_LENGTH,
  }
  ShortLivedData.create_data_key(**params)
end

#methodObject



97
98
99
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 97

def method
  REASON_MAPPER[reason][:method]
end

#url_methodObject



101
102
103
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 101

def url_method
  REASON_MAPPER[reason][:url_method]
end

#validate!Object



105
106
107
108
109
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 105

def validate!
  raise "Expected user to be a User. Received #{user.class}" unless user.is_a? User
  raise "Expected reason to be a symbol. Received #{reason.class}" unless reason.is_a? Symbol
  raise "Expected #{reason} to be in #{REASON_MAPPER.keys}" unless REASON_MAPPER.keys.include?(reason)
end

#velocity_frameObject



89
90
91
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 89

def velocity_frame
  VELOCITY_FRAME
end

#velocity_maxObject



85
86
87
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 85

def velocity_max
  VELOCITY_MAX
end

#velocity_max_in_frameObject



81
82
83
# File 'app/services/rails_base/authentication/send_verification_email.rb', line 81

def velocity_max_in_frame
  VELOCITY_MAX_IN_FRAME
end