Class: ActionDispatch::Cookies::SignedCookieJar

Inherits:
CookieJar show all
Defined in:
actionpack/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 Attribute Summary

Attributes inherited from CookieJar

#closed

Instance Method Summary collapse

Methods inherited from CookieJar

build, #close!, #delete, #each, #handle_options, #key?, #permanent, #signed, #update, #write

Methods included from Enumerable

#as_json, #each_with_object, #exclude?, #group_by, #index_by, #many?, #sum

Constructor Details

#initialize(parent_jar, secret) ⇒ SignedCookieJar

Returns a new instance of SignedCookieJar.



272
273
274
275
276
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 272

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



299
300
301
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 299

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

Instance Method Details

#[](name) ⇒ Object



278
279
280
281
282
283
284
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 278

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

#[]=(key, options) ⇒ Object

Raises:



286
287
288
289
290
291
292
293
294
295
296
297
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 286

def []=(key, options)
  raise ClosedError, :cookies if closed?
  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