diff --git a/CHANGELOG.md b/CHANGELOG.md index 311a0a4..aa11560 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------ diff --git a/lib/aws/session_store/dynamo_db/rack_middleware.rb b/lib/aws/session_store/dynamo_db/rack_middleware.rb index 5c508d9..d3d2a1f 100644 --- a/lib/aws/session_store/dynamo_db/rack_middleware.rb +++ b/lib/aws/session_store/dynamo_db/rack_middleware.rb @@ -17,31 +17,15 @@ 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) @@ -49,25 +33,11 @@ def find_session(req, sid) 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. @@ -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.