Class: Merb::CookieSession

Inherits:
Object show all
Defined in:
lib/merb-core/dispatch/session/cookie.rb

Overview

If you have more than 4K of session data or don’t want your data to be visible to the user, pick another session store.

CookieOverflow is raised if you attempt to store more than 4K of data. TamperedWithCookie is raised if the data integrity check fails.

A message digest is included with the cookie to ensure data integrity: a user cannot alter session data without knowing the secret key included in the hash.

To use Cookie Sessions, set in config/merb.yml

:session_secret_key - your secret digest key
:session_store: cookie

Defined Under Namespace

Classes: CookieOverflow, TamperedWithCookie

Constant Summary collapse

MAX =

Cookies can typically store 4096 bytes.

4096
DIGEST =

or MD5, RIPEMD160, SHA256?

OpenSSL::Digest::Digest.new('SHA1')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookie, secret) ⇒ CookieSession

Parameters

cookie<String>

The cookie.

secret<String>

A session secret.

Raises

ArgumentError

Nil or blank secret.



72
73
74
75
76
77
78
# File 'lib/merb-core/dispatch/session/cookie.rb', line 72

def initialize(cookie, secret)
  if secret.nil? or secret.blank?
    raise ArgumentError, 'A secret is required to generate an integrity hash for cookie session data.'
  end
  @secret = secret
  @data = unmarshal(cookie) || Hash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Attempts to redirect any messages to the data object.



125
126
127
# File 'lib/merb-core/dispatch/session/cookie.rb', line 125

def method_missing(name, *args, &block)
  @data.send(name, *args, &block)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



64
65
66
# File 'lib/merb-core/dispatch/session/cookie.rb', line 64

def data
  @data
end

Instance Method Details

#[](k) ⇒ Object

Parameters

k<~to_s>

The key of the session parameter to retrieve.

Returns

String

The value of the session parameter.



105
106
107
# File 'lib/merb-core/dispatch/session/cookie.rb', line 105

def [](k) 
  @data[k] 
end

#[]=(k, v) ⇒ Object

Parameters

k<~to_s>

The key of the session parameter to set.

v<~to_s>

The value of the session parameter to set.



96
97
98
# File 'lib/merb-core/dispatch/session/cookie.rb', line 96

def []=(k, v) 
  @data[k] = v
end

#deleteObject

Deletes the session by emptying stored data.



118
119
120
# File 'lib/merb-core/dispatch/session/cookie.rb', line 118

def delete  
  @data = {} 
end

#each(&b) ⇒ Object

Yields the session data to an each block.

Parameter

&b

The block to pass to each.



113
114
115
# File 'lib/merb-core/dispatch/session/cookie.rb', line 113

def each(&b) 
  @data.each(&b) 
end

Returns

String

Cookie value.

Raises

CookieOverflow

Session contains too much information.



85
86
87
88
89
90
91
# File 'lib/merb-core/dispatch/session/cookie.rb', line 85

def read_cookie
  unless @data.nil?
    updated = marshal(@data)
    raise CookieOverflow if updated.size > MAX
    updated
  end
end