Skip to content

Instantly share code, notes, and snippets.

@spastorino
Created December 31, 2012 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spastorino/a8a1f6f58a040951cf7b to your computer and use it in GitHub Desktop.
Save spastorino/a8a1f6f58a040951cf7b to your computer and use it in GitHub Desktop.
diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb
index 2f14875..121a11c 100644
--- a/actionpack/lib/action_dispatch/middleware/cookies.rb
+++ b/actionpack/lib/action_dispatch/middleware/cookies.rb
@@ -87,6 +87,9 @@ module ActionDispatch
ENCRYPTED_SIGNED_COOKIE_SALT = "action_dispatch.encrypted_signed_cookie_salt".freeze
TOKEN_KEY = "action_dispatch.secret_token".freeze
+ # Cookies can typically store 4096 bytes.
+ MAX_COOKIE_SIZE = 4096
+
# Raised when storing more than 4K of session data.
CookieOverflow = Class.new StandardError
@@ -293,13 +296,17 @@ module ActionDispatch
end
end
- class PermanentCookieJar < CookieJar #:nodoc:
+ class PermanentCookieJar #:nodoc:
def initialize(parent_jar, key_generator, options = {})
@parent_jar = parent_jar
@key_generator = key_generator
@options = options
end
+ def [](key)
+ @parent_jar[name.to_s]
+ end
+
def []=(key, options)
if options.is_a?(Hash)
options.symbolize_keys!
@@ -311,14 +318,25 @@ module ActionDispatch
@parent_jar[key] = options
end
+ def permanent
+ @permanent ||= PermanentCookieJar.new(self, @key_generator, @options)
+ end
+
+ def signed
+ @signed ||= SignedCookieJar.new(self, @key_generator, @options)
+ end
+
+ def encrypted
+ @encrypted ||= EncryptedCookieJar.new(self, @key_generator, @options)
+ end
+
def method_missing(method, *arguments, &block)
- @parent_jar.send(method, *arguments, &block)
+ ActiveSupport::Deprecation.warn "#{method} is deprecated with no replacement. " +
+ "You probably want to try this method over the parent CookieJar."
end
end
- class SignedCookieJar < CookieJar #:nodoc:
- MAX_COOKIE_SIZE = 4096 # Cookies can typically store 4096 bytes.
-
+ class SignedCookieJar #:nodoc:
def initialize(parent_jar, key_generator, options = {})
@parent_jar = parent_jar
@options = options
@@ -346,12 +364,25 @@ module ActionDispatch
@parent_jar[key] = options
end
+ def permanent
+ @permanent ||= PermanentCookieJar.new(self, @key_generator, @options)
+ end
+
+ def signed
+ @signed ||= SignedCookieJar.new(self, @key_generator, @options)
+ end
+
+ def encrypted
+ @encrypted ||= EncryptedCookieJar.new(self, @key_generator, @options)
+ end
+
def method_missing(method, *arguments, &block)
- @parent_jar.send(method, *arguments, &block)
+ ActiveSupport::Deprecation.warn "#{method} is deprecated with no replacement. " +
+ "You probably want to try this method over the parent CookieJar."
end
end
- class EncryptedCookieJar < SignedCookieJar #:nodoc:
+ class EncryptedCookieJar #:nodoc:
def initialize(parent_jar, key_generator, options = {})
if ActiveSupport::DummyKeyGenerator === key_generator
raise "Encrypted Cookies must be used in conjunction with config.secret_key_base." +
@@ -365,8 +396,8 @@ module ActionDispatch
@encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret)
end
- def [](name)
- if encrypted_message = @parent_jar[name]
+ def [](key)
+ if encrypted_message = @parent_jar[key]
@encryptor.decrypt_and_verify(encrypted_message)
end
rescue ActiveSupport::MessageVerifier::InvalidSignature,
@@ -385,6 +416,23 @@ module ActionDispatch
raise CookieOverflow if options[:value].size > MAX_COOKIE_SIZE
@parent_jar[key] = options
end
+
+ def permanent
+ @permanent ||= PermanentCookieJar.new(self, @key_generator, @options)
+ end
+
+ def signed
+ @signed ||= SignedCookieJar.new(self, @key_generator, @options)
+ end
+
+ def encrypted
+ @encrypted ||= EncryptedCookieJar.new(self, @key_generator, @options)
+ end
+
+ def method_missing(method, *arguments, &block)
+ ActiveSupport::Deprecation.warn "#{method} is deprecated with no replacement. " +
+ "You probably want to try this method over the parent CookieJar."
+ end
end
def initialize(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment