@@ -17,57 +17,27 @@ class RackMiddleware < Rack::Session::Abstract::Persisted
17
17
def initialize ( app , options = { } )
18
18
super
19
19
@config = Configuration . new ( options )
20
+ validate_config
20
21
set_locking_strategy
21
22
end
22
23
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
-
42
24
# 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.
43
28
def find_session ( req , sid )
44
- validate_config
45
29
case verify_hmac ( sid )
46
30
when nil
47
31
set_new_session_properties ( req . env )
48
32
when false
49
33
handle_error { raise Errors ::InvalidIDError }
50
34
set_new_session_properties ( req . env )
51
35
else
52
- get_session ( req , sid )
36
+ data = @lock . get_session_data ( req . env , sid )
37
+ [ sid , data || { } ]
53
38
end
54
39
end
55
40
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
-
71
41
# Sets the session in the database after packing data.
72
42
#
73
43
# @return [Hash] If session has been saved.
@@ -84,6 +54,31 @@ def delete_session(req, sid, options)
84
54
generate_sid unless options [ :drop ]
85
55
end
86
56
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
+
87
82
# Each database operation is placed in this rescue wrapper.
88
83
# This wrapper will call the method, rescue any exceptions and then pass
89
84
# exceptions to the configured session handler.
0 commit comments