Sha256: 90b2244977a4220bc159ed54b1376d2eeab68a52ac7c6942b39dd086e564e0b6

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

# LimitedSessions
# (c) 2007-2012 t.e.morgan
# Made available under the MIT license

# This version is compatible with Rack 1.4 (possibly earlier; untested).
# Correspondingly, it is compatible with Rails 3.x.

module LimitedSessions
  # Rack middleware that should be installed *after* the session handling middleware
  class Expiry
    DEFAULT_OPTIONS = {
      :recent_activity => nil,  # eg: 2.hours
      :max_session => nil       # eg: 24.hours
    }

    def initialize(app, options={})
      @app = app
      @options = DEFAULT_OPTIONS.merge(options)
    end

    def call(env)
      @env = env
      if @options[:recent_activity]
        if session[:last_visit] && (session[:last_visit] + @options[:recent_activity]) < Time.now.to_i
          logger.info "Session expired: no recent activity"
          clear_session
        end
        if @options[:recent_activity] > 600
          # Rounds to the nearest 5 minutes to minimize writes when a DB is in use
          session[:last_visit] = (Time.now.to_f/300).ceil*300
        else
          session[:last_visit] = (Time.now.to_f/10).ceil*10
        end
      end
      if @options[:max_session]
        session[:first_visit] ||= Time.now.to_i
        if (session[:first_visit] + @options[:max_session]) < Time.now.to_i
          logger.info "Session expired: max session length reached"
          clear_session
          session[:first_visit] ||= Time.now.to_i
        end
      end
      @app.call(env)
    end

    def session
      @env['rack.session'] || {}
    end
    def clear_session
      @env['rack.session'].clear
    end
    def logger
      (Rails.logger rescue nil) || @env['rack.logger']
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
limited_sessions-3.0.2 lib/limited_sessions/expiry.rb
limited_sessions-3.0.1 lib/limited_sessions/expiry.rb
limited_sessions-3.0.0 lib/limited_sessions/expiry.rb