tomtaylor (owner)

Revisions

gist: 14086 Download_button fork
public
Public Clone URL: git://gist.github.com/14086.git
Embed All Files: show embed
Text #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# For what I have done, please forgive me.
# This monkey patch adds in the HttpOnly support for the session cookie
# This is present and accepted into Rails 2.2 at the time of writing, but not in Rails 2.1.
# You can remove this and set HttpOnly properly when moving to Rails 2.2
 
module ActionController
  class RackRequest
    DEFAULT_SESSION_OPTIONS = {
      :database_manager => CGI::Session::CookieStore, # store data in cookie
      :prefix => "ruby_sess.", # prefix session file names
      :session_path => "/", # available to all paths in app
      :session_key => "_session_id",
      :cookie_only => true,
      :session_http_only=> true
    }
  end
  
  class CgiRequest
    DEFAULT_SESSION_OPTIONS = {
      :database_manager => CGI::Session::CookieStore, # store data in cookie
      :prefix => "ruby_sess.", # prefix session file names
      :session_path => "/", # available to all paths in app
      :session_key => "_session_id",
      :cookie_only => true,
      :session_http_only=> true
    }
  end
end
 
class CGI::Session::CookieStore
  
  def initialize(session, options = {})
    # The session_key option is required.
    if options['session_key'].blank?
      raise ArgumentError, 'A session_key is required to write a cookie containing the session data. Use config.action_controller.session = { :session_key => "_myapp_session", :secret => "some secret phrase" } in config/environment.rb'
    end
 
    # The secret option is required.
    ensure_secret_secure(options['secret'])
 
    # Keep the session and its secret on hand so we can read and write cookies.
    @session, @secret = session, options['secret']
 
    # Message digest defaults to SHA1.
    @digest = options['digest'] || 'SHA1'
 
    # Default cookie options derived from session settings.
    @cookie_options = {
      'name' => options['session_key'],
      'path' => options['session_path'],
      'domain' => options['session_domain'],
      'expires' => options['session_expires'],
      'secure' => options['session_secure'],
      'http_only' => options['session_http_only']
    }
 
    # Set no_hidden and no_cookies since the session id is unused and we
    # set our own data cookie.
    options['no_hidden'] = true
    options['no_cookies'] = true
  end
end