Class: ActionDispatch::Cookies::EncryptedCookieJar

Inherits:
Object
  • Object
show all
Includes:
ChainedCookieJars
Defined in:
actionpack/lib/action_dispatch/middleware/cookies.rb

Overview

:nodoc:

Direct Known Subclasses

UpgradeLegacyEncryptedCookieJar

Instance Method Summary collapse

Methods included from ChainedCookieJars

#encrypted, #permanent, #signed, #signed_or_encrypted

Constructor Details

#initialize(parent_jar, key_generator, options = {}) ⇒ EncryptedCookieJar

Returns a new instance of EncryptedCookieJar.



428
429
430
431
432
433
434
435
436
437
438
439
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 428

def initialize(parent_jar, key_generator, options = {})
  if ActiveSupport::LegacyKeyGenerator === key_generator
    raise "You didn't set config.secret_key_base, which is required for this cookie jar. " +
      "Read the upgrade documentation to learn more about this new config option."
  end

  @parent_jar = parent_jar
  @options = options
  secret = key_generator.generate_key(@options[:encrypted_cookie_salt])
  sign_secret = key_generator.generate_key(@options[:encrypted_signed_cookie_salt])
  @encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret)
end

Instance Method Details

#[](name) ⇒ Object



441
442
443
444
445
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 441

def [](name)
  if encrypted_message = @parent_jar[name]
    decrypt_and_verify(encrypted_message)
  end
end

#[]=(name, options) ⇒ Object

Raises:



447
448
449
450
451
452
453
454
455
456
457
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 447

def []=(name, options)
  if options.is_a?(Hash)
    options.symbolize_keys!
  else
    options = { :value => options }
  end
  options[:value] = @encryptor.encrypt_and_sign(options[:value])

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