Skip to content

Commit cca714c

Browse files
authored
Make Rack Middleware methods public for Rails Abstract implementations (#38)
1 parent b18dbea commit cca714c

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Unreleased Changes
99

1010
* Feature - Moves Error classes into the Errors module.
1111

12+
* Issue - Set `find_session`, `write_session`, and `delete_session` as public.
13+
14+
* Issue - Validate `Configuration` has secret_key on `:initialize` instead of on `:find_session`.
15+
1216
2.2.0 (2024-01-25)
1317
------------------
1418

lib/aws/session_store/dynamo_db/rack_middleware.rb

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,27 @@ class RackMiddleware < Rack::Session::Abstract::Persisted
1717
def initialize(app, options = {})
1818
super
1919
@config = Configuration.new(options)
20+
validate_config
2021
set_locking_strategy
2122
end
2223

23-
# @return [Configuration] An instance of Configuration that is used for
24-
# this middleware.
25-
attr_reader :config
26-
27-
private
28-
29-
def set_locking_strategy
30-
@lock =
31-
if @config.enable_locking
32-
Aws::SessionStore::DynamoDB::Locking::Pessimistic.new(@config)
33-
else
34-
Aws::SessionStore::DynamoDB::Locking::Null.new(@config)
35-
end
36-
end
37-
38-
def validate_config
39-
raise Errors::MissingSecretKeyError unless @config.secret_key
40-
end
41-
4224
# Get session from the database or create a new session.
25+
#
26+
# @raise [Aws::SessionStore::DynamoDB::Errors::LockWaitTimeoutError] If the session
27+
# has waited too long to obtain lock.
4328
def find_session(req, sid)
44-
validate_config
4529
case verify_hmac(sid)
4630
when nil
4731
set_new_session_properties(req.env)
4832
when false
4933
handle_error { raise Errors::InvalidIDError }
5034
set_new_session_properties(req.env)
5135
else
52-
get_session(req, sid)
36+
data = @lock.get_session_data(req.env, sid)
37+
[sid, data || {}]
5338
end
5439
end
5540

56-
# Sets new session properties.
57-
def set_new_session_properties(env)
58-
env['dynamo_db.new_session'] = 'true'
59-
[generate_sid, {}]
60-
end
61-
62-
# Retrieves session from the database after unpacking data.
63-
#
64-
# @raise [Aws::SessionStore::DynamoDB::Errors::LockWaitTimeoutError] If the session
65-
# has waited too long to obtain lock.
66-
def get_session(req, sid)
67-
data = @lock.get_session_data(req.env, sid)
68-
[sid, data || {}]
69-
end
70-
7141
# Sets the session in the database after packing data.
7242
#
7343
# @return [Hash] If session has been saved.
@@ -84,6 +54,31 @@ def delete_session(req, sid, options)
8454
generate_sid unless options[:drop]
8555
end
8656

57+
# @return [Configuration] An instance of Configuration that is used for
58+
# this middleware.
59+
attr_reader :config
60+
61+
private
62+
63+
def set_locking_strategy
64+
@lock =
65+
if @config.enable_locking
66+
Aws::SessionStore::DynamoDB::Locking::Pessimistic.new(@config)
67+
else
68+
Aws::SessionStore::DynamoDB::Locking::Null.new(@config)
69+
end
70+
end
71+
72+
def validate_config
73+
raise Errors::MissingSecretKeyError unless @config.secret_key
74+
end
75+
76+
# Sets new session properties.
77+
def set_new_session_properties(env)
78+
env['dynamo_db.new_session'] = 'true'
79+
[generate_sid, {}]
80+
end
81+
8782
# Each database operation is placed in this rescue wrapper.
8883
# This wrapper will call the method, rescue any exceptions and then pass
8984
# exceptions to the configured session handler.

0 commit comments

Comments
 (0)