/
session.rb
281 lines (236 loc) · 6.96 KB
/
session.rb
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# frozen_string_literal: true
require "rack/session/abstract/id"
module ActionDispatch
class Request
# Session is responsible for lazily loading the session from store.
class Session # :nodoc:
DisabledSessionError = Class.new(StandardError)
ENV_SESSION_KEY = Rack::RACK_SESSION # :nodoc:
ENV_SESSION_OPTIONS_KEY = Rack::RACK_SESSION_OPTIONS # :nodoc:
# Singleton object used to determine if an optional param wasn't specified.
Unspecified = Object.new
# Creates a session hash, merging the properties of the previous session if any.
def self.create(store, req, default_options)
session_was = find req
session = Request::Session.new(store, req)
session.merge! session_was if session_was
set(req, session)
Options.set(req, Request::Session::Options.new(store, default_options))
session
end
def self.disabled(req)
new(nil, req, enabled: false).tap do
Session::Options.set(req, Session::Options.new(nil, { id: nil }))
end
end
def self.find(req)
req.get_header ENV_SESSION_KEY
end
def self.set(req, session)
req.set_header ENV_SESSION_KEY, session
end
def self.delete(req)
req.delete_header ENV_SESSION_KEY
end
class Options # :nodoc:
def self.set(req, options)
req.set_header ENV_SESSION_OPTIONS_KEY, options
end
def self.find(req)
req.get_header ENV_SESSION_OPTIONS_KEY
end
def initialize(by, default_options)
@by = by
@delegate = default_options.dup
end
def [](key)
@delegate[key]
end
def id(req)
@delegate.fetch(:id) {
@by.send(:extract_session_id, req)
}
end
def []=(k, v); @delegate[k] = v; end
def to_hash; @delegate.dup; end
def values_at(*args); @delegate.values_at(*args); end
end
def initialize(by, req, enabled: true)
@by = by
@req = req
@delegate = {}
@loaded = false
@exists = nil # We haven't checked yet.
@enabled = enabled
@id_was = nil
@id_was_initialized = false
end
def id
options.id(@req)
end
def enabled?
@enabled
end
def options
Options.find @req
end
def destroy
clear
if enabled?
options = self.options || {}
@by.send(:delete_session, @req, options.id(@req), options)
# Load the new sid to be written with the response.
@loaded = false
load_for_write!
end
end
# Returns value of the key stored in the session or
# +nil+ if the given key is not found in the session.
def [](key)
load_for_read!
key = key.to_s
if key == "session_id"
id&.public_id
else
@delegate[key]
end
end
# Returns the nested value specified by the sequence of keys, returning
# +nil+ if any intermediate step is +nil+.
def dig(*keys)
load_for_read!
keys = keys.map.with_index { |key, i| i.zero? ? key.to_s : key }
@delegate.dig(*keys)
end
# Returns true if the session has the given key or false.
def has_key?(key)
load_for_read!
@delegate.key?(key.to_s)
end
alias :key? :has_key?
alias :include? :has_key?
# Returns keys of the session as Array.
def keys
load_for_read!
@delegate.keys
end
# Returns values of the session as Array.
def values
load_for_read!
@delegate.values
end
# Writes given value to given key of the session.
def []=(key, value)
load_for_write!
@delegate[key.to_s] = value
end
# Clears the session.
def clear
load_for_delete!
@delegate.clear
end
# Returns the session as Hash.
def to_hash
load_for_read!
@delegate.dup.delete_if { |_, v| v.nil? }
end
alias :to_h :to_hash
# Updates the session with given Hash.
#
# session.to_hash
# # => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2"}
#
# session.update({ "foo" => "bar" })
# # => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"}
#
# session.to_hash
# # => {"session_id"=>"e29b9ea315edf98aad94cc78c34cc9b2", "foo" => "bar"}
def update(hash)
unless hash.respond_to?(:to_hash)
raise TypeError, "no implicit conversion of #{hash.class.name} into Hash"
end
load_for_write!
@delegate.update hash.to_hash.stringify_keys
end
alias :merge! :update
# Deletes given key from the session.
def delete(key)
load_for_delete!
@delegate.delete key.to_s
end
# Returns value of the given key from the session, or raises +KeyError+
# if can't find the given key and no default value is set.
# Returns default value if specified.
#
# session.fetch(:foo)
# # => KeyError: key not found: "foo"
#
# session.fetch(:foo, :bar)
# # => :bar
#
# session.fetch(:foo) do
# :bar
# end
# # => :bar
def fetch(key, default = Unspecified, &block)
load_for_read!
if default == Unspecified
@delegate.fetch(key.to_s, &block)
else
@delegate.fetch(key.to_s, default, &block)
end
end
def inspect
if loaded?
super
else
"#<#{self.class}:0x#{(object_id << 1).to_s(16)} not yet loaded>"
end
end
def exists?
return false unless enabled?
return @exists unless @exists.nil?
@exists = @by.send(:session_exists?, @req)
end
def loaded?
@loaded
end
def empty?
load_for_read!
@delegate.empty?
end
def each(&block)
to_hash.each(&block)
end
def id_was
load_for_read!
@id_was
end
private
def load_for_read!
load! if !loaded? && exists?
end
def load_for_write!
if enabled?
load! unless loaded?
else
raise DisabledSessionError, "Your application has sessions disabled. To write to the session you must first configure a session store"
end
end
def load_for_delete!
load! if enabled? && !loaded?
end
def load!
if enabled?
@id_was_initialized = true unless exists?
id, session = @by.load_session @req
options[:id] = id
@delegate.replace(session.stringify_keys)
@id_was = id unless @id_was_initialized
end
@id_was_initialized = true
@loaded = true
end
end
end
end