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

#clear_otp_fields!Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 58

def clear_otp_fields!
  @time_based_otp = nil
  @recovery_otp = nil

  self.update!(
    otp_auth_secret: nil,
    otp_recovery_secret: nil,
    otp_persistence_seed: nil,
    otp_session_challenge: nil,
    otp_challenge_expires: nil,
    otp_failed_attempts: 0,
    otp_recovery_counter: 0
  )
end

#disable_otp!Object



77
78
79
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 77

def disable_otp!
  update!(otp_enabled: false, otp_enabled_on: nil)
end

#enable_otp!Object



73
74
75
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 73

def enable_otp!
  update!(otp_enabled: true, otp_enabled_on: Time.now)
end

#generate_otp_challenge!(expires = nil) ⇒ Object



81
82
83
84
85
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 81

def generate_otp_challenge!(expires = nil)
  update!(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 = self.class.otp_recovery_tokens) ⇒ Object



106
107
108
109
110
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 106

def next_otp_recovery_tokens(number = self.class.otp_recovery_tokens)
  (otp_recovery_counter..otp_recovery_counter + number).each_with_object({}) do |index, h|
    h[index] = recovery_otp.at(index)
  end
end

#otp_challenge_valid?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 87

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

#otp_issuerObject



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

def otp_issuer
  (self.class.otp_issuer || Rails.application.class.module_parent_name).to_s
end

#otp_provisioning_identifierObject



37
38
39
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 37

def otp_provisioning_identifier
  email
end

#otp_provisioning_uriObject



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

def otp_provisioning_uri
  time_based_otp.provisioning_uri(otp_provisioning_identifier)
end

#populate_otp_secrets!Object



50
51
52
53
54
55
56
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 50

def populate_otp_secrets!
  if [otp_auth_secret, otp_recovery_secret, otp_persistence_seed].any? { |a| a.blank? }
    generate_otp_auth_secret
    generate_otp_persistence_seed
    self.save!
  end
end

#recovery_otpObject



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

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

#reset_otp_persistenceObject



41
42
43
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 41

def reset_otp_persistence
  generate_otp_persistence_seed
end

#reset_otp_persistence!Object



45
46
47
48
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 45

def reset_otp_persistence!
  reset_otp_persistence
  save!
end

#time_based_otpObject



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

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

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



112
113
114
115
116
117
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 112

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?



100
101
102
103
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 100

def validate_otp_time_token(token)
  return false if token.blank?
  validate_otp_token_with_drift(token)
end

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



91
92
93
94
95
96
97
# File 'lib/devise_otp_authenticatable/models/otp_authenticatable.rb', line 91

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