module Roda::RodaPlugins::Sessions::RequestMethods

  1. lib/roda/plugins/sessions.rb

Public Instance methods

persist_session(headers, session)

Persist the session data as a cookie. If transparently upgrading from Rack::Session::Cookie, mark the related cookie for expiration so it isn’t sent in the future.

[show source]
    # File lib/roda/plugins/sessions.rb
263 def persist_session(headers, session)
264   opts = roda_class.opts[:sessions]
265 
266   if session.empty?
267     if env[SESSION_SERIALIZED]
268       # If session was submitted and is now empty, remove the cookie
269       Rack::Utils.delete_cookie_header!(headers, opts[:key], opts[:remove_cookie_options])
270     # else
271       # If no session was submitted, and the session is empty
272       # then there is no need to do anything
273     end
274   elsif cookie_value = _serialize_session(session)
275     cookie = Hash[opts[:cookie_options]]
276     cookie[:value] = cookie_value
277     cookie[:secure] = true if !cookie.has_key?(:secure) && ssl?
278 
279     before_size = if (set_cookie_before = headers[RodaResponseHeaders::SET_COOKIE]).is_a?(String)
280       set_cookie_before.bytesize
281     else
282       0
283     end
284 
285     Rack::Utils.set_cookie_header!(headers, opts[:key], cookie)
286 
287     cookie_size = case set_cookie_after = headers[RodaResponseHeaders::SET_COOKIE]
288     when String
289       # Rack < 3 always takes this branch, combines cookies into string, subtract previous size
290       # Rack 3+ takes this branch if this is the first cookie set, in which case before size is 0
291       set_cookie_after.bytesize - before_size
292     else # when Array
293       # Rack 3+ takes branch if this is not the first cookie set, and last element of the array
294       # is most recently added cookie
295       set_cookie_after.last.bytesize
296     end
297 
298     if cookie_size >= 4096
299       raise CookieTooLarge, "attempted to create cookie larger than 4096 bytes (bytes: #{cookie_size})"
300     end
301   end
302   
303   if env[SESSION_DELETE_RACK_COOKIE]
304     Rack::Utils.delete_cookie_header!(headers, opts[:upgrade_from_rack_session_cookie_key], opts[:upgrade_from_rack_session_cookie_options])
305   end
306 
307   nil
308 end
session()

Load the session information from the cookie. With the sessions plugin, you must call this method to get the session, instead of trying to access the session directly through the request environment. For maximum compatibility with other software that uses rack sessions, this method stores the session in ‘rack.session’ in the request environment, but that does not happen until this method is called.

[show source]
    # File lib/roda/plugins/sessions.rb
244 def session
245   @env[roda_class.opts[:sessions][:env_key]] ||= _load_session
246 end
session_created_at()

The time the session was originally created. nil if there is no active session.

[show source]
    # File lib/roda/plugins/sessions.rb
249 def session_created_at
250   session
251   Time.at(@env[SESSION_CREATED_AT]) if @env[SESSION_SERIALIZED]
252 end
session_updated_at()

The time the session was last updated. nil if there is no active session.

[show source]
    # File lib/roda/plugins/sessions.rb
255 def session_updated_at
256   session
257   Time.at(@env[SESSION_UPDATED_AT]) if @env[SESSION_SERIALIZED]
258 end