limited_sessions 3.0.2 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 372dbf49775aefdb7902b765bc5727c9506f6df46251d890e5a31e957ba70962
4
+ data.tar.gz: d2d4e1684ec8f9b183a5cdf3d4722e73d8ac8bd7d1b656b41c3fd0f3d2ed6a62
5
+ SHA512:
6
+ metadata.gz: 92285992bce470310c88b656caf2a62d26bfa9315656d13bab944e44241743eb1a7bb86d912f837a13328be682a402c021d89552124577ce26e6212d84c1dba6
7
+ data.tar.gz: 176aea0865080e0399d2cd5f17cd3a04bb3daeccc06e5be982ed4c422bbeee6b2c1b47aaa73d7ff3a514468dcda0a39adee855916d955ecb48ec43b39b098425
data/CHANGELOG CHANGED
@@ -1,3 +1,33 @@
1
+ * 2021-apr-20 - v5.0.0
2
+
3
+ - Drop support for Rack <= 2.0.8 and Rails < 5.2
4
+ - Update for new rubies
5
+ - Cleanup readme and comments
6
+
7
+ * 2017-may-22 - v4.2.0
8
+
9
+ - Fixed ActiveRecord session cleanup on Rails 5.1
10
+ - Prevent ActiveRecord session cleanup from possibly running more often than
11
+ configured due to Rails loading sessions more than once per request.
12
+
13
+ * 2016-feb-12 - v4.1.0
14
+
15
+ - Support Rails 5.0 & Rack 2.0
16
+
17
+ * 2013-dec-14 - v4.0.1
18
+
19
+ - Fix deprecation warning
20
+
21
+ * 2013-jun-15 - Support for Rails 4
22
+
23
+ - v4.0.0 - Rails 4 compatibility. Use v3.x.x for Rails 3 apps.
24
+ - For non-ActiveRecord session stores, no change is required from the
25
+ previous version.
26
+ - For ActiveRecord session stores, you must add the
27
+ 'activerecord-session_store' gem to your Gemfile and it must be
28
+ above limited_sessions so that it will be auto-detected properly.
29
+ This is the only change required.
30
+
1
31
  * 2012-nov-14 - Merge changes from ejdraper
2
32
 
3
33
  - Lower Rack requirement to v1.2.5+ for Rails 3.0 compatibility
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2007-2012 t.e.morgan
1
+ Copyright 2007-2021 t.e.morgan
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md ADDED
@@ -0,0 +1,205 @@
1
+ # LimitedSessions
2
+
3
+ LimitedSessions provides two distinct features, each in a separate part:
4
+
5
+ * Rack-compatible middleware that expires sessions based on inactivity or maximum session length. The middleware supports any session storage type, including cookies, Redis, ActiveRecord, etc.
6
+
7
+ * Rails extension to the (now separate) ActiveRecord Session Store to auto-cleanup stale session records.
8
+
9
+
10
+ ## Features
11
+
12
+ * For all session stores:
13
+ * Configurable session expiry time (eg: 2 hours from last page access)
14
+ * Optional hard maximum limit from beginning of session (eg: 24 hours)
15
+
16
+ * When using the ActiveRecord Session Store:
17
+ * DB-based handling of session expiry (activity and hard limits) instead of by session paramters
18
+ * Auto-cleaning of expired session records
19
+
20
+
21
+ ## Requirements
22
+
23
+ * Rack and any Rack-compatible app (including Rails)
24
+ * Utilizing Rack's (or Rails') sessions
25
+ * For ActiveRecord session enhancements:
26
+ * Must be using the standard ActiveRecord::SessionStore
27
+ (`ActionDispatch::Session::ActiveRecordStore.session_store = :active_record_store`)
28
+ * Ensure your sessions table has an `updated_at` column
29
+ * If using hard session limits, a `created_at` column is needed too
30
+
31
+
32
+ ## Compatibility
33
+
34
+ The middleware should be compatible with any framework using a recent version of Rack. It has been tested with Rack 2.x and Rails 5.2-6.1.
35
+
36
+ The optional ActiveRecord Session Store extension requires Rails.
37
+
38
+ If using Rack < 2.0.9 or Rails < 5.2, use LimitedSessions 4.x.
39
+
40
+
41
+ ## Upgrading
42
+
43
+ No changes are required to upgrade from LimitedSessions 4.x to 5.0.
44
+
45
+ Upgrading `activerecord-session_store` from 1.x to 2.x may require changes. See its own upgrade instructions.
46
+
47
+
48
+ ## Installation
49
+
50
+ Add this gem to your Gemfile or otherwise make it available to your app. Then, configure as required.
51
+
52
+ ```ruby
53
+ gem 'limited_sessions', '~> 5'
54
+ ```
55
+
56
+ If storing sessions in the DB using ActiveRecord with AR Session Store:
57
+
58
+ ```ruby
59
+ gem 'activerecord-session_store'
60
+ gem 'limited_sessions', '~> 5'
61
+ ```
62
+
63
+ `activerecord-session_store` must be loaded first in order for `limited_sessions` to properly detect it.
64
+
65
+
66
+ ## Configuration
67
+
68
+ ### Rack Middleware with Rails
69
+
70
+ 1. Add/update `config/initializers/session_store.rb` and append the following:
71
+
72
+ ```ruby
73
+ config.middleware.insert_after ActionDispatch::Flash, LimitedSessions::Expiry, \
74
+ recent_activity: 2.hours, max_session: 24.hours
75
+ ```
76
+
77
+ 2. Configuration options.
78
+
79
+ The example above shows both configuration options. You may include one, both, or none.
80
+
81
+ #### Session activity timeout
82
+ Example: `recent_activity: 2.hours`
83
+ By default, the session activity timeout is disabled (`nil`).
84
+
85
+ #### Maximum session length
86
+ Example: `max_session: 24.hours`
87
+ By default, the maximum session length is disabled (`nil`).
88
+
89
+
90
+ ### Rack Middleware apart from Rails
91
+
92
+ 1. In `config.ru`, add the following *after* the middleware that handles your sessions.
93
+
94
+ ```ruby
95
+ use LimitedSessions::Expiry, recent_activity: 2.hours, max_session: 24.hours
96
+ ```
97
+
98
+ 2. For configuration options, see #2 above, under Rack Middleware with Rails.
99
+
100
+
101
+ ### ActionRecord Session Store extension
102
+
103
+ 1. If you don't already have an `updated_at` column on your sessions table, create a migration and add it. If you plan to use the hard session limit feature, you'll also need to add `created_at`.
104
+
105
+ 2. Tell Rails to use your the new session store. Change `config/initializers/session_store.rb` to reflect the following:
106
+
107
+ ```ruby
108
+ Rails.application.config.session_store :active_record_store
109
+ ActionDispatch::Session::ActiveRecordStore.session_class = LimitedSessions::SelfCleaningSession
110
+ ```
111
+
112
+ 3. Configuration options.
113
+
114
+ Each of the following options should also be added to your initializer file from step 2.
115
+
116
+ #### Self-cleaning
117
+ By default, SelfCleaningSession will clean the sessions table every 1000 page views. Technically, it's a 1 in 1000 chance on each page. For most sites this is good. Higher traffic sites may want to increase it to 10000 or more. Set to 0 to disable self-cleaning.
118
+
119
+ ```ruby
120
+ LimitedSessions::SelfCleaningSession.self_clean_sessions = 1000
121
+ ```
122
+
123
+ #### Session activity timeout
124
+ The default session activity timeout is 2 hours. This uses the `updated_at` column which will be updated on every page load.
125
+
126
+ This can also be disabled by setting to `nil`. However, the `updated_at` column is still required for self-cleaning and will effectively function as if set to `1.week`. If you really want it longer, set it to `1.year` or something.
127
+
128
+ ```ruby
129
+ LimitedSessions::SelfCleaningSession.recent_activity = 2.hours
130
+ ```
131
+
132
+ #### Maximum session length
133
+ By default, maximum session length handling is disabled. When enabled, it uses the `created_at` column to do its work.
134
+
135
+ A value of `nil` disables this feature and `created_at` does not need to exist in this case.
136
+
137
+ ```ruby
138
+ LimitedSessions::SelfCleaningSession.max_session = 12.hours
139
+ ```
140
+
141
+
142
+ ## Questions
143
+
144
+ * Do I need both the middleware and the ActiveRecord Session Store?
145
+
146
+ No. While it should work, it is not necessary to use both the middleware
147
+ and the ActiveRecord Session Store. If you are storing sessions via AR,
148
+ then use the ActiveRecord Session Store. If you are storing sessions any
149
+ other way, then use the middleware.
150
+
151
+ * I'm storing sessions in {Memcache, Redis, etc.} and they auto-expire sessions. Do I need this?
152
+
153
+ Maybe, maybe not. Normally, that auto-expire period is equivalent to LimitedSessions' :recent_activity. If that's all you want, then you don't need this. However, if you'd also like to put a maximum cap on session length, regardless of activity, then LimitedSessions' `:max_session` feature will still be useful.
154
+
155
+ * Can I use the middleware with ActiveRecord instead of the ActionRecord Session Store enhancement?
156
+
157
+ Yes. Session expiry (recent activity and max session length) should work fine in this circumstance. The only thing you won't get is self-cleaning of the AR sessions table.
158
+
159
+ * How are session expiry times tracked?
160
+
161
+ The middleware adds one or two keys to the session data: `:last_visit` and/or `:first_visit`.
162
+
163
+ The AR enhancement uses `updated_a`t and possibly `created_at`.
164
+
165
+ * How is this different from using the session cookie's own expires= value?
166
+
167
+ The cookie's own value puts the trust in the client to self-expire. If you really want to control session lengths, then you need to manage the values on the application side. LimitedSessions is fully compatible with the cookie's expires= value, however, and the two can be used together.
168
+
169
+ * What's the difference between `:recent_activity` and `:max_session`?
170
+
171
+ Recent activity requires regular access on your site. If it's set to 15 minutes, then a page must be loaded at least once every 15 minutes.
172
+
173
+ Max session is a cap on the session from the very beginning. If it's set to 12 hours, then even if a user is accessing the page constantly, and not triggering the recent activity timeout, after 12 hours their session would be reset anyway.
174
+
175
+ * What are the security implications of using LimitedSessions?
176
+
177
+ LimitedSessions enhances security by reducing risk of session cookie replay attacks. The specifics will depend on what cookie store you're using.
178
+
179
+ For Rails' default cookie store, `:max_session` handling is perhaps most valuable as it guarantees an end to the session. Rails' default behavior allows a session to last for an infinite time. If a cookie is somehow exposed, the holder of the cookie has an open-ended session. Note that signing and/or encryption do not mitigate this.
180
+
181
+ For any session store that uses a server-side database (AR, memcache, Redis, etc.), at least the user can formally logout and terminate the session. Auto-expiring sessions (memcache, Redis, AR w/SelfCleaningSession, etc.) will also expire if allowed to, but can also be maintained perpetually by ongoing access.
182
+
183
+ Since the cookie store doesn't expire ever, `:recent_activity` addresses this by making sessions expire similarly to if memcache, Redis, or something similar was being used.
184
+
185
+ It is recommended to use both aspects of LimitedSessions for best security.
186
+
187
+ * What are the performance implications of using LimitedSessions?
188
+
189
+ The middleware should have minimal impact.
190
+
191
+ The AR enhancement should result in an overall net gain in performance as the size of the AR sessions table will be kept to a smaller size. The 1 in 1000 hit (or whatever you've configured it to) may be slightly slower while the database cleanup is in progress.
192
+
193
+
194
+ ## Contributing
195
+
196
+ 1. Fork it ( https://github.com/zarqman/smart_assets/fork )
197
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
198
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
199
+ 4. Push to the branch (`git push origin my-new-feature`)
200
+ 5. Create new Pull Request
201
+
202
+
203
+ ## License
204
+
205
+ MIT
@@ -1,11 +1,7 @@
1
- # LimitedSessions
2
- # (c) 2007-2012 t.e.morgan
3
- # Made available under the MIT license
4
-
5
1
  module LimitedSessions
6
2
  end
7
3
 
8
4
  require 'limited_sessions/expiry'
9
- if defined? ActiveRecord
5
+ if defined? ActiveRecord::SessionStore::Session
10
6
  require 'limited_sessions/self_cleaning_session'
11
7
  end
@@ -1,16 +1,9 @@
1
- # LimitedSessions
2
- # (c) 2007-2012 t.e.morgan
3
- # Made available under the MIT license
4
-
5
- # This version is compatible with Rack 1.4 (possibly earlier; untested).
6
- # Correspondingly, it is compatible with Rails 3.x.
7
-
8
1
  module LimitedSessions
9
2
  # Rack middleware that should be installed *after* the session handling middleware
10
3
  class Expiry
11
4
  DEFAULT_OPTIONS = {
12
- :recent_activity => nil, # eg: 2.hours
13
- :max_session => nil # eg: 24.hours
5
+ recent_activity: nil, # eg: 2.hours
6
+ max_session: nil # eg: 24.hours
14
7
  }
15
8
 
16
9
  def initialize(app, options={})
@@ -1,14 +1,8 @@
1
- # LimitedSessions
2
- # (c) 2007-2012 t.e.morgan
3
- # Made available under the MIT license
4
-
5
- # This is the Rails 3.x version; it is /not/ compatible with Rails 2.x.
6
-
7
1
  module LimitedSessions
8
2
  class SelfCleaningSession < ActiveRecord::SessionStore::Session
9
3
 
10
4
  # disable short circuit by Dirty module; ensures :updated_at is kept updated
11
- self.partial_updates = false
5
+ self.partial_writes = false
12
6
 
13
7
  self.table_name = 'sessions'
14
8
 
@@ -29,19 +23,21 @@ module LimitedSessions
29
23
  # If this is a problem, use a migration and rename the column.
30
24
  def find_by_session_id(session_id)
31
25
  consider_self_clean
32
- active_session.current_session.where(:session_id=>session_id).first
26
+ active_session.current_session.where(session_id: session_id).first
33
27
  end
34
28
 
35
29
  private
36
30
  def consider_self_clean
37
31
  return if self_clean_sessions == 0
32
+ return if defined?(@@last_check) && @@last_check == Time.now.to_i
38
33
  if rand(self_clean_sessions) == 0
34
+ @@last_check = Time.now.to_i
39
35
  # logger.info "SelfCleaningSession :: scrubbing expired sessions"
40
36
  look_back_recent = recent_activity || 1.week
41
37
  if max_session
42
- delete_all ['updated_at < ? OR created_at < ?', Time.current - look_back_recent, Time.current - max_session]
38
+ self.where('updated_at < ? OR created_at < ?', Time.current - look_back_recent, Time.current - max_session).delete_all
43
39
  elsif columns_hash['updated_at']
44
- delete_all ['updated_at < ?', Time.current - look_back_recent]
40
+ self.where('updated_at < ?', Time.current - look_back_recent).delete_all
45
41
  else
46
42
  # logger.warning "WARNING: Unable to self-clean Sessions table; updated_at column is missing"
47
43
  self.self_clean_sessions = 0
@@ -1,3 +1,3 @@
1
1
  module LimitedSessions
2
- VERSION = "3.0.2"
2
+ VERSION = '5.0.0'
3
3
  end
metadata CHANGED
@@ -1,94 +1,96 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: limited_sessions
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
5
- prerelease:
4
+ version: 5.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - t.e.morgan
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-15 00:00:00.000000000 Z
11
+ date: 2021-04-20 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rack
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: 1.2.5
22
- - - <
19
+ version: 2.0.9
20
+ - - "<"
23
21
  - !ruby/object:Gem::Version
24
- version: '2.0'
22
+ version: '3'
25
23
  type: :runtime
26
24
  prerelease: false
27
25
  version_requirements: !ruby/object:Gem::Requirement
28
- none: false
29
26
  requirements:
30
- - - ! '>='
27
+ - - ">="
31
28
  - !ruby/object:Gem::Version
32
- version: 1.2.5
33
- - - <
29
+ version: 2.0.9
30
+ - - "<"
34
31
  - !ruby/object:Gem::Version
35
- version: '2.0'
32
+ version: '3'
36
33
  - !ruby/object:Gem::Dependency
37
34
  name: sqlite3
38
35
  requirement: !ruby/object:Gem::Requirement
39
- none: false
40
36
  requirements:
41
- - - ! '>='
37
+ - - ">="
42
38
  - !ruby/object:Gem::Version
43
39
  version: '0'
44
40
  type: :development
45
41
  prerelease: false
46
42
  version_requirements: !ruby/object:Gem::Requirement
47
- none: false
48
43
  requirements:
49
- - - ! '>='
44
+ - - ">="
50
45
  - !ruby/object:Gem::Version
51
46
  version: '0'
52
47
  - !ruby/object:Gem::Dependency
53
48
  name: rails
54
49
  requirement: !ruby/object:Gem::Requirement
55
- none: false
56
50
  requirements:
57
- - - ~>
51
+ - - ">="
58
52
  - !ruby/object:Gem::Version
59
- version: 3.2.6
53
+ version: '5.2'
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '6.2'
60
57
  type: :development
61
58
  prerelease: false
62
59
  version_requirements: !ruby/object:Gem::Requirement
63
- none: false
64
60
  requirements:
65
- - - ~>
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '5.2'
64
+ - - "<"
66
65
  - !ruby/object:Gem::Version
67
- version: 3.2.6
68
- description: ! 'LimitedSessions provides two core features to handle cookie-based
69
- session expiry: 1) Rack Middleware for most session stores and 2) an ActiveRecord
70
- extension for AR-based session stores. Sessions can be expired on inactivity and/or
71
- overall session length.'
66
+ version: '6.2'
67
+ description: 'LimitedSessions provides two core features to handle cookie-based session
68
+ expiry: 1) Rack Middleware for most session stores and 2) an ActiveRecord extension
69
+ for AR-based session stores. Sessions can be expired on inactivity and/or overall
70
+ session length.'
72
71
  email:
73
72
  - tm@iprog.com
74
73
  executables: []
75
74
  extensions: []
76
75
  extra_rdoc_files: []
77
76
  files:
77
+ - CHANGELOG
78
+ - MIT-LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - lib/limited_sessions.rb
78
82
  - lib/limited_sessions/expiry.rb
79
83
  - lib/limited_sessions/self_cleaning_session.rb
80
84
  - lib/limited_sessions/version.rb
81
- - lib/limited_sessions.rb
82
85
  - lib/tasks/limited_sessions_tasks.rake
83
- - MIT-LICENSE
84
- - Rakefile
85
- - README
86
- - CHANGELOG
86
+ - test/dummy/README.rdoc
87
+ - test/dummy/Rakefile
87
88
  - test/dummy/app/assets/javascripts/application.js
88
89
  - test/dummy/app/assets/stylesheets/application.css
89
90
  - test/dummy/app/controllers/application_controller.rb
90
91
  - test/dummy/app/helpers/application_helper.rb
91
92
  - test/dummy/app/views/layouts/application.html.erb
93
+ - test/dummy/config.ru
92
94
  - test/dummy/config/application.rb
93
95
  - test/dummy/config/boot.rb
94
96
  - test/dummy/config/database.yml
@@ -104,72 +106,65 @@ files:
104
106
  - test/dummy/config/initializers/wrap_parameters.rb
105
107
  - test/dummy/config/locales/en.yml
106
108
  - test/dummy/config/routes.rb
107
- - test/dummy/config.ru
108
- - test/dummy/db/test.sqlite3
109
109
  - test/dummy/log/test.log
110
110
  - test/dummy/public/404.html
111
111
  - test/dummy/public/422.html
112
112
  - test/dummy/public/500.html
113
113
  - test/dummy/public/favicon.ico
114
- - test/dummy/Rakefile
115
- - test/dummy/README.rdoc
116
114
  - test/dummy/script/rails
117
115
  - test/limited_sessions_test.rb
118
116
  - test/test_helper.rb
119
- homepage: http://iprog.com/projects#limited_sessions
117
+ homepage: https://iprog.com/projects#limited_sessions
120
118
  licenses: []
121
- post_install_message:
119
+ metadata: {}
120
+ post_install_message:
122
121
  rdoc_options: []
123
122
  require_paths:
124
123
  - lib
125
124
  required_ruby_version: !ruby/object:Gem::Requirement
126
- none: false
127
125
  requirements:
128
- - - ! '>='
126
+ - - ">="
129
127
  - !ruby/object:Gem::Version
130
128
  version: '0'
131
129
  required_rubygems_version: !ruby/object:Gem::Requirement
132
- none: false
133
130
  requirements:
134
- - - ! '>='
131
+ - - ">="
135
132
  - !ruby/object:Gem::Version
136
133
  version: '0'
137
134
  requirements: []
138
- rubyforge_project:
139
- rubygems_version: 1.8.24
140
- signing_key:
141
- specification_version: 3
135
+ rubygems_version: 3.0.9
136
+ signing_key:
137
+ specification_version: 4
142
138
  summary: Server-side session expiry via either Rack Middleware or ActiveRecord extension
143
139
  test_files:
140
+ - test/dummy/app/controllers/application_controller.rb
141
+ - test/dummy/app/views/layouts/application.html.erb
144
142
  - test/dummy/app/assets/javascripts/application.js
145
143
  - test/dummy/app/assets/stylesheets/application.css
146
- - test/dummy/app/controllers/application_controller.rb
147
144
  - test/dummy/app/helpers/application_helper.rb
148
- - test/dummy/app/views/layouts/application.html.erb
149
- - test/dummy/config/application.rb
150
- - test/dummy/config/boot.rb
151
- - test/dummy/config/database.yml
152
- - test/dummy/config/environment.rb
153
- - test/dummy/config/environments/development.rb
145
+ - test/dummy/config/routes.rb
146
+ - test/dummy/config/locales/en.yml
154
147
  - test/dummy/config/environments/production.rb
148
+ - test/dummy/config/environments/development.rb
155
149
  - test/dummy/config/environments/test.rb
150
+ - test/dummy/config/environment.rb
151
+ - test/dummy/config/application.rb
152
+ - test/dummy/config/database.yml
153
+ - test/dummy/config/boot.rb
156
154
  - test/dummy/config/initializers/backtrace_silencers.rb
157
- - test/dummy/config/initializers/inflections.rb
158
155
  - test/dummy/config/initializers/mime_types.rb
159
- - test/dummy/config/initializers/secret_token.rb
160
156
  - test/dummy/config/initializers/session_store.rb
161
157
  - test/dummy/config/initializers/wrap_parameters.rb
162
- - test/dummy/config/locales/en.yml
163
- - test/dummy/config/routes.rb
158
+ - test/dummy/config/initializers/secret_token.rb
159
+ - test/dummy/config/initializers/inflections.rb
164
160
  - test/dummy/config.ru
165
- - test/dummy/db/test.sqlite3
166
- - test/dummy/log/test.log
167
- - test/dummy/public/404.html
161
+ - test/dummy/script/rails
162
+ - test/dummy/Rakefile
163
+ - test/dummy/public/favicon.ico
168
164
  - test/dummy/public/422.html
169
165
  - test/dummy/public/500.html
170
- - test/dummy/public/favicon.ico
171
- - test/dummy/Rakefile
166
+ - test/dummy/public/404.html
167
+ - test/dummy/log/test.log
172
168
  - test/dummy/README.rdoc
173
- - test/dummy/script/rails
174
169
  - test/limited_sessions_test.rb
175
170
  - test/test_helper.rb
data/README DELETED
@@ -1,201 +0,0 @@
1
- LimitedSessions
2
- ===============
3
- Copyright 2007-2012 t.e.morgan.
4
- License: MIT
5
-
6
- Updates/info: http://iprog.com/projects#limited_sessions
7
- Source: https://github.com/zarqman/limited_sessions
8
- Contact: tm@iprog.com
9
-
10
-
11
- LimitedSessions provides two distinct features, each in a separate part:
12
- * Rack-compatible middleware that expires sessions based on inactivity or
13
- maximum session length. This works with Rails 3 just fine.
14
- * Rails 3 extension to the ActiveRecord Session Store to auto-cleanup stale
15
- session records.
16
-
17
-
18
- Notes on Rails and Rack versions:
19
- The middleware should be compatible with any framework using a recent
20
- version of Rack. It was tested with Rack 1.4 and Rails 3.2.
21
-
22
- The ActiveRecord Session Store extension requires Rails 3 (and was also
23
- tested with Rails 3.2).
24
-
25
- Versions compatible with Rails 2.3 and Rails 2.2/prior can be found at:
26
- https://github.com/zarqman/limited_sessions/tree/v2.3 and
27
- https://github.com/zarqman/limited_sessions/tree/v2.2
28
-
29
-
30
- Upgrading from previous versions:
31
- Both initialization and configuration options have changed. See the
32
- Configuration section below.
33
-
34
- Note that all support for IP address restrictions has been removed. IPv4/IPv6
35
- dual-stack environments have demonstrated a number of real-world issues,
36
- namely user HTTP traffic bouncing between IPv4 and IPv6 resulting in chronic
37
- session resets. Additionally, homes and offices increasingly have two or more
38
- ISPs, not to mention mobile devices bouncing between WiFi and 3G/4G networks.
39
- These scenarios also cause frequent IP address changes.
40
-
41
-
42
- Features:
43
- * For all session stores:
44
- * Configurable session expiry time (eg: 2 hours from last page access)
45
- * Optional hard maximum limit from beginning of session (eg: 24 hours)
46
- * When using the ActiveRecord Session Store:
47
- * DB-based handling of session expiry (activity and hard limits) instead of
48
- by session paramters
49
- * Auto-cleaning of expired session records
50
-
51
-
52
- Requirements:
53
- * Rack and possibly Rails 3
54
- * Utilizing Rack's (or Rails') sessions support
55
- * For ActiveRecord session enhancements:
56
- * Must be using the standard ActiveRecord::SessionStore
57
- (ActionController::Base.session_store = :active_record_store)
58
- * Ensure your sessions table has an `updated_at` column
59
- * If using hard session limits, a `created_at` column is needed too
60
-
61
-
62
- Installation:
63
- Add this gem to your Gemfile (Rails) or otherwise make it available to your
64
- app. Then, configure as required.
65
-
66
- gem 'limited_sessions'
67
-
68
-
69
- Configuration:
70
- Rack Middleware with Rails
71
- 1. To either your config/environments/production.rb or your
72
- config/application.rb file (depending on if you want this to apply in
73
- production only or also during development), add the following:
74
-
75
- config.middleware.insert_after ActionDispatch::Flash, LimitedSessions::Expiry, \
76
- :recent_activity=>2.hours, :max_session=>24.hours
77
-
78
- 2. Configuration options.
79
- The example above shows both configuration options. You may include
80
- both, one, or none.
81
-
82
- * Session activity timeout *
83
- Example: :recent_activity => 2.hours
84
- By default, the session activity timeout is disabled (nil).
85
-
86
- * Maximum session length *
87
- Example: :max_session => 24.hours
88
- By default, the maximum session length is disabled (nil).
89
-
90
-
91
- Rack Middleware apart from Rails
92
- 1. In your config.ru, add the following *after* the middleware that handles
93
- your sessions.
94
-
95
- use LimitedSessions::Expiry, :recent_activity=>2.hours, :max_session=>24.hours
96
-
97
- 2. See #2 above, under Rack Middleware with Rails, for Configuration options.
98
-
99
-
100
- ActionRecord Session Store
101
- 1. If you don't already have an 'updated_at' column on your sessions table,
102
- create a migration and add it. If you plan to use the hard session limit
103
- feature, you'll also need to add 'created_at'.
104
-
105
- 2. Tell Rails to use your the new session store. Change
106
- config/initializers/session_store.rb to reflect the following:
107
-
108
- <YourApp>::Application.config.session_store :active_record_store
109
- ActiveRecord::SessionStore.session_class = LimitedSessions::SelfCleaningSession
110
-
111
- 3. Configuration options.
112
- Each of the following options should also be added to your initializer
113
- file from step 2.
114
-
115
-
116
- * Self-cleaning *
117
- By default, SelfCleaningSession will clean sessions out about every 1000
118
- page views. Technically, it's a 1 in 1000 chance on each page. For most
119
- sites this is good. Higher traffic sites may want to increase it to
120
- 10000 or more. 0 will disable self-cleaning.
121
-
122
- LimitedSessions::SelfCleaningSession.self_clean_sessions = 1000
123
-
124
-
125
- * Session activity timeout *
126
- The default session activity timeout is 2 hours. This uses the
127
- 'updated_at' column which will be updated on every page load.
128
-
129
- This can also be disabled by setting to nil. However, the 'updated_at'
130
- column is still required for self-cleaning and will effectively function
131
- as if this was set to 1.week. If you really want it longer, set it to
132
- 1.year or something.
133
-
134
- LimitedSessions::SelfCleaningSession.recent_activity = 2.hours
135
-
136
-
137
- * Maximum session length *
138
- By default, the maximum session length handling is disabled. When
139
- enabled, it uses the 'created_at' column to do its work.
140
-
141
- A value of nil disables this feature and 'created_at' does not need to
142
- exist in this case.
143
-
144
- LimitedSessions::SelfCleaningSession.max_session = 12.hours
145
-
146
-
147
- Other questions:
148
- Do I need both the middleware and the ActiveRecord Session Store?
149
- No. While it should work, it is not necessary to use both the middleware
150
- and the ActiveRecord Session Store. If you are storing sessions via AR,
151
- then use the ActiveRecord Session Store. If you are storing sessions any
152
- other way, then use the middleware.
153
-
154
- I'm storing sessions in {Memcache, Redis, etc.} and they auto-expire
155
- sessions. Do I need this?
156
- Maybe, maybe not. Normally, that auto-expire period is equivalent to
157
- LimitedSessions' :recent_activity. If that's all you want, then you don't
158
- need this. However, if you'd also like to put a maximum cap on session
159
- length, regardless of activity, then LimitedSessions' :max_session feature
160
- will still be useful.
161
-
162
- Can I use the middleware with ActiveRecord instead of the ActionRecord
163
- Session Store enhancement?
164
- Yes; session expiry (recent activity and max session length) should work
165
- fine in this circumstance. The only thing you won't get is self-cleaning of
166
- the AR sessions table.
167
-
168
- How are session expiry times tracked?
169
- The middleware adds one or two keys to the session data: :last_visit and/or
170
- :first_visit.
171
- The AR enhancement uses 'updated_at' and possibly 'created_at'.
172
-
173
- How is this different from using the session cookie's own expires= value?
174
- The cookie's own value puts the trust in the client to self-expire. If you
175
- really want to control session lengths, then you need to manage the values
176
- on the application side. LimitedSessions is fully compatible with the
177
- cookie's expires= value, however, and the two can be used together.
178
-
179
- What's the difference between :recent_activity and :max_session?
180
- Recent activity requires regular access on your site. If it's set to 15
181
- minutes, then a page must be loaded at least once every 15 minutes.
182
-
183
- Max session is a cap on the session from the very beginning. If it's set to
184
- 12 hours, then even if a user is accessing the page constantly, and not
185
- triggering the recent activity timeout, after 12 hours their session would
186
- be reset anyway.
187
-
188
- Is the AR enhancement compatible with the legacy 'sessid' column?
189
- No. Please rename that column to 'session_id'.
190
-
191
-
192
- Other Notes:
193
- I'm sure there are better ways to do some of what's here, but this seems to
194
- work. This version has been tested on Rack 1.4, Rails 3.2, PostgreSQL 9.1,
195
- and Redis 2.2 (via the redis and redis-session-store gems). Other databases
196
- and session stores should work, but if you find a bug, I'd love to hear about
197
- it. Likewise, give me a shout if you have a suggestion or just want to tell
198
- me that it works. Thanks for checking limited_sessions out!
199
-
200
- --t (tm@iprog.com; http://iprog.com/)
201
-
File without changes