Class: EncryptedCookies::EncryptedCookieJar

Inherits:
ActionDispatch::Cookies::CookieJar
  • Object
show all
Defined in:
lib/encrypted-cookies/encrypted_cookie_jar.rb

Overview

:nodoc:

Constant Summary collapse

4096
SECRET_MIN_LENGTH =

Characters

30

Instance Method Summary collapse

Constructor Details

#initialize(parent_jar, secret) ⇒ EncryptedCookieJar


8
9
10
11
12
# File 'lib/encrypted-cookies/encrypted_cookie_jar.rb', line 8

def initialize(parent_jar, secret)
  ensure_secret_secure(secret)
  @parent_jar = parent_jar
  @encrypter  = ActiveSupport::MessageEncryptor.new(secret)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


36
37
38
# File 'lib/encrypted-cookies/encrypted_cookie_jar.rb', line 36

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

Instance Method Details

#[](name) ⇒ Object


14
15
16
17
18
19
20
21
22
# File 'lib/encrypted-cookies/encrypted_cookie_jar.rb', line 14

def [](name)
  if encrypted_message = @parent_jar[name]
    @encrypter.decrypt_and_verify(encrypted_message)
  end
rescue ActiveSupport::MessageVerifier::InvalidSignature
  nil
rescue ActiveSupport::MessageEncryptor::InvalidMessage
  nil
end

#[]=(key, options) ⇒ Object

Raises:

  • (CookieOverflow)

24
25
26
27
28
29
30
31
32
33
34
# File 'lib/encrypted-cookies/encrypted_cookie_jar.rb', line 24

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

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