Class: RoseQuartz::UserAuthenticator

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rose_quartz/user_authenticator.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_backup_code!(token) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/rose_quartz/user_authenticator.rb', line 26

def authenticate_backup_code!(token)
  if token == backup_code
    reset_backup_code!
    true
  else
    false
  end
end

#authenticate_otp!(token) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/rose_quartz/user_authenticator.rb', line 15

def authenticate_otp!(token)
  authenticated_at = totp.verify_with_drift_and_prior(
      token, RoseQuartz.configuration.time_drift, last_authenticated_at)
  if authenticated_at
    update_columns last_authenticated_at: authenticated_at if persisted?
    true
  else
    false
  end
end

#provisioning_uriObject



43
44
45
# File 'lib/rose_quartz/user_authenticator.rb', line 43

def provisioning_uri
  totp.provisioning_uri(user.send(RoseQuartz.configuration.user_identifier))
end

#reset_backup_code!Object



35
36
37
# File 'lib/rose_quartz/user_authenticator.rb', line 35

def reset_backup_code!
  update_columns backup_code: generate_backup_code
end

#set_secret_and_backup_codeObject



10
11
12
13
# File 'lib/rose_quartz/user_authenticator.rb', line 10

def set_secret_and_backup_code
  self.secret ||= ROTP::Base32.random_base32
  self.backup_code ||= generate_backup_code
end

#totpObject



39
40
41
# File 'lib/rose_quartz/user_authenticator.rb', line 39

def totp
  @authenticator ||= ROTP::TOTP.new(secret, issuer: RoseQuartz.configuration.issuer)
end