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
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.setex(prefixed_key(key), expires.to_i, val.to_s)
|
2019-10-16 01:53:31 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-11-10 19:18:12 -05:00
|
|
|
def ttl(key)
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.ttl(prefixed_key(key))
|
2019-11-10 19:18:12 -05:00
|
|
|
end
|
|
|
|
|
2016-12-19 02:00:22 -05:00
|
|
|
def [](key)
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.get(prefixed_key(key))
|
2016-12-19 02:00:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, val)
|
|
|
|
if val == nil
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.del(prefixed_key(key))
|
2016-12-19 02:00:22 -05:00
|
|
|
else
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.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
|