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