Module: ActiveModel::OneTimePassword::InstanceMethodsOnActivation
- Defined in:
- lib/active_model/one_time_password.rb
Instance Method Summary collapse
- #authenticate_otp(code, options = {}) ⇒ Object
- #backup_codes_enabled? ⇒ Boolean
- #otp_code(options = {}) ⇒ Object
- #otp_column ⇒ Object
- #otp_column=(attr) ⇒ Object
- #otp_counter ⇒ Object
- #otp_counter=(attr) ⇒ Object
- #otp_regenerate_backup_codes ⇒ 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
57 58 59 60 61 62 63 64 65 |
# File 'lib/active_model/one_time_password.rb', line 57 def authenticate_otp(code, = {}) return true if backup_codes_enabled? && authenticate_backup_code(code) if otp_counter_based otp_counter == authenticate_hotp(code, ) else authenticate_totp(code, ).present? end end |
#backup_codes_enabled? ⇒ Boolean
130 131 132 |
# File 'lib/active_model/one_time_password.rb', line 130 def backup_codes_enabled? self.class.attribute_method?(self.class.otp_backup_codes_column_name) end |
#otp_code(options = {}) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/active_model/one_time_password.rb', line 67 def otp_code( = {}) if otp_counter_based hotp_code() else totp_code() end end |
#otp_column ⇒ Object
90 91 92 |
# File 'lib/active_model/one_time_password.rb', line 90 def otp_column self.public_send(self.class.otp_column_name) end |
#otp_column=(attr) ⇒ Object
94 95 96 |
# File 'lib/active_model/one_time_password.rb', line 94 def otp_column=(attr) self.public_send("#{self.class.otp_column_name}=", attr) end |
#otp_counter ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/active_model/one_time_password.rb', line 98 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
106 107 108 109 110 111 112 |
# File 'lib/active_model/one_time_password.rb', line 106 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_backup_codes ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/active_model/one_time_password.rb', line 121 def otp_regenerate_backup_codes otp = ROTP::OTP.new(otp_column) backup_codes = Array.new(self.class.otp_backup_codes_count) do otp.generate_otp((SecureRandom.random_number(9e5) + 1e5).to_i) end public_send("#{self.class.otp_backup_codes_column_name}=", backup_codes) end |
#otp_regenerate_counter ⇒ Object
53 54 55 |
# File 'lib/active_model/one_time_password.rb', line 53 def otp_regenerate_counter self.otp_counter = 1 end |
#otp_regenerate_secret ⇒ Object
49 50 51 |
# File 'lib/active_model/one_time_password.rb', line 49 def otp_regenerate_secret self.otp_column = self.class.otp_random_secret end |
#provisioning_uri(account = nil, options = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/active_model/one_time_password.rb', line 75 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, self.otp_counter) else ROTP::TOTP .new(otp_column, ) .provisioning_uri(account) end end |
#serializable_hash(options = nil) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/active_model/one_time_password.rb', line 114 def serializable_hash( = nil) ||= {} [:except] = Array([:except]) [:except] << self.class.otp_column_name super() end |