Module: Istox::Vault::TOTP

Defined in:
lib/istox/helpers/vault.rb

Class Method Summary collapse

Class Method Details

.create(sid, host) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/istox/helpers/vault.rb', line 8

def create(sid, host)
  Rails.logger.debug { "Generate vault TOTP for key #{totp_key(sid).inspect}" }

  write_data(totp_key(sid),
             generate: true,
             issuer: host,
             account_name: sid,
             period: 300,
             qr_size: 300)
end

.generate_code(sid) ⇒ Object



19
20
21
22
# File 'lib/istox/helpers/vault.rb', line 19

def generate_code(sid)
  Rails.logger.debug { "Generate 6-digit OTP code: key #{totp_code_key(sid)}" }
  return read_data(totp_code_key(sid)).data[:code]
end

.validate?(sid, code) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/istox/helpers/vault.rb', line 24

def validate?(sid, code)
  Rails.logger.debug { "Validate TOTP code: key #{totp_code_key(sid)}, code: #{code}" }
  result = write_data(totp_code_key(sid), code: code).data[:valid]

  unless result
    code = read_data(totp_code_key(sid)).data[:code]
    Rails.logger.debug { "Code is not valid, it should be #{code}" }
  end

  result
end