Sha256: 48771461935099669b9969c4a1b33f46840c52648d89ee958aaef49512b3842c

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

require 'securerandom'

module MultiSessionStore
  class SubsessionGeneratorMiddleware
    def initialize(app, config = {})
      @app = app
      @config = config
    end

    def call(env)
      request = Rack::Request.new(env)
      set_subsession_id_from_header(request)
      generate_subsession_id_if_needed(request)
      @app.call(env)
    end

    private

    SUBSESSION_ID_HEADER = 'HTTP_X_SUBSESSIONID'.freeze

    def set_subsession_id_from_header(request)
      request.update_param 'subsession_id', request.get_header(SUBSESSION_ID_HEADER) if request.has_header?(SUBSESSION_ID_HEADER)
    end

    def generate_subsession_id_if_needed(request)
      request.update_param 'subsession_id', new_subsession_id if subsession_id_is_needed?(request)
    end

    def new_subsession_id
      SecureRandom.hex
    end

    def subsession_id_is_needed?(request)
      !request.params['subsession_id'] && !path_excluded?(request.path)
    end

    def path_excluded?(current_path)
      excluded_paths.any? { |excluded_path| excluded_path.match? current_path }
    end

    def excluded_paths
      @config[:exclude_paths] ||= []
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
multi_session_store-0.2.7 lib/multi_session_store/subsession_generator_middleware.rb
multi_session_store-0.2.6 lib/multi_session_store/subsession_generator_middleware.rb
multi_session_store-0.2.5 lib/multi_session_store/subsession_generator_middleware.rb
multi_session_store-0.2.4 lib/multi_session_store/subsession_generator_middleware.rb