rails_mongo_sessions 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/lib/rails_mongo_sessions/mongo_store.rb +44 -0
- data/rails_mongo_sessions.gemspec +52 -0
- data/test/helper.rb +9 -0
- data/test/test_mongo_store.rb +155 -0
- metadata +92 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Mathias Biilmann
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= rails_mongo_sessions
|
2
|
+
|
3
|
+
MongoDB Session store for Rails 3
|
4
|
+
|
5
|
+
Uses the ruby Mongo driver to store sessions in a MongoDB collection.
|
6
|
+
|
7
|
+
== Note on Patches/Pull Requests
|
8
|
+
|
9
|
+
* Fork the project.
|
10
|
+
* Make your feature addition or bug fix.
|
11
|
+
* Add tests for it. This is important so I don't break it in a
|
12
|
+
future version unintentionally.
|
13
|
+
* Commit, do not mess with rakefile, version, or history.
|
14
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
15
|
+
* Send me a pull request. Bonus points for topic branches.
|
16
|
+
|
17
|
+
== Copyright
|
18
|
+
|
19
|
+
Copyright (c) 2010 Mathias Biilmann. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "rails_mongo_sessions"
|
8
|
+
gem.summary = %Q{MongoDB Session store for rails}
|
9
|
+
gem.description = %Q{Uses the ruby Mongo driver to store sessions in a MongoDB collection}
|
10
|
+
gem.email = "info@mathias-biilmann.net"
|
11
|
+
gem.homepage = "http://github.com/biilmann/rails_mongo_sessions"
|
12
|
+
gem.authors = ["Mathias Biilmann"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
|
15
|
+
gem.add_dependency 'actionpack', '~> 3.0'
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "rails_mongo_sessions #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.3
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'action_dispatch/middleware/session/abstract_store'
|
2
|
+
|
3
|
+
module ActionDispatch
|
4
|
+
module Session
|
5
|
+
class MongoStore < AbstractStore
|
6
|
+
attr_accessor :collection
|
7
|
+
|
8
|
+
def initialize(app, options = {})
|
9
|
+
require 'mongo'
|
10
|
+
|
11
|
+
unless options[:collection]
|
12
|
+
raise "To avoid creating multiple connections to MongoDB, " +
|
13
|
+
"the Mongo Session Store will not create it's own connection" +
|
14
|
+
"to MongoDB - you must pass in a collection with the :collection option"
|
15
|
+
end
|
16
|
+
|
17
|
+
@collection = options[:collection].respond_to?(:call) ? options[:collection].call : options[:collection]
|
18
|
+
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def get_session(env, sid)
|
24
|
+
sid ||= generate_sid
|
25
|
+
data = collection.find_one('_id' => sid)
|
26
|
+
[sid, data ? unpack(data['s']) : {}]
|
27
|
+
end
|
28
|
+
|
29
|
+
def set_session(env, sid, session_data)
|
30
|
+
collection.update({'_id' => sid}, {'_id' => sid, 's' => pack(session_data)}, {:upsert => true})
|
31
|
+
sid
|
32
|
+
end
|
33
|
+
|
34
|
+
def pack(data)
|
35
|
+
[Marshal.dump(data)].pack("m*")
|
36
|
+
end
|
37
|
+
|
38
|
+
def unpack(packed)
|
39
|
+
return nil unless packed
|
40
|
+
Marshal.load(packed.unpack("m*").first)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rails_mongo_sessions}
|
8
|
+
s.version = "0.2.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Mathias Biilmann"]
|
12
|
+
s.date = %q{2010-06-15}
|
13
|
+
s.description = %q{Uses the ruby Mongo driver to store sessions in a MongoDB collection}
|
14
|
+
s.email = %q{info@mathias-biilmann.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/rails_mongo_sessions/mongo_store.rb",
|
27
|
+
"rails_mongo_sessions.gemspec"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://github.com/biilmann/rails_mongo_sessions}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.3.7}
|
33
|
+
s.summary = %q{MongoDB Session store for rails}
|
34
|
+
s.test_files = [
|
35
|
+
"test/helper.rb",
|
36
|
+
"test/test_mongo_store.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_runtime_dependency(%q<actionpack>, ["~> 3.0"])
|
45
|
+
else
|
46
|
+
s.add_dependency(%q<actionpack>, ["~> 3.0"])
|
47
|
+
end
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<actionpack>, ["~> 3.0"])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'mongo'
|
3
|
+
require 'action_dispatch'
|
4
|
+
require 'action_dispatch/testing/integration'
|
5
|
+
require 'helper'
|
6
|
+
|
7
|
+
class RoutedRackApp
|
8
|
+
attr_reader :routes
|
9
|
+
|
10
|
+
def initialize(routes, &blk)
|
11
|
+
@routes = routes
|
12
|
+
@stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes)
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
@stack.call(env)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# You need to start a mongodb server inorder to run these tests
|
21
|
+
class MongoStoreTest < ActionController::IntegrationTest
|
22
|
+
class TestController < ActionController::Base
|
23
|
+
def no_session_access
|
24
|
+
head :ok
|
25
|
+
end
|
26
|
+
|
27
|
+
def set_session_value
|
28
|
+
session[:foo] = "bar"
|
29
|
+
head :ok
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_session_value
|
33
|
+
render :text => "foo: #{session[:foo].inspect}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_session_id
|
37
|
+
session[:foo]
|
38
|
+
render :text => "#{request.session_options[:id]}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def call_reset_session
|
42
|
+
session[:bar]
|
43
|
+
reset_session
|
44
|
+
session[:bar] = "baz"
|
45
|
+
head :ok
|
46
|
+
end
|
47
|
+
|
48
|
+
def rescue_action(e) raise end
|
49
|
+
end
|
50
|
+
|
51
|
+
COLLECTION = Mongo::Connection.new.db('rails_mongo_sessions').collection('sessions')
|
52
|
+
|
53
|
+
def test_setting_and_getting_session_value
|
54
|
+
with_test_route_set do
|
55
|
+
get '/set_session_value'
|
56
|
+
assert_response :success
|
57
|
+
assert cookies['_session_id']
|
58
|
+
|
59
|
+
get '/get_session_value'
|
60
|
+
assert_response :success
|
61
|
+
assert_equal 'foo: "bar"', response.body
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_getting_nil_session_value
|
66
|
+
with_test_route_set do
|
67
|
+
get '/get_session_value'
|
68
|
+
assert_response :success
|
69
|
+
assert_equal 'foo: nil', response.body
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_setting_session_value_after_session_reset
|
74
|
+
with_test_route_set do
|
75
|
+
get '/set_session_value'
|
76
|
+
assert_response :success
|
77
|
+
assert cookies['_session_id']
|
78
|
+
session_id = cookies['_session_id']
|
79
|
+
|
80
|
+
get '/call_reset_session'
|
81
|
+
assert_response :success
|
82
|
+
assert_not_equal [], headers['Set-Cookie']
|
83
|
+
|
84
|
+
get '/get_session_value'
|
85
|
+
assert_response :success
|
86
|
+
assert_equal 'foo: nil', response.body
|
87
|
+
|
88
|
+
get '/get_session_id'
|
89
|
+
assert_response :success
|
90
|
+
assert_not_equal session_id, response.body
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_getting_session_id
|
95
|
+
with_test_route_set do
|
96
|
+
get '/set_session_value'
|
97
|
+
assert_response :success
|
98
|
+
assert cookies['_session_id']
|
99
|
+
session_id = cookies['_session_id']
|
100
|
+
|
101
|
+
get '/get_session_id'
|
102
|
+
assert_response :success
|
103
|
+
assert_equal session_id, response.body
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_prevents_session_fixation
|
108
|
+
with_test_route_set do
|
109
|
+
get '/get_session_value'
|
110
|
+
assert_response :success
|
111
|
+
assert_equal 'foo: nil', response.body
|
112
|
+
session_id = cookies['_session_id']
|
113
|
+
|
114
|
+
reset!
|
115
|
+
|
116
|
+
get '/set_session_value', :_session_id => session_id
|
117
|
+
assert_response :success
|
118
|
+
assert_not_equal session_id, cookies['_session_id']
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.build_app(routes = nil)
|
123
|
+
RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
|
124
|
+
middleware.use "ActionDispatch::Callbacks"
|
125
|
+
middleware.use "ActionDispatch::ParamsParser"
|
126
|
+
middleware.use "ActionDispatch::Cookies"
|
127
|
+
middleware.use "ActionDispatch::Flash"
|
128
|
+
middleware.use "ActionDispatch::Head"
|
129
|
+
yield(middleware) if block_given?
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
self.app = build_app
|
134
|
+
|
135
|
+
private
|
136
|
+
def with_test_route_set
|
137
|
+
with_routing do |set|
|
138
|
+
set.draw do |map|
|
139
|
+
match ':action', :to => ::MongoStoreTest::TestController
|
140
|
+
end
|
141
|
+
::MongoStoreTest::TestController.class_eval do
|
142
|
+
include set.url_helpers
|
143
|
+
end
|
144
|
+
|
145
|
+
|
146
|
+
@app = self.class.build_app(set) do |middleware|
|
147
|
+
middleware.use ActionDispatch::Session::MongoStore, :key => '_session_id', :collection => COLLECTION
|
148
|
+
end
|
149
|
+
|
150
|
+
yield
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
|
155
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_mongo_sessions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 17
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 3
|
10
|
+
version: 0.2.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Mathias Biilmann
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-15 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: actionpack
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
version: "3.0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: Uses the ruby Mongo driver to store sessions in a MongoDB collection
|
37
|
+
email: info@mathias-biilmann.net
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- LICENSE
|
44
|
+
- README.rdoc
|
45
|
+
files:
|
46
|
+
- .document
|
47
|
+
- .gitignore
|
48
|
+
- LICENSE
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- VERSION
|
52
|
+
- lib/rails_mongo_sessions/mongo_store.rb
|
53
|
+
- rails_mongo_sessions.gemspec
|
54
|
+
- test/helper.rb
|
55
|
+
- test/test_mongo_store.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/biilmann/rails_mongo_sessions
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: MongoDB Session store for rails
|
90
|
+
test_files:
|
91
|
+
- test/helper.rb
|
92
|
+
- test/test_mongo_store.rb
|