discourse/lib/secure_session.rb
Sam Saffron 91daafc674 DEV: correct implementation of expiry api
Previously we were always hard-coding expiry, this allows the secure session
to correctly handle custom expiry times

Also adds a ttl method for looking up time to live
2019-11-11 11:18:12 +11:00

46 lines
790 B
Ruby

# frozen_string_literal: true
# session that is not stored in cookie, expires after 1.hour unconditionally
class SecureSession
def initialize(prefix)
@prefix = prefix
end
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), expires.to_i, val.to_s)
true
end
def ttl(key)
$redis.ttl(prefixed_key(key))
end
def [](key)
$redis.get(prefixed_key(key))
end
def []=(key, val)
if val == nil
$redis.del(prefixed_key(key))
else
$redis.setex(prefixed_key(key), SecureSession.expiry.to_i, val.to_s)
end
val
end
private
def prefixed_key(key)
"#{@prefix}#{key}"
end
end