serializer option for cookie store

This commit is contained in:
Greg Ose
2014-06-16 16:12:33 -05:00
committed by Charlie Somerville
parent 7ed2b48747
commit 1edd8b587b

View File

@@ -106,13 +106,18 @@ module ActionDispatch
secret = request.env[TOKEN_KEY]
host = request.host
secure = request.ssl?
serializer = if session_options = request.env[Session::AbstractStore::ENV_SESSION_OPTIONS_KEY]
session_options[:serializer]
else
nil
end
new(secret, host, secure).tap do |hash|
new(secret, host, secure, serializer).tap do |hash|
hash.update(request.cookies)
end
end
def initialize(secret = nil, host = nil, secure = false)
def initialize(secret = nil, host = nil, secure = false, serializer = nil)
@secret = secret
@set_cookies = {}
@delete_cookies = {}
@@ -120,6 +125,7 @@ module ActionDispatch
@secure = secure
@closed = false
@cookies = {}
@serializer = serializer
end
def each(&block)
@@ -157,6 +163,8 @@ module ActionDispatch
# if host matches one of the supplied domains without a dot in front of it
options[:domain] = options[:domain].find {|domain| @host.include? domain[/^\.?(.*)$/, 1] }
end
@serializer = options[:serializer]
end
# Sets the cookie named +name+. The second argument may be the very cookie
@@ -211,7 +219,7 @@ module ActionDispatch
# cookies.permanent.signed[:remember_me] = current_user.id
# # => Set-Cookie: remember_me=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
def permanent
@permanent ||= PermanentCookieJar.new(self, @secret)
@permanent ||= PermanentCookieJar.new(self, @secret, @serializer)
end
# Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from
@@ -228,7 +236,7 @@ module ActionDispatch
#
# cookies.signed[:discount] # => 45
def signed
@signed ||= SignedCookieJar.new(self, @secret)
@signed ||= SignedCookieJar.new(self, @secret, @serializer)
end
def write(headers)
@@ -252,8 +260,8 @@ module ActionDispatch
end
class PermanentCookieJar < CookieJar #:nodoc:
def initialize(parent_jar, secret)
@parent_jar, @secret = parent_jar, secret
def initialize(parent_jar, secret, serializer)
@parent_jar, @secret, @serializer = parent_jar, secret, serializer
end
def []=(key, options)
@@ -268,7 +276,7 @@ module ActionDispatch
end
def signed
@signed ||= SignedCookieJar.new(self, @secret)
@signed ||= SignedCookieJar.new(self, @secret, @serializer)
end
def method_missing(method, *arguments, &block)
@@ -280,10 +288,10 @@ module ActionDispatch
MAX_COOKIE_SIZE = 4096 # Cookies can typically store 4096 bytes.
SECRET_MIN_LENGTH = 30 # Characters
def initialize(parent_jar, secret)
def initialize(parent_jar, secret, serializer)
ensure_secret_secure(secret)
@parent_jar = parent_jar
@verifier = ActiveSupport::MessageVerifier.new(secret)
@verifier = ActiveSupport::MessageVerifier.new(secret, :serializer => serializer)
end
def [](name)