Module: Istox::Vault

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

Class Method Summary collapse

Class Method Details

.get_otp(sid, _host, expired_seconds: 600) ⇒ Object

if File.exist?(file_location)

    token = File.read(file_location).strip
    config.token = token
    config.ssl_verify = false
    config.timeout = 60
  else
    log.info 'Vault token not found, OTP will not be able to use'
  end
end

end



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/istox/helpers/vault.rb', line 76

def get_otp(sid, _host, expired_seconds: 600)
  # ::Istox::Vault::TOTP.create(sid, host)
  # Istox::Vault::TOTP.generate_code(sid)

  otp = 6.times.map { rand(10) }.join

  otp_redis.set(sid, otp, nx: false, ex: expired_seconds.seconds)
  otp_redis.del(sid + '_tries')

  otp
end

.otp_redisObject



114
115
116
# File 'lib/istox/helpers/vault.rb', line 114

def otp_redis
  @otp_redis ||= ::Istox::RedisManager.otp_redis
end

.validate_otp(sid, otp) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/istox/helpers/vault.rb', line 88

def validate_otp(sid, otp)
  # ::Istox::Vault::TOTP.validate?(sid, otp)

  stored_otp = otp_redis.get(sid)

  return false if stored_otp.blank?
  return false if otp.blank?

  result = otp.to_s == stored_otp.to_s

  otp_redis.del(sid) if result == true

  if result == false
    current_tries = (otp_redis.get(sid + '_tries') || 0).to_s.to_i

    if current_tries > 4
      otp_redis.del(sid)
      otp_redis.del(sid + '_tries')
    else
      otp_redis.set(sid + '_tries', current_tries + 1)
    end
  end

  result
end