Files
openfoodnetwork/lib/session_cookie_upgrader.rb
Matt-Yorkley 1d472d0dec Add Rack Middleware for transitioning existing sessions
This checks if the current request has been submitted using the old session key (_session_id) and transparently migrates the session id to a new session cookie with the new settings and the new key (_ofn_session_id).
2021-08-22 15:57:43 +01:00

36 lines
944 B
Ruby

# frozen_string_literal: true
class SessionCookieUpgrader
def initialize(app, options = {})
@app = app
@options = options
end
def call(env)
request = ::Rack::Request.new(env)
cookies = request.cookies
old_key = @options[:old_key]
new_key = @options[:new_key]
# Set the session id for this request from the old session cookie (if present)
# This must be done before @app.call(env) or a new session will be initialized
cookies[new_key] = cookies[old_key] if cookies[old_key]
status, headers, body = @app.call(env)
if cookies[old_key]
# Create new session cookie with pre-existing session id
Rack::Utils.set_cookie_header!(
headers,
new_key,
{ value: cookies[old_key], path: "/", domain: @options[:domain] }
)
# Delete old session cookie
Rack::Utils.delete_cookie_header!(headers, old_key)
end
[status, headers, body]
end
end