Module: RailsMFA::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/rails_mfa/model.rb
Instance Method Summary collapse
- #generate_totp_secret! ⇒ Object
- #send_numeric_code(via: :sms) ⇒ Object
- #totp_provisioning_uri(issuer: "RailsMFA") ⇒ Object
- #verify_numeric_code(code) ⇒ Object
- #verify_totp(code) ⇒ Object
Instance Method Details
#generate_totp_secret! ⇒ Object
18 19 20 21 22 23 |
# File 'lib/rails_mfa/model.rb', line 18 def generate_totp_secret! secret = ROTP::Base32.random_base32 # host app should store secret encrypted in a column like :mfa_secret update!(mfa_secret: secret) if respond_to?(:update!) secret end |
#send_numeric_code(via: :sms) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rails_mfa/model.rb', line 38 def send_numeric_code(via: :sms) tm = TokenManager.new code = tm.generate_numeric_code(id) case via.to_sym when :sms raise "sms_provider not configured" unless RailsMFA.configuration.sms_provider RailsMFA.configuration.sms_provider.call(phone_number_for_sms, "Your verification code is: #{code}") when :email raise "email_provider not configured" unless RailsMFA.configuration.email_provider RailsMFA.configuration.email_provider.call(email, "Your verification code", "Code: #{code}") else raise "Unsupported channel" end code end |
#totp_provisioning_uri(issuer: "RailsMFA") ⇒ Object
25 26 27 28 29 |
# File 'lib/rails_mfa/model.rb', line 25 def totp_provisioning_uri(issuer: "RailsMFA") raise "No mfa_secret present" unless respond_to?(:mfa_secret) && mfa_secret ROTP::TOTP.new(mfa_secret, issuer: issuer).provisioning_uri(respond_to?(:email) ? email : "user") end |
#verify_numeric_code(code) ⇒ Object
56 57 58 59 |
# File 'lib/rails_mfa/model.rb', line 56 def verify_numeric_code(code) tm = TokenManager.new tm.verify_numeric_code(id, code) end |
#verify_totp(code) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/rails_mfa/model.rb', line 31 def verify_totp(code) return false unless respond_to?(:mfa_secret) && mfa_secret totp = ROTP::TOTP.new(mfa_secret) totp.verify(code, drift_behind: 30) end |