Module: OTP::ActiveRecord
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveSupport::Configurable
- Defined in:
- lib/otp/active_record.rb
Overview
- ActiveRecord
-
concern.
Constant Summary collapse
- OTP_DIGITS =
Length of the generated OTP, defaults to 4.
4
Instance Method Summary collapse
-
#deliver_otp ⇒ ActiveJob::Base
Helper to deliver the OTP.
-
#email_otp ⇒ OTP::API::Mailer
Helper to email the OTP using a job.
-
#otp ⇒ String
Generates the OTP.
-
#sms_otp ⇒ OTP::API::SMSOTPJob
Helper to send the OTP using the SMS job.
-
#verify_otp(otp) ⇒ Object
Verifies the OTP.
Instance Method Details
#deliver_otp ⇒ ActiveJob::Base
Helper to deliver the OTP
Will use the SMS job if the phone number is available. Will default to the email delivery.
70 71 72 73 |
# File 'lib/otp/active_record.rb', line 70 def deliver_otp return unless persisted? sms_otp || email_otp || raise(NotImplementedError, self) end |
#email_otp ⇒ OTP::API::Mailer
Helper to email the OTP using a job
Does nothing. Implement your own handler.
61 62 |
# File 'lib/otp/active_record.rb', line 61 def email_otp end |
#otp ⇒ String
Generates the OTP
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/otp/active_record.rb', line 21 def otp return nil if !valid? || !persisted? || otp_secret.blank? otp_digits = self.class.const_get(:OTP_DIGITS) hotp = ROTP::HOTP.new(otp_secret, digits: otp_digits) transaction do increment!(:otp_counter) hotp.at(otp_counter) end end |
#sms_otp ⇒ OTP::API::SMSOTPJob
Helper to send the OTP using the SMS job
Does nothing. Implement your own handler.
53 54 |
# File 'lib/otp/active_record.rb', line 53 def sms_otp end |
#verify_otp(otp) ⇒ Object
Verifies the OTP
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/otp/active_record.rb', line 36 def verify_otp(otp) return nil if !valid? || !persisted? || otp_secret.blank? otp_digits = self.class.const_get(:OTP_DIGITS) hotp = ROTP::HOTP.new(otp_secret, digits: otp_digits) transaction do otp_status = hotp.verify(otp.to_s, otp_counter) increment!(:otp_counter) otp_status end end |