mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-06 16:31:57 +08:00
This patch will be followed by https://github.com/discourse/discourse/pull/34747. `SecureSession` doesn’t make a lot of sense anymore and can be confusing as the current cookie store used for the session is actually secure since it’s encrypted. Renaming it to `ServerSession` better conveys what it does: providing a session but on the server side only. This patch also makes some improvements, like injecting that server session into Rack-like request objects, allowing the server session to be available virtually everywhere.
47 lines
835 B
Ruby
47 lines
835 B
Ruby
# frozen_string_literal: true
|
|
|
|
# Session that is not stored in cookie, expires after 1.hour unconditionally
|
|
class ServerSession
|
|
delegate :expiry, to: :class
|
|
|
|
class << self
|
|
def expiry
|
|
@expiry ||= 1.hour.to_i
|
|
end
|
|
|
|
def expiry=(val)
|
|
@expiry = val
|
|
end
|
|
end
|
|
|
|
def initialize(prefix)
|
|
@prefix = prefix
|
|
end
|
|
|
|
def set(key, val, expires: expiry)
|
|
Discourse.redis.setex(prefixed_key(key), expires.to_i, val.to_s)
|
|
true
|
|
end
|
|
|
|
def ttl(key)
|
|
Discourse.redis.ttl(prefixed_key(key))
|
|
end
|
|
|
|
def [](key)
|
|
Discourse.redis.get(prefixed_key(key))
|
|
end
|
|
|
|
def []=(key, val)
|
|
if val == nil
|
|
Discourse.redis.del(prefixed_key(key))
|
|
else
|
|
Discourse.redis.setex(prefixed_key(key), expiry.to_i, val.to_s)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def prefixed_key(key)
|
|
"#{@prefix}#{key}"
|
|
end
|
|
end
|