lib/global_session/rack.rb in global_session-0.9.0 vs lib/global_session/rack.rb in global_session-1.0.0
- old
+ new
@@ -50,11 +50,11 @@
# Read a cookie from the Rack environment.
#
# === Parameters
# env(Hash): Rack environment.
def read_cookie(env)
- if env['rack.cookies'].key?(@cookie_name)
+ if env['rack.cookies'].has_key?(@cookie_name)
env['global_session'] = Session.new(@directory,
env['rack.cookies'][@cookie_name])
elsif @cookie_retrieval && cookie = @cookie_retrieval.call(env)
env['global_session'] = Session.new(@directory, cookie)
else
@@ -67,31 +67,33 @@
# Renew the session ticket.
#
# === Parameters
# env(Hash): Rack environment
def renew_cookie(env)
+ return unless env['global_session'].directory.local_authority_name
+ return if env['global_session.req.renew'] == false
+
if (renew = @configuration['renew']) && env['global_session'] &&
- env['global_session.req.renew'] != false &&
- env['global_session'].directory.local_authority_name &&
env['global_session'].expired_at < Time.at(Time.now.utc + 60 * renew.to_i)
env['global_session'].renew!
end
end
# Update the cookie jar with the revised ticket.
#
# === Parameters
# env(Hash): Rack environment
def update_cookie(env)
+ return unless env['global_session'].directory.local_authority_name
return if env['global_session.req.update'] == false
begin
domain = @configuration['cookie']['domain'] || env['SERVER_NAME']
if env['global_session'] && env['global_session'].valid?
value = env['global_session'].to_s
expires = @configuration['ephemeral'] ? nil : env['global_session'].expired_at
- unless env['rack.cookies'].key?(@cookie_name) &&
+ unless env['rack.cookies'].has_key?(@cookie_name) &&
env['rack.cookies'][@cookie_name] == value
env['rack.cookies'][@cookie_name] = {:value => value, :domain => domain, :expires => expires}
end
else
# write an empty cookie
@@ -101,33 +103,34 @@
wipe_cookie(env)
raise e
end
end
- # Delete the ticket from the cookie jar.
+ # Delete the global session cookie from the cookie jar.
#
# === Parameters
# env(Hash): Rack environment
def wipe_cookie(env)
domain = @configuration['cookie']['domain'] || env['SERVER_NAME']
env['rack.cookies'][@cookie_name] = {:value => nil, :domain => domain, :expires => Time.at(0)}
end
- # Handle exceptions that occur during app invocation.
+ # Handle exceptions that occur during app invocation. This will either save the error
+ # in the Rack environment or raise it, depending on the type of error. The error may
+ # also be logged.
#
# === Parameters
# activity(String): name of activity in which error happened
# env(Hash): Rack environment
# e(Exception): error that happened
def handle_error(activity, env, e)
if e.is_a? ClientError
env['global_session.error'] = e
- return @app.call(env)
+ wipe_cookie(env)
elsif e.is_a? ConfigurationError
env['rack.logger'].error("#{e.class} while #{activity}: #{e} #{e.backtrace}") if env['rack.logger']
env['global_session.error'] = e
- return @app.call(env)
else
raise e
end
end
@@ -138,25 +141,28 @@
begin
read_cookie(env)
rescue Exception => e
env['global_session'] = Session.new(@directory)
- return handle_error('reading session cookie', env, e)
+ handle_error('reading session cookie', env, e)
end
+ tuple = nil
+
begin
tuple = @app.call(env)
+ rescue Exception => e
+ handle_error('processing request', env, e)
+ return tuple
+ else
renew_cookie(env)
update_cookie(env)
return tuple
- rescue Exception => e
- wipe_cookie(env)
- return handle_error('processing request', env, e)
end
end
end
end
end
module Rack
- GlobalSession = GlobalSession::Rack::Middleware unless defined?(GlobalSession)
+ GlobalSession = ::GlobalSession::Rack::Middleware unless defined?(::Rack::GlobalSession)
end