Module: ActiveModel::OneTimePassword::InstanceMethodsOnActivation
- Defined in:
- lib/active_model/one_time_password.rb
Instance Method Summary collapse
- #authenticate_otp(code, options = {}) ⇒ Object
- #otp_code(options = {}) ⇒ Object
- #otp_column ⇒ Object
- #otp_column=(attr) ⇒ Object
- #otp_counter ⇒ Object
- #otp_counter=(attr) ⇒ Object
- #otp_regenerate_counter ⇒ Object
- #otp_regenerate_secret ⇒ Object
- #provisioning_uri(account = nil, options = {}) ⇒ Object
Instance Method Details
#authenticate_otp(code, options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_model/one_time_password.rb', line 43 def authenticate_otp(code, = {}) if otp_counter_based hotp = ROTP::HOTP.new(otp_column, digits: otp_digits) result = hotp.verify(code, otp_counter) if result && [:auto_increment] self.otp_counter += 1 save if !new_record? end result else totp = ROTP::TOTP.new(otp_column, digits: otp_digits) if drift = [:drift] totp.verify_with_drift(code, drift) else totp.verify(code) end end end |
#otp_code(options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/active_model/one_time_password.rb', line 62 def otp_code( = {}) if otp_counter_based if [:auto_increment] self.otp_counter += 1 save if !new_record? end ROTP::HOTP.new(otp_column, digits: otp_digits).at(self.otp_counter) else if .is_a? Hash time = .fetch(:time, Time.now) padding = .fetch(:padding, true) else time = padding = true end ROTP::TOTP.new(otp_column, digits: otp_digits).at(time, padding) end end |
#otp_column ⇒ Object
91 92 93 |
# File 'lib/active_model/one_time_password.rb', line 91 def otp_column self.send(self.class.otp_column_name) end |
#otp_column=(attr) ⇒ Object
95 96 97 |
# File 'lib/active_model/one_time_password.rb', line 95 def otp_column=(attr) self.send("#{self.class.otp_column_name}=", attr) end |
#otp_counter ⇒ Object
99 100 101 |
# File 'lib/active_model/one_time_password.rb', line 99 def otp_counter self.send(self.class.otp_counter_column_name) end |
#otp_counter=(attr) ⇒ Object
103 104 105 |
# File 'lib/active_model/one_time_password.rb', line 103 def otp_counter=(attr) self.send("#{self.class.otp_counter_column_name}=", attr) end |
#otp_regenerate_counter ⇒ Object
39 40 41 |
# File 'lib/active_model/one_time_password.rb', line 39 def otp_regenerate_counter self.otp_counter = 1 end |
#otp_regenerate_secret ⇒ Object
35 36 37 |
# File 'lib/active_model/one_time_password.rb', line 35 def otp_regenerate_secret self.otp_column = ROTP::Base32.random_base32 end |
#provisioning_uri(account = nil, options = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/active_model/one_time_password.rb', line 81 def provisioning_uri(account = nil, = {}) account ||= self.email if self.respond_to?(:email) if otp_counter_based ROTP::HOTP.new(otp_column, ).provisioning_uri(account) else ROTP::TOTP.new(otp_column, ).provisioning_uri(account) end end |