mongo_sessions 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
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,35 @@
1
+ = mongo_sessions
2
+
3
+ MongoDB Session store for Rails 3 and Rack applications
4
+
5
+ Uses the ruby Mongo driver to store sessions in a MongoDB collection.
6
+
7
+ = When Using in Rails 3 Applications
8
+
9
+ To use the session store in rails (In this example with Mongoid) add the rails store to your Gemfile:
10
+
11
+ gem "mongo_sessions", :require => "mongo_sessions/rails_mongo_store"
12
+
13
+ And change config/application.rb to something like:
14
+
15
+ config.session_store :mongo_store, :key => '_my_session', :collection => lambda { Mongoid.master.collection('sessions') }
16
+
17
+ = When Using in Rack Applications
18
+
19
+ Require the rack mongo store and add the Rack Middleware to your app:
20
+
21
+ use Rack::Session::MongoStore
22
+
23
+ == Note on Patches/Pull Requests
24
+
25
+ * Fork the project.
26
+ * Make your feature addition or bug fix.
27
+ * Add tests for it. This is important so I don't break it in a
28
+ future version unintentionally.
29
+ * Commit, do not mess with rakefile, version, or history.
30
+ (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)
31
+ * Send me a pull request. Bonus points for topic branches.
32
+
33
+ == Copyright
34
+
35
+ 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 = "mongo_sessions"
8
+ gem.summary = %Q{MongoDB Session store for Rails and Rack}
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/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 = "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.3.0
@@ -0,0 +1,46 @@
1
+ #require 'action_dispatch/middleware/session/abstract_store'
2
+ require 'rack/session/abstract/id'
3
+
4
+ module MongoSessions
5
+ module MongoStore
6
+ def collection
7
+ @collection
8
+ end
9
+
10
+ def initialize(app, options = {})
11
+ require 'mongo'
12
+
13
+ unless options[:collection]
14
+ raise "To avoid creating multiple connections to MongoDB, " +
15
+ "the Mongo Session Store will not create it's own connection " +
16
+ "to MongoDB - you must pass in a collection with the :collection option"
17
+ end
18
+
19
+ @collection = options[:collection].respond_to?(:call) ? options[:collection].call : options[:collection]
20
+
21
+ super
22
+ end
23
+
24
+ private
25
+ def get_session(env, sid)
26
+ sid ||= generate_sid
27
+ data = collection.find_one('_id' => sid)
28
+ [sid, data ? unpack(data['s']) : {}]
29
+ end
30
+
31
+ def set_session(env, sid, session_data, options = {})
32
+ sid ||= generate_sid
33
+ collection.update({'_id' => sid}, {'_id' => sid, 's' => pack(session_data)}, {:upsert => true})
34
+ sid
35
+ end
36
+
37
+ def pack(data)
38
+ [Marshal.dump(data)].pack("m*")
39
+ end
40
+
41
+ def unpack(packed)
42
+ return nil unless packed
43
+ Marshal.load(packed.unpack("m*").first)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,10 @@
1
+ require 'mongo_sessions/mongo_store'
2
+ require 'rack/session/abstract/id'
3
+
4
+ module Rack
5
+ module Session
6
+ class MongoStore < Rack::Session::Abstract::ID
7
+ include MongoSessions::MongoStore
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'mongo_sessions/mongo_store'
2
+ require 'action_dispatch/middleware/session/abstract_store'
3
+
4
+ module ActionDispatch
5
+ module Session
6
+ class MongoStore < Rack::Session::Abstract::ID
7
+ include MongoSessions::MongoStore
8
+ end
9
+ end
10
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+
7
+ class Test::Unit::TestCase
8
+ end
@@ -0,0 +1,156 @@
1
+ require 'rubygems'
2
+ require 'mongo'
3
+ require 'action_dispatch'
4
+ require 'action_dispatch/testing/integration'
5
+ require 'helper'
6
+ require 'mongo_sessions/rails_mongo_store'
7
+
8
+ class RoutedRackApp
9
+ attr_reader :routes
10
+
11
+ def initialize(routes, &blk)
12
+ @routes = routes
13
+ @stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes)
14
+ end
15
+
16
+ def call(env)
17
+ @stack.call(env)
18
+ end
19
+ end
20
+
21
+ # You need to start a mongodb server inorder to run these tests
22
+ class MongoStoreTest < ActionController::IntegrationTest
23
+ class TestController < ActionController::Base
24
+ def no_session_access
25
+ head :ok
26
+ end
27
+
28
+ def set_session_value
29
+ session[:foo] = "bar"
30
+ head :ok
31
+ end
32
+
33
+ def get_session_value
34
+ render :text => "foo: #{session[:foo].inspect}"
35
+ end
36
+
37
+ def get_session_id
38
+ session[:foo]
39
+ render :text => "#{request.session_options[:id]}"
40
+ end
41
+
42
+ def call_reset_session
43
+ session[:bar]
44
+ reset_session
45
+ session[:bar] = "baz"
46
+ head :ok
47
+ end
48
+
49
+ def rescue_action(e) raise end
50
+ end
51
+
52
+ COLLECTION = Mongo::Connection.new.db('mongo_sessions').collection('sessions')
53
+
54
+ def test_setting_and_getting_session_value
55
+ with_test_route_set do
56
+ get '/set_session_value'
57
+ assert_response :success
58
+ assert cookies['_session_id']
59
+
60
+ get '/get_session_value'
61
+ assert_response :success
62
+ assert_equal 'foo: "bar"', response.body
63
+ end
64
+ end
65
+
66
+ def test_getting_nil_session_value
67
+ with_test_route_set do
68
+ get '/get_session_value'
69
+ assert_response :success
70
+ assert_equal 'foo: nil', response.body
71
+ end
72
+ end
73
+
74
+ def test_setting_session_value_after_session_reset
75
+ with_test_route_set do
76
+ get '/set_session_value'
77
+ assert_response :success
78
+ assert cookies['_session_id']
79
+ session_id = cookies['_session_id']
80
+
81
+ get '/call_reset_session'
82
+ assert_response :success
83
+ assert_not_equal [], headers['Set-Cookie']
84
+
85
+ get '/get_session_value'
86
+ assert_response :success
87
+ assert_equal 'foo: nil', response.body
88
+
89
+ get '/get_session_id'
90
+ assert_response :success
91
+ assert_not_equal session_id, response.body
92
+ end
93
+ end
94
+
95
+ def test_getting_session_id
96
+ with_test_route_set do
97
+ get '/set_session_value'
98
+ assert_response :success
99
+ assert cookies['_session_id']
100
+ session_id = cookies['_session_id']
101
+
102
+ get '/get_session_id'
103
+ assert_response :success
104
+ assert_equal session_id, response.body
105
+ end
106
+ end
107
+
108
+ def test_prevents_session_fixation
109
+ with_test_route_set do
110
+ get '/get_session_value'
111
+ assert_response :success
112
+ assert_equal 'foo: nil', response.body
113
+ session_id = cookies['_session_id']
114
+
115
+ reset!
116
+
117
+ get '/set_session_value', :_session_id => session_id
118
+ assert_response :success
119
+ assert_not_equal session_id, cookies['_session_id']
120
+ end
121
+ end
122
+
123
+ def self.build_app(routes = nil)
124
+ RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
125
+ middleware.use "ActionDispatch::Callbacks"
126
+ middleware.use "ActionDispatch::ParamsParser"
127
+ middleware.use "ActionDispatch::Cookies"
128
+ middleware.use "ActionDispatch::Flash"
129
+ middleware.use "ActionDispatch::Head"
130
+ yield(middleware) if block_given?
131
+ end
132
+ end
133
+
134
+ self.app = build_app
135
+
136
+ private
137
+ def with_test_route_set
138
+ with_routing do |set|
139
+ set.draw do |map|
140
+ match ':action', :to => ::MongoStoreTest::TestController
141
+ end
142
+ ::MongoStoreTest::TestController.class_eval do
143
+ include set.url_helpers
144
+ end
145
+
146
+
147
+ @app = self.class.build_app(set) do |middleware|
148
+ middleware.use ActionDispatch::Session::MongoStore, :key => '_session_id', :collection => COLLECTION
149
+ end
150
+
151
+ yield
152
+ end
153
+ end
154
+
155
+
156
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_sessions
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
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/mongo_sessions/mongo_store.rb
53
+ - lib/mongo_sessions/rack_mongo_store.rb
54
+ - lib/mongo_sessions/rails_mongo_store.rb
55
+ - test/helper.rb
56
+ - test/test_mongo_store.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/biilmann/mongo_sessions
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: MongoDB Session store for Rails and Rack
91
+ test_files:
92
+ - test/helper.rb
93
+ - test/test_mongo_store.rb