Skip to content

Make Rack Middleware methods public for Rails Abstract implementations #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Unreleased Changes

* Feature - Moves Error classes into the Errors module.

* Issue - Set `find_session`, `write_session`, and `delete_session` as public.

* Issue - Validate `Configuration` has secret_key on `:initialize` instead of on `:find_session`.

2.2.0 (2024-01-25)
------------------

Expand Down
67 changes: 31 additions & 36 deletions lib/aws/session_store/dynamo_db/rack_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,27 @@ class RackMiddleware < Rack::Session::Abstract::Persisted
def initialize(app, options = {})
super
@config = Configuration.new(options)
validate_config
set_locking_strategy
end

# @return [Configuration] An instance of Configuration that is used for
# this middleware.
attr_reader :config

private

def set_locking_strategy
@lock =
if @config.enable_locking
Aws::SessionStore::DynamoDB::Locking::Pessimistic.new(@config)
else
Aws::SessionStore::DynamoDB::Locking::Null.new(@config)
end
end

def validate_config
raise Errors::MissingSecretKeyError unless @config.secret_key
end

# Get session from the database or create a new session.
#
# @raise [Aws::SessionStore::DynamoDB::Errors::LockWaitTimeoutError] If the session
# has waited too long to obtain lock.
def find_session(req, sid)
validate_config
case verify_hmac(sid)
when nil
set_new_session_properties(req.env)
when false
handle_error { raise Errors::InvalidIDError }
set_new_session_properties(req.env)
else
get_session(req, sid)
data = @lock.get_session_data(req.env, sid)
[sid, data || {}]
end
end

# Sets new session properties.
def set_new_session_properties(env)
env['dynamo_db.new_session'] = 'true'
[generate_sid, {}]
end

# Retrieves session from the database after unpacking data.
#
# @raise [Aws::SessionStore::DynamoDB::Errors::LockWaitTimeoutError] If the session
# has waited too long to obtain lock.
def get_session(req, sid)
data = @lock.get_session_data(req.env, sid)
[sid, data || {}]
end

# Sets the session in the database after packing data.
#
# @return [Hash] If session has been saved.
Expand All @@ -84,6 +54,31 @@ def delete_session(req, sid, options)
generate_sid unless options[:drop]
end

# @return [Configuration] An instance of Configuration that is used for
# this middleware.
attr_reader :config

private

def set_locking_strategy
@lock =
if @config.enable_locking
Aws::SessionStore::DynamoDB::Locking::Pessimistic.new(@config)
else
Aws::SessionStore::DynamoDB::Locking::Null.new(@config)
end
end

def validate_config
raise Errors::MissingSecretKeyError unless @config.secret_key
end

# Sets new session properties.
def set_new_session_properties(env)
env['dynamo_db.new_session'] = 'true'
[generate_sid, {}]
end

# Each database operation is placed in this rescue wrapper.
# This wrapper will call the method, rescue any exceptions and then pass
# exceptions to the configured session handler.
Expand Down