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
- #serializable_hash(options = nil) ⇒ Object
Instance Method Details
#authenticate_otp(code, options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/active_model/one_time_password.rb', line 46 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 respond_to?(:new_record) && !new_record? end result else totp = ROTP::TOTP.new(otp_column, digits: otp_digits) if drift = [:drift] totp.verify(code, drift_behind: drift) else totp.verify(code) end end end |
#otp_code(options = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_model/one_time_password.rb', line 65 def otp_code( = {}) if otp_counter_based if [:auto_increment] self.otp_counter += 1 save if respond_to?(:new_record) && !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) else time = end ROTP::TOTP.new(otp_column, digits: otp_digits).at(time) end end |
#otp_column ⇒ Object
93 94 95 |
# File 'lib/active_model/one_time_password.rb', line 93 def otp_column self.public_send(self.class.otp_column_name) end |
#otp_column=(attr) ⇒ Object
97 98 99 |
# File 'lib/active_model/one_time_password.rb', line 97 def otp_column=(attr) self.public_send("#{self.class.otp_column_name}=", attr) end |
#otp_counter ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/active_model/one_time_password.rb', line 101 def otp_counter if self.class.otp_counter_column_name != "otp_counter" self.public_send(self.class.otp_counter_column_name) else super end end |
#otp_counter=(attr) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/active_model/one_time_password.rb', line 109 def otp_counter=(attr) if self.class.otp_counter_column_name != "otp_counter" self.public_send("#{self.class.otp_counter_column_name}=", attr) else super end end |
#otp_regenerate_counter ⇒ Object
42 43 44 |
# File 'lib/active_model/one_time_password.rb', line 42 def otp_regenerate_counter self.otp_counter = 1 end |
#otp_regenerate_secret ⇒ Object
38 39 40 |
# File 'lib/active_model/one_time_password.rb', line 38 def otp_regenerate_secret self.otp_column = self.class.otp_random_secret end |
#provisioning_uri(account = nil, options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/active_model/one_time_password.rb', line 82 def provisioning_uri(account = nil, = {}) account ||= self.email if self.respond_to?(:email) account ||= "" if otp_counter_based ROTP::HOTP.new(otp_column, ).provisioning_uri(account) else ROTP::TOTP.new(otp_column, ).provisioning_uri(account) end end |
#serializable_hash(options = nil) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/active_model/one_time_password.rb', line 117 def serializable_hash( = nil) ||= {} [:except] = Array([:except]) [:except] << self.class.otp_column_name super() end |