2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-19 02:00:22 -05:00
|
|
|
# session that is not stored in cookie, expires after 1.hour unconditionally
|
|
|
|
class SecureSession
|
|
|
|
def initialize(prefix)
|
|
|
|
@prefix = prefix
|
|
|
|
end
|
|
|
|
|
2019-10-16 01:53:31 -04:00
|
|
|
def self.expiry
|
|
|
|
@expiry ||= 1.hour.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.expiry=(val)
|
|
|
|
@expiry = val
|
|
|
|
end
|
|
|
|
|
|
|
|
def set(key, val, expires: nil)
|
|
|
|
expires ||= SecureSession.expiry
|
|
|
|
$redis.setex(prefixed_key(key), SecureSession.expiry.to_i, val.to_s)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2016-12-19 02:00:22 -05:00
|
|
|
def [](key)
|
2019-10-16 01:53:31 -04:00
|
|
|
$redis.get(prefixed_key(key))
|
2016-12-19 02:00:22 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def []=(key, val)
|
2016-12-19 02:00:22 -05:00
|
|
|
if val == nil
|
2019-10-16 01:53:31 -04:00
|
|
|
$redis.del(prefixed_key(key))
|
2016-12-19 02:00:22 -05:00
|
|
|
else
|
2019-10-16 01:53:31 -04:00
|
|
|
$redis.setex(prefixed_key(key), SecureSession.expiry.to_i, val.to_s)
|
2016-12-19 02:00:22 -05:00
|
|
|
end
|
2019-10-16 01:53:31 -04:00
|
|
|
val
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def prefixed_key(key)
|
|
|
|
"#{@prefix}#{key}"
|
2016-12-19 02:00:22 -05:00
|
|
|
end
|
|
|
|
end
|