module Devise::Models::TwoFactorAuthenticatable
Public Class Methods
required_fields(klass)
click to toggle source
# File lib/devise_two_factor/models/two_factor_authenticatable.rb, line 24 def self.required_fields(klass) [:encrypted_otp_secret, :encrypted_otp_secret_iv, :encrypted_otp_secret_salt, :consumed_timestep] end
Public Instance Methods
clean_up_passwords()
click to toggle source
# File lib/devise_two_factor/models/two_factor_authenticatable.rb, line 58 def clean_up_passwords self.otp_attempt = nil end
current_otp()
click to toggle source
# File lib/devise_two_factor/models/two_factor_authenticatable.rb, line 44 def current_otp otp.at(Time.now) end
current_otp_timestep()
click to toggle source
ROTP's TOTP#timecode is private, so we duplicate it here
# File lib/devise_two_factor/models/two_factor_authenticatable.rb, line 49 def current_otp_timestep Time.now.utc.to_i / otp.interval end
otp(otp_secret = self.otp_secret)
click to toggle source
# File lib/devise_two_factor/models/two_factor_authenticatable.rb, line 40 def otp(otp_secret = self.otp_secret) ROTP::TOTP.new(otp_secret) end
otp_provisioning_uri(account, options = {})
click to toggle source
# File lib/devise_two_factor/models/two_factor_authenticatable.rb, line 53 def otp_provisioning_uri(account, options = {}) otp_secret = options[:otp_secret] || self.otp_secret ROTP::TOTP.new(otp_secret, options).provisioning_uri(account) end
validate_and_consume_otp!(code, options = {})
click to toggle source
This defaults to the model's otp_secret If this hasn't been generated yet, pass a secret as an option
# File lib/devise_two_factor/models/two_factor_authenticatable.rb, line 30 def validate_and_consume_otp!(code, options = {}) otp_secret = options[:otp_secret] || self.otp_secret return false unless code.present? && otp_secret.present? totp = self.otp(otp_secret) return consume_otp! if totp.verify_with_drift(code, self.class.otp_allowed_drift) false end
Protected Instance Methods
consume_otp!()
click to toggle source
An OTP cannot be used more than once in a given timestep Storing timestep of last valid OTP is sufficient to satisfy this requirement
# File lib/devise_two_factor/models/two_factor_authenticatable.rb, line 66 def consume_otp! if self.consumed_timestep != current_otp_timestep self.consumed_timestep = current_otp_timestep return save(validate: false) end false end