@@ -156,26 +156,30 @@ def decode(str)
156156
157157 attr_reader :coder , :encryptors
158158
159- def initialize ( app , options = { } )
160- # support both :secrets and :secret for backwards compatibility
161- secrets = [ *( options [ :secrets ] || options [ :secret ] ) ]
159+ def initialize ( app , coder : Marshal , serialize_json : false , key : nil , purpose : nil , secrets : [ ] , secret : nil , **options )
160+ # Support both :secrets and :secret for backwards compatibility:
161+ if secret
162+ secrets << secret
163+ end
164+
165+ # `serialize_json` is awefully specific... allow a general `coder` option:
166+ if serialize_json
167+ coder ||= JSON
168+ end
162169
163- encryptor_opts = {
164- purpose : options [ :key ] , serialize_json : options [ :serialize_json ]
165- }
170+ # Let's consider `key` to be legacy:
171+ purpose ||= key
166172
167- # For each secret, create an Encryptor. We have iterate this Array at
168- # decryption time to achieve key rotation.
173+ # For each secret, create an Encryptor, to support key rotation:
169174 @encryptors = secrets . map do |secret |
170- Rack ::Session ::Encryptor . new secret , encryptor_opts
175+ Rack ::Session ::Encryptor . new ( secret , delegate : coder , purpose : purpose )
171176 end
172177
173- # If a legacy HMAC secret is present, initialize those features.
174- # Fallback to :secret for backwards compatibility.
175- if options . has_key? ( :legacy_hmac_secret ) || options . has_key? ( :secret )
178+ # If a legacy HMAC secret is present, initialize those features:
179+ if options . has_key? ( :legacy_hmac_secret ) || secret
176180 @legacy_hmac = options . fetch ( :legacy_hmac , 'SHA1' )
177181
178- @legacy_hmac_secret = options [ :legacy_hmac_secret ] || options [ : secret]
182+ @legacy_hmac_secret = options [ :legacy_hmac_secret ] || secret
179183 @legacy_hmac_coder = options . fetch ( :legacy_hmac_coder , Base64 ::Marshal . new )
180184 else
181185 @legacy_hmac = false
@@ -216,7 +220,7 @@ def unpacked_cookie_data(request)
216220 session_data = nil
217221
218222 # Try to decrypt the session data with our encryptors
219- encryptors . each do |encryptor |
223+ @ encryptors. each do |encryptor |
220224 begin
221225 session_data = encryptor . decrypt ( cookie_data )
222226 break
@@ -290,10 +294,10 @@ def legacy_generate_hmac(data)
290294 end
291295
292296 def encode_session_data ( session )
293- if encryptors . empty?
297+ if @ encryptors. empty?
294298 coder . encode ( session )
295299 else
296- encryptors . first . encrypt ( session )
300+ @ encryptors. first . encrypt ( session )
297301 end
298302 end
299303
0 commit comments