Class: Redmine::Twofa::Totp

Inherits:
Base
  • Object
show all
Defined in:
lib/redmine/twofa/totp.rb

Instance Method Summary collapse

Methods inherited from Base

#backup_codes, #confirm_pairing!, #deliver_twofa_paired, #deliver_twofa_unpaired, #destroy_pairing!, inherited, #init_backup_codes!, #initialize, #otp_confirm_view_variables, scheme_name, #scheme_name, #send_code, #verify!, #verify_backup_code!

Constructor Details

This class inherits a constructor from Redmine::Twofa::Base

Instance Method Details

#destroy_pairing_without_verify!Object



32
33
34
35
36
37
# File 'lib/redmine/twofa/totp.rb', line 32

def destroy_pairing_without_verify!
  @user.update!(twofa_totp_key: nil, twofa_totp_last_used_at: nil)
  # reset the cached totp as the key might have changed
  @totp = nil
  super
end

#init_pairing!Object



25
26
27
28
29
30
# File 'lib/redmine/twofa/totp.rb', line 25

def init_pairing!
  @user.update!(twofa_totp_key: ROTP::Base32.random)
  # reset the cached totp as the key might have changed
  @totp = nil
  super
end

#init_pairing_view_variablesObject



56
57
58
59
60
61
62
63
# File 'lib/redmine/twofa/totp.rb', line 56

def init_pairing_view_variables
  super.merge(
    {
      provisioning_uri: provisioning_uri,
      totp_key: @user.twofa_totp_key
    }
  )
end

#provisioning_uriObject



52
53
54
# File 'lib/redmine/twofa/totp.rb', line 52

def provisioning_uri
  totp.provisioning_uri(@user.)
end

#verify_otp!(code) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/redmine/twofa/totp.rb', line 39

def verify_otp!(code)
  # topt codes are white-space-insensitive
  code = code.to_s.remove(/[[:space:]]/)
  last_verified_at = @user.twofa_totp_last_used_at
  verified_at = totp.verify(code.to_s, drift_behind: allowed_drift, after: last_verified_at)
  if verified_at
    @user.update!(twofa_totp_last_used_at: verified_at)
    true
  else
    false
  end
end