Module: Devise::Models::TwoFactorAuthenticatable
- Extended by:
- ActiveSupport::Concern
- Includes:
- DatabaseAuthenticatable
- Defined in:
- lib/devise_two_factor/models/two_factor_authenticatable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #clean_up_passwords ⇒ Object
- #current_otp ⇒ Object
-
#legacy_otp_secret ⇒ Object
Decrypt and return the ‘encrypted_otp_secret` attribute which was used in prior versions of devise-two-factor See: # github.com/tinfoil/devise-two-factor/blob/main/UPGRADING.md.
- #otp(otp_secret = self.otp_secret) ⇒ Object
- #otp_provisioning_uri(account, options = {}) ⇒ Object
- #otp_secret ⇒ Object
-
#validate_and_consume_otp!(code, options = {}) ⇒ Object
This defaults to the model’s otp_secret If this hasn’t been generated yet, pass a secret as an option.
Class Method Details
.required_fields(klass) ⇒ Object
30 31 32 |
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 30 def self.required_fields(klass) [:otp_secret, :consumed_timestep] end |
Instance Method Details
#clean_up_passwords ⇒ Object
66 67 68 69 |
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 66 def clean_up_passwords super self.otp_attempt = nil end |
#current_otp ⇒ Object
57 58 59 |
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 57 def current_otp otp.at(Time.now) end |
#legacy_otp_secret ⇒ Object
Decrypt and return the ‘encrypted_otp_secret` attribute which was used in prior versions of devise-two-factor See: # github.com/tinfoil/devise-two-factor/blob/main/UPGRADING.md
26 27 28 |
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 26 def legacy_otp_secret nil end |
#otp(otp_secret = self.otp_secret) ⇒ Object
53 54 55 |
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 53 def otp(otp_secret = self.otp_secret) ROTP::TOTP.new(otp_secret) end |
#otp_provisioning_uri(account, options = {}) ⇒ Object
61 62 63 64 |
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 61 def otp_provisioning_uri(account, = {}) otp_secret = [:otp_secret] || self.otp_secret ROTP::TOTP.new(otp_secret, ).provisioning_uri(account) end |
#otp_secret ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 14 def otp_secret # return the OTP secret stored as a Rails encrypted attribute if it # exists. Otherwise return OTP secret stored by the `attr_encrypted` gem return self[:otp_secret] if self[:otp_secret] legacy_otp_secret end |
#validate_and_consume_otp!(code, options = {}) ⇒ Object
This defaults to the model’s otp_secret If this hasn’t been generated yet, pass a secret as an option
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 36 def validate_and_consume_otp!(code, = {}) otp_secret = [:otp_secret] || self.otp_secret return false unless code.present? && otp_secret.present? totp = otp(otp_secret) if self.consumed_timestep # reconstruct the timestamp of the last consumed timestep = self.consumed_timestep * totp.interval end = totp.verify(code.gsub(/\s+/, ""), drift_behind: self.class.otp_allowed_drift, drift_ahead: self.class.otp_allowed_drift, after: ) return consume_otp!(totp, ) if false end |