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
272 def persist_session(headers, session)
273   opts = roda_class.sessions_opts
274 
275   if session.empty?
276     if env[SESSION_SERIALIZED]
277       # If session was submitted and is now empty, remove the cookie
278       Rack::Utils.delete_cookie_header!(headers, opts[:key], opts[:remove_cookie_options])
279     # else
280       # If no session was submitted, and the session is empty
281       # then there is no need to do anything
282     end
283   elsif cookie_value = _serialize_session(session)
284     cookie = Hash[opts[:cookie_options]]
285     cookie[:value] = cookie_value
286     cookie[:secure] = true if !cookie.has_key?(:secure) && ssl?
287 
288     before_size = if (set_cookie_before = headers[RodaResponseHeaders::SET_COOKIE]).is_a?(String)
289       set_cookie_before.bytesize
290     else
291       0
292     end
293 
294     Rack::Utils.set_cookie_header!(headers, opts[:key], cookie)
295 
296     cookie_size = case set_cookie_after = headers[RodaResponseHeaders::SET_COOKIE]
297     when String
298       # Rack < 3 always takes this branch, combines cookies into string, subtract previous size
299       # Rack 3+ takes this branch if this is the first cookie set, in which case before size is 0
300       set_cookie_after.bytesize - before_size
301     else # when Array
302       # Rack 3+ takes branch if this is not the first cookie set, and last element of the array
303       # is most recently added cookie
304       set_cookie_after.last.bytesize
305     end
306 
307     if cookie_size >= 4096
308       raise CookieTooLarge, "attempted to create cookie larger than 4096 bytes (bytes: #{cookie_size})"
309     end
310   end
311   
312   if env[SESSION_DELETE_RACK_COOKIE]
313     Rack::Utils.delete_cookie_header!(headers, opts[:upgrade_from_rack_session_cookie_key], opts[:upgrade_from_rack_session_cookie_options])
314   end
315 
316   nil
317 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
253 def session
254   @env[roda_class.sessions_opts[:env_key]] ||= _load_session
255 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
258 def session_created_at
259   session
260   Time.at(@env[SESSION_CREATED_AT]) if @env[SESSION_SERIALIZED]
261 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
264 def session_updated_at
265   session
266   Time.at(@env[SESSION_UPDATED_AT]) if @env[SESSION_SERIALIZED]
267 end