Module: Devise::Models::OtpAuthenticatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/devise_otp_authenticatable/models/otp_authenticatable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#generate_otp_challenge!(expires = nil) ⇒ Object



64
65
66
67
68
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 64

def generate_otp_challenge!(expires = nil)
  update_columns(:otp_session_challenge => SecureRandom.hex,
                 :otp_challenge_expires => DateTime.now + (expires || self.class.otp_authentication_timeout))
  otp_session_challenge
end

#next_otp_recovery_tokens(number = 5) ⇒ Object



94
95
96
97
98
99
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 94

def next_otp_recovery_tokens(number = 5)
  (otp_recovery_counter..otp_recovery_counter + number).inject({}) do |h, index|
    h[index] = recovery_otp.at(index)
    h
  end
end

#otp_challenge_valid?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 70

def otp_challenge_valid?
  (otp_challenge_expires.nil? || otp_challenge_expires > Time.now)
end

#otp_provisioning_identifierObject



34
35
36
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 34

def otp_provisioning_identifier
  "#{email}/#{self.class.otp_uri_application || Rails.application.class.parent_name}"
end

#otp_provisioning_uriObject



30
31
32
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 30

def otp_provisioning_uri
  time_based_otp.provisioning_uri(otp_provisioning_identifier)
end

#recovery_otpObject



26
27
28
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 26

def recovery_otp
  @recovery_otp ||= ROTP::HOTP.new(otp_recovery_secret)
end

#reset_otp_credentialsObject



39
40
41
42
43
44
45
46
47
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 39

def reset_otp_credentials
  @time_based_otp = nil
  @recovery_otp = nil
  generate_otp_auth_secret
  reset_otp_persistence
  update_columns(:otp_enabled => false, :otp_time_drift => 0,
                    :otp_session_challenge => nil, :otp_challenge_expires => nil,
                    :otp_recovery_counter => 0)
end

#reset_otp_credentials!Object



49
50
51
52
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 49

def reset_otp_credentials!
  reset_otp_credentials
  save!
end

#reset_otp_persistenceObject



55
56
57
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 55

def reset_otp_persistence
  generate_otp_persistence_seed
end

#reset_otp_persistence!Object



59
60
61
62
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 59

def reset_otp_persistence!
  reset_otp_persistence
  save!
end

#time_based_otpObject



22
23
24
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 22

def time_based_otp
  @time_based_otp ||= ROTP::TOTP.new(otp_auth_secret)
end

#validate_otp_recovery_token(token) ⇒ Object Also known as: valid_otp_recovery_token?



101
102
103
104
105
106
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 101

def validate_otp_recovery_token(token)
  recovery_otp.verify(token, otp_recovery_counter).tap do
    self.otp_recovery_counter += 1
    save!
  end
end

#validate_otp_time_token(token) ⇒ Object Also known as: valid_otp_time_token?



84
85
86
87
88
89
90
91
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 84

def validate_otp_time_token(token)
  if drift = validate_otp_token_with_drift(token)
    update_column(:otp_time_drift, drift)
    true
  else
    false
  end
end

#validate_otp_token(token, recovery = false) ⇒ Object Also known as: valid_otp_token?



75
76
77
78
79
80
81
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 75

def validate_otp_token(token, recovery = false)
  if recovery
    validate_otp_recovery_token token
  else
    validate_otp_time_token token
  end
end