Class: ActionDispatch::Cookies::EncryptedCookieJar

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

Overview

:nodoc:

Direct Known Subclasses

UpgradeLegacyEncryptedCookieJar

Constant Summary

Constants included from SerializedCookieJars

SerializedCookieJars::MARSHAL_SIGNATURE

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.



501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/action_dispatch/middleware/cookies.rb', line 501

def initialize(parent_jar, key_generator, options = {})
  if ActiveSupport::LegacyKeyGenerator === key_generator
    raise "You didn't set secrets.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])[0, ActiveSupport::MessageEncryptor.key_len]
  sign_secret = key_generator.generate_key(@options[:encrypted_signed_cookie_salt])
  @encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret, digest: digest, serializer: ActiveSupport::MessageEncryptor::NullSerializer)
end

Instance Method Details

#[](name) ⇒ Object



514
515
516
517
518
# File 'lib/action_dispatch/middleware/cookies.rb', line 514

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

#[]=(name, options) ⇒ Object

Raises:



520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/action_dispatch/middleware/cookies.rb', line 520

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

  options[:value] = @encryptor.encrypt_and_sign(serialize(name, options[:value]))

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