Class: QuoVadis::Totp

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Hmacable
Defined in:
app/models/quo_vadis/totp.rb

Instance Method Summary collapse

Methods included from Hmacable

compute_hmac, timing_safe_eql?

Instance Method Details

#hmac_keyObject



27
28
29
# File 'app/models/quo_vadis/totp.rb', line 27

def hmac_key
  self.class.compute_hmac key
end

#qr_codeObject



20
21
22
23
24
# File 'app/models/quo_vadis/totp.rb', line 20

def qr_code
  RQRCode::QRCode.new(
    ROTP::TOTP.new(key, issuer: QuoVadis.app_name).provisioning_uri(.identifier)
  )
end

#reused?(otp) ⇒ Boolean

Returns:

  • (Boolean)


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

def reused?(otp)
  totp = ROTP::TOTP.new key
  !totp.verify(otp, after: last_used_at) && totp.verify(otp)
end

#verify(otp) ⇒ Object

Returns true and saves the record if ‘otp` is valid, false otherwise.



33
34
35
36
37
38
39
40
# File 'app/models/quo_vadis/totp.rb', line 33

def verify(otp)
  if (_last_used_at = ROTP::TOTP.new(key).verify otp, after: last_used_at)
    self.last_used_at = _last_used_at
    save
  else
    false
  end
end