Class: ActionDispatch::Cookies::SignedCookieJar

Inherits:
CookieJar
  • Object
show all
Defined in:
lib/action_dispatch/middleware/cookies.rb

Overview

:nodoc:

Constant Summary collapse

4096
SECRET_MIN_LENGTH =

Characters

30

Constants inherited from CookieJar

CookieJar::DOMAIN_REGEXP

Instance Method Summary collapse

Methods inherited from CookieJar

build, #delete, #handle_options, #permanent, #signed, #write

Constructor Details

#initialize(parent_jar, secret) ⇒ SignedCookieJar

Returns a new instance of SignedCookieJar.



245
246
247
248
249
# File 'lib/action_dispatch/middleware/cookies.rb', line 245

def initialize(parent_jar, secret)
  ensure_secret_secure(secret)
  @parent_jar = parent_jar
  @verifier   = ActiveSupport::MessageVerifier.new(secret)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



271
272
273
# File 'lib/action_dispatch/middleware/cookies.rb', line 271

def method_missing(method, *arguments, &block)
  @parent_jar.send(method, *arguments, &block)
end

Instance Method Details

#[](name) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/action_dispatch/middleware/cookies.rb', line 251

def [](name)
  if signed_message = @parent_jar[name]
    @verifier.verify(signed_message)
  end
rescue ActiveSupport::MessageVerifier::InvalidSignature
  nil
end

#[]=(key, options) ⇒ Object

Raises:



259
260
261
262
263
264
265
266
267
268
269
# File 'lib/action_dispatch/middleware/cookies.rb', line 259

def []=(key, options)
  if options.is_a?(Hash)
    options.symbolize_keys!
    options[:value] = @verifier.generate(options[:value])
  else
    options = { :value => @verifier.generate(options) }
  end

  raise CookieOverflow if options[:value].size > MAX_COOKIE_SIZE
  @parent_jar[key] = options
end