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

Returns a new instance of EncryptedCookieJar.



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

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



34
35
36
# File 'lib/encrypted-cookies/encrypted_cookie_jar.rb', line 34

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

Instance Method Details

#[](name) ⇒ Object



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

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

#[]=(key, options) ⇒ Object

Raises:

  • (CookieOverflow)


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

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

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