2008-12-15 17:33:31 -05:00
|
|
|
require 'rack/utils'
|
2009-12-22 18:11:21 -05:00
|
|
|
require 'rack/request'
|
2010-09-20 04:18:44 -04:00
|
|
|
require 'rack/session/abstract/id'
|
2010-05-17 21:18:23 -04:00
|
|
|
require 'action_dispatch/middleware/cookies'
|
2012-05-03 17:28:11 -04:00
|
|
|
require 'action_dispatch/request/session'
|
2008-12-15 17:33:31 -05:00
|
|
|
|
2009-01-27 19:54:01 -05:00
|
|
|
module ActionDispatch
|
2008-12-15 17:33:31 -05:00
|
|
|
module Session
|
2009-09-24 00:37:31 -04:00
|
|
|
class SessionRestoreError < StandardError #:nodoc:
|
2012-05-02 14:55:14 -04:00
|
|
|
attr_reader :original_exception
|
|
|
|
|
|
|
|
def initialize(const_error)
|
|
|
|
@original_exception = const_error
|
|
|
|
|
|
|
|
super("Session contains objects whose class definition isn't available.\n" +
|
|
|
|
"Remember to require the classes for all objects kept in the session.\n" +
|
|
|
|
"(Original exception: #{const_error.message} [#{const_error.class}])\n")
|
|
|
|
end
|
2009-09-24 00:37:31 -04:00
|
|
|
end
|
|
|
|
|
2010-09-20 04:18:44 -04:00
|
|
|
module Compatibility
|
2008-12-15 17:33:31 -05:00
|
|
|
def initialize(app, options = {})
|
2010-09-20 04:18:44 -04:00
|
|
|
options[:key] ||= '_session_id'
|
|
|
|
super
|
2008-12-15 17:33:31 -05:00
|
|
|
end
|
|
|
|
|
2010-09-20 04:18:44 -04:00
|
|
|
def generate_sid
|
2011-05-23 07:02:06 -04:00
|
|
|
sid = SecureRandom.hex(16)
|
2011-12-25 06:34:58 -05:00
|
|
|
sid.encode!('UTF-8')
|
2011-04-14 14:27:27 -04:00
|
|
|
sid
|
2008-12-15 17:33:31 -05:00
|
|
|
end
|
2010-10-04 02:47:36 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def initialize_sid
|
|
|
|
@default_options.delete(:sidbits)
|
|
|
|
@default_options.delete(:secure_random)
|
|
|
|
end
|
2010-09-20 04:18:44 -04:00
|
|
|
end
|
2008-12-15 17:33:31 -05:00
|
|
|
|
2010-09-20 04:18:44 -04:00
|
|
|
module StaleSessionCheck
|
|
|
|
def load_session(env)
|
|
|
|
stale_session_check! { super }
|
|
|
|
end
|
2010-06-22 09:55:50 -04:00
|
|
|
|
2010-09-20 04:18:44 -04:00
|
|
|
def extract_session_id(env)
|
|
|
|
stale_session_check! { super }
|
|
|
|
end
|
2010-05-17 21:18:23 -04:00
|
|
|
|
2010-09-20 04:18:44 -04:00
|
|
|
def stale_session_check!
|
|
|
|
yield
|
|
|
|
rescue ArgumentError => argument_error
|
|
|
|
if argument_error.message =~ %r{undefined class/module ([\w:]*\w)}
|
|
|
|
begin
|
|
|
|
# Note that the regexp does not allow $1 to end with a ':'
|
|
|
|
$1.constantize
|
2012-05-02 14:55:14 -04:00
|
|
|
rescue LoadError, NameError => e
|
|
|
|
raise ActionDispatch::Session::SessionRestoreError, e, e.backtrace
|
2010-06-27 14:35:31 -04:00
|
|
|
end
|
2010-09-20 04:18:44 -04:00
|
|
|
retry
|
|
|
|
else
|
|
|
|
raise
|
2010-06-27 14:35:31 -04:00
|
|
|
end
|
2010-09-20 04:18:44 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-27 14:35:31 -04:00
|
|
|
|
2012-05-04 14:26:03 -04:00
|
|
|
module SessionObject # :nodoc:
|
2012-05-02 17:45:12 -04:00
|
|
|
def prepare_session(env)
|
2012-05-02 20:29:33 -04:00
|
|
|
Request::Session.create(self, env, @default_options)
|
2012-05-02 17:45:12 -04:00
|
|
|
end
|
|
|
|
|
2012-05-02 18:06:21 -04:00
|
|
|
def loaded_session?(session)
|
|
|
|
!session.is_a?(Request::Session) || session.loaded?
|
|
|
|
end
|
2012-05-04 14:26:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class AbstractStore < Rack::Session::Abstract::ID
|
|
|
|
include Compatibility
|
|
|
|
include StaleSessionCheck
|
|
|
|
include SessionObject
|
|
|
|
|
|
|
|
private
|
2012-05-02 18:06:21 -04:00
|
|
|
|
2012-04-29 20:14:51 -04:00
|
|
|
def set_cookie(env, session_id, cookie)
|
|
|
|
request = ActionDispatch::Request.new(env)
|
|
|
|
request.cookie_jar[key] = cookie
|
|
|
|
end
|
2008-12-15 17:33:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|