Class: QuoVadis::Account

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/quo_vadis/account.rb

Constant Summary collapse

MAX_NUMBER_OF_RECOVERY_CODES =
5

Instance Method Summary collapse

Instance Method Details

#confirm(otp, counter) ⇒ Object

If the ‘otp` is valid for the `counter`, confirms the account and returns truthy. Otherwise returns falsey.



31
32
33
# File 'app/models/quo_vadis/account.rb', line 31

def confirm(otp, counter)
  hotp_for_confirmation.verify(otp, counter) && confirmed!
end

#confirmed!Object



47
48
49
# File 'app/models/quo_vadis/account.rb', line 47

def confirmed!
  touch :confirmed_at
end

#confirmed?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/quo_vadis/account.rb', line 43

def confirmed?
  confirmed_at.present?
end

#generate_recovery_codesObject

Returns an array of the recovery codes’ codes.



56
57
58
59
# File 'app/models/quo_vadis/account.rb', line 56

def generate_recovery_codes
  recovery_codes.delete_all
  Array.new(MAX_NUMBER_OF_RECOVERY_CODES) { recovery_codes.create }.map &:code
end

#has_two_factors?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/models/quo_vadis/account.rb', line 51

def has_two_factors?
  password.present? && totp.present?
end

#otp_for_confirmation(counter) ⇒ Object



25
26
27
# File 'app/models/quo_vadis/account.rb', line 25

def otp_for_confirmation(counter)
  hotp_for_confirmation.at(counter)
end

#otp_for_password_reset(counter) ⇒ Object



35
36
37
# File 'app/models/quo_vadis/account.rb', line 35

def otp_for_password_reset(counter)
  hotp_for_password_reset.at(counter)
end

#revokeObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/quo_vadis/account.rb', line 61

def revoke
  password&.destroy
  totp&.destroy
  recovery_codes.destroy_all
  sessions.destroy_all

  Log.create(
    account: self,
    action: Log::REVOKE,
    ip: (CurrentRequestDetails.ip || '')
  )
end

#verify_password_reset(otp, counter) ⇒ Object



39
40
41
# File 'app/models/quo_vadis/account.rb', line 39

def verify_password_reset(otp, counter)
  hotp_for_password_reset.verify(otp, counter)
end