File: redis_store.rb

package info (click to toggle)
ruby-redis-actionpack 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 212 kB
  • sloc: ruby: 394; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 1,272 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

require 'redis-store'
require 'redis-rack'
require 'action_dispatch/middleware/session/abstract_store'

module ActionDispatch
  module Session
    # Session storage in Redis, using +Redis::Rack+ as a basis.
    class RedisStore < Rack::Session::Redis
      include Compatibility
      include StaleSessionCheck
      include SessionObject

      def initialize(app, options = {})
        options = options.dup
        options[:redis_server] ||= options[:servers]
        super
      end

      def generate_sid
        Rack::Session::SessionId.new(super)
      end

      private

      def set_cookie(env, _session_id, cookie)
        request = wrap_in_request(env)
        cookie_jar(request)[key] = cookie.merge(cookie_options)
      end

      def get_cookie(request)
        cookie_jar(request)[key]
      end

      def wrap_in_request(env)
        return env if env.is_a?(ActionDispatch::Request)
        ActionDispatch::Request.new(env)
      end

      def cookie_options
        @default_options.slice(:httponly, :secure)
      end

      def cookie_jar(request)
        if @default_options[:signed]
          request.cookie_jar.signed_or_encrypted
        else
          request.cookie_jar
        end
      end
    end
  end
end