Module: RailsBase::UserHelper::Totp::ConsumeMethodOptions

Defined in:
app/models/rails_base/user_helper/totp/consume_method_options.rb

Instance Method Summary collapse

Instance Method Details

#otp_metadata(safe: false, use_existing_temp: false) ⇒ Object



26
27
28
29
30
31
32
33
# File 'app/models/rails_base/user_helper/totp/consume_method_options.rb', line 26

def (safe: false, use_existing_temp: false)
  secret ||= self.otp_secret
  secret ||= self.temp_otp_secret if safe && use_existing_temp
  secret ||= temporary_otp! if safe

  uri = otp_provisioning_uri({ otp_secret: secret })
  { secret: secret, uri: uri, qr_code: qr_code(uri) }
end

#otp_provisioning_uri(options = {}) ⇒ Object



19
20
21
22
23
24
# File 'app/models/rails_base/user_helper/totp/consume_method_options.rb', line 19

def otp_provisioning_uri(options = {})
  label = options.delete(:label) || "#{RailsBase.app_name}:#{self.email}"
  otp_secret = options[:otp_secret] || self.otp_secret

  ROTP::TOTP.new(otp_secret, options).provisioning_uri(label)
end

#persist_otp_metadata!Object



9
10
11
12
13
14
15
16
17
# File 'app/models/rails_base/user_helper/totp/consume_method_options.rb', line 9

def persist_otp_metadata!
  reload # Ensure the user is relaoded to use the correct data for the secret
   = (safe: true, use_existing_temp: true)

  self.otp_secret = self.otp_secret || self.temp_otp_secret
  self.temp_otp_secret = nil
  self.mfa_otp_enabled = true
  save!
end