2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-19 01:00:22 -06:00
|
|
|
# session that is not stored in cookie, expires after 1.hour unconditionally
|
|
|
|
class SecureSession
|
|
|
|
def initialize(prefix)
|
|
|
|
@prefix = prefix
|
|
|
|
end
|
|
|
|
|
2019-10-16 00:53:31 -05: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
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.setex(prefixed_key(key), expires.to_i, val.to_s)
|
2019-10-16 00:53:31 -05:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-11-10 18:18:12 -06:00
|
|
|
def ttl(key)
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.ttl(prefixed_key(key))
|
2019-11-10 18:18:12 -06:00
|
|
|
end
|
|
|
|
|
2016-12-19 01:00:22 -06:00
|
|
|
def [](key)
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.get(prefixed_key(key))
|
2016-12-19 01:00:22 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, val)
|
|
|
|
if val == nil
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.del(prefixed_key(key))
|
2016-12-19 01:00:22 -06:00
|
|
|
else
|
2019-12-03 03:05:53 -06:00
|
|
|
Discourse.redis.setex(prefixed_key(key), SecureSession.expiry.to_i, val.to_s)
|
2016-12-19 01:00:22 -06:00
|
|
|
end
|
2019-10-16 00:53:31 -05:00
|
|
|
val
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def prefixed_key(key)
|
|
|
|
"#{@prefix}#{key}"
|
2016-12-19 01:00:22 -06:00
|
|
|
end
|
|
|
|
end
|