diff options
Diffstat (limited to 'actionpack/lib/action_dispatch/middleware/cookies.rb')
| -rw-r--r-- | actionpack/lib/action_dispatch/middleware/cookies.rb | 70 |
1 files changed, 59 insertions, 11 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/cookies.rb b/actionpack/lib/action_dispatch/middleware/cookies.rb index 2f148752cb..6ecbb03784 100644 --- a/actionpack/lib/action_dispatch/middleware/cookies.rb +++ b/actionpack/lib/action_dispatch/middleware/cookies.rb @@ -15,7 +15,7 @@ module ActionDispatch # being written will be sent out with the response. Reading a cookie does not get # the cookie object itself back, just the value it holds. # - # Examples for writing: + # Examples of writing: # # # Sets a simple session cookie. # # This cookie will be deleted when the user's browser is closed. @@ -38,7 +38,7 @@ module ActionDispatch # # You can also chain these methods: # cookies.permanent.signed[:login] = "XJ-122" # - # Examples for reading: + # Examples of reading: # # cookies[:user_name] # => "david" # cookies.size # => 2 @@ -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) |
