Sha256: 372d06f334ca6d001d242d2e838714a49b3b73fd418910f8d42d17147144c78f

Contents?: true

Size: 1.97 KB

Versions: 32

Compression:

Stored size: 1.97 KB

Contents

begin
  require "rails"
rescue Exception
end

require "timber/config"
require "timber/contexts/session"
require "timber/integrations/rack/middleware"

module Timber
  module Integrations
    module Rack
      # A Rack middleware that is responsible for adding the Session context
      # {Timber::Contexts::Session}.
      class SessionContext < Middleware
        def call(env)
          id = get_session_id(env)
          if id
            context = Contexts::Session.new(id: id)
            CurrentContext.with(context) do
              @app.call(env)
            end
          else
            @app.call(env)
          end
        end

        private
          def get_session_id(env)
            if defined?(::Rails)
              session_key = ::Rails.application.config.session_options[:key]
              request = ::ActionDispatch::Request.new(env)
              Timber::Config.instance.debug { "Rails detected, extracting session_id from cookie" }
              extract_from_cookie(request, session_key)

            elsif session = env['rack.session']
              if session.respond_to?(:id)
                Timber::Config.instance.debug { "Rack env session detected, using id attribute" }
                session.id
              elsif session.respond_to?(:[])
                Timber::Config.instance.debug { "Rack env session detected, using the session_id key" }
                session["session_id"]
              else
                Timber::Config.instance.debug { "Rack env session detected but could not extract id" }
                nil
              end
            else
              Timber::Config.instance.debug { "No session data could be detected, skipping" }

              nil
            end
          rescue Exception => e
            nil
          end

          def extract_from_cookie(request, session_key)
            data = request
              .cookie_jar
              .signed_or_encrypted[session_key] || {}
            data["session_id"]
          end
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
timber-2.6.2 lib/timber/integrations/rack/session_context.rb
timber-2.6.1 lib/timber/integrations/rack/session_context.rb
timber-2.6.0 lib/timber/integrations/rack/session_context.rb
timber-2.6.0.pre.beta2 lib/timber/integrations/rack/session_context.rb
timber-2.6.0.pre.beta1 lib/timber/integrations/rack/session_context.rb
timber-2.5.1 lib/timber/integrations/rack/session_context.rb
timber-2.5.0 lib/timber/integrations/rack/session_context.rb
timber-2.4.0 lib/timber/integrations/rack/session_context.rb
timber-2.3.4 lib/timber/integrations/rack/session_context.rb
timber-2.3.3 lib/timber/integrations/rack/session_context.rb
timber-2.3.2 lib/timber/integrations/rack/session_context.rb
timber-2.3.1 lib/timber/integrations/rack/session_context.rb
timber-2.3.0 lib/timber/integrations/rack/session_context.rb
timber-2.2.3 lib/timber/integrations/rack/session_context.rb
timber-2.2.2 lib/timber/integrations/rack/session_context.rb
timber-2.2.1 lib/timber/integrations/rack/session_context.rb
timber-2.2.0 lib/timber/integrations/rack/session_context.rb
timber-2.1.10 lib/timber/integrations/rack/session_context.rb
timber-2.1.9 lib/timber/integrations/rack/session_context.rb
timber-2.1.8 lib/timber/integrations/rack/session_context.rb