moped-session_store 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-session.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 JGW Maxwell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Moped::SessionStore
2
+
3
+ Moped::SessionStore is a highly performant MongoDB ActiveSupport Session Store implementation using the excellent new Moped driver from Mongoid 3.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'moped-session_store'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install moped-session_store
18
+
19
+ ## Usage
20
+
21
+ In *config/initializers/session_store.rb* set the session store as follows:
22
+
23
+ YourApp::Application.config.session_store :moped_store, :key => "_session_id", :collection => $a_moped_collection_object
24
+
25
+ The collection key is required, and currently it is required to be an existing Moped collection object, to avoid adding unneccessary additional connections to your MongoDB instance.
26
+
27
+ *MORE TO FOLLOW*
28
+
29
+ ## Contributing
30
+
31
+ This is in development at the moment, and there are some cool improvements to come regarding optimising the way that ActiveSupport deals with sessions. I'm always happy to accept contributions, and any new authors will be acknowledged fully for their contributions.
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake'
4
+
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/test_*.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ begin
13
+ require 'rcov/rcovtask'
14
+ Rcov::RcovTask.new do |test|
15
+ test.libs << 'test'
16
+ test.pattern = 'test/**/test_*.rb'
17
+ test.verbose = true
18
+ end
19
+ rescue LoadError
20
+ task :rcov do
21
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
22
+ end
23
+ end
24
+
25
+ task :default => :test
26
+
27
+ require 'rdoc/task'
28
+ Rake::RDocTask.new do |rdoc|
29
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
30
+
31
+ rdoc.rdoc_dir = 'rdoc'
32
+ rdoc.title = "moped-session #{version}"
33
+ rdoc.rdoc_files.include('README*')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
@@ -0,0 +1,10 @@
1
+ require 'moped/session_store'
2
+ require 'action_dispatch/middleware/session/abstract_store'
3
+
4
+ module ActionDispatch
5
+ module Session
6
+ class MopedStore < AbstractStore
7
+ include Moped::SessionStore
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,44 @@
1
+ module Moped
2
+ module SessionStore
3
+ module Base
4
+ def collection
5
+ @collection
6
+ end
7
+
8
+ # Sets up a new instance of Moped::Session::Store
9
+ def initialize(app, options = {})
10
+
11
+ unless options[:collection]
12
+ raise "To avoid creating unneccessary connections to MongoDB" +
13
+ "this Session Store will only reuse an existing Moped connection" +
14
+ "this behaviour WILL CHANGE in the future, but not yet"
15
+ end
16
+ @collection = options[:collection].respond_to?(:call) ? options[:collection].call : options[:collection]
17
+
18
+ super
19
+ end
20
+
21
+ private
22
+ def destroy(env)
23
+ if sid = current_session_id(env)
24
+ collection.remove({'_id' => sid})
25
+ end
26
+ end
27
+
28
+ def set_session(env, sid, session_data, options = {})
29
+ sid ||= generate_sid
30
+ collection.update({'_id' => sid}, {'_id' => sid, 't' => Time.now, 's' => pack(session_data), 'user_id' => session_data['user_id']}, {:upsert => true})
31
+ return 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,5 @@
1
+ module Moped
2
+ module SessionStore
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ require 'action_dispatch/session'
2
+ require 'rack/session/abstract/id'
3
+ require 'moped'
4
+ require 'moped/session_store/version'
5
+ require 'moped/session_store/base'
6
+
7
+ module Moped
8
+ module SessionStore
9
+ include Base
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/moped/session_store/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["JGW Maxwell"]
6
+ gem.email = ["jgwmaxwell@gmail.com"]
7
+ gem.description = %q{Moped::SessionStore is a performant ActiveSupport Session store implementation for MongoDB
8
+ using the latest Moped driver from Mongoid 3}
9
+ gem.summary = %q{A performant MongoDB session store on a Moped}
10
+ gem.homepage = "http://jgwmaxwell.com/open-source/moped-session_store"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "moped-session_store"
16
+ gem.require_paths = ["lib/moped/session_store", "lib"]
17
+ gem.version = Moped::SessionStore::VERSION
18
+
19
+ gem.add_dependency("moped", "~> 1.0.0.rc")
20
+ 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,144 @@
1
+ require 'rubygems'
2
+ require 'moped'
3
+ require 'action_dispatch'
4
+ require 'action_dispatch/testing/integration'
5
+ require 'helper'
6
+ require 'moped-session/moped-session'
7
+
8
+ class MopedSessionStoreTest < ActionController::integrationTest
9
+ class TestController < ActionController::Base
10
+ def no_session_access
11
+ head :ok
12
+ end
13
+
14
+ def set_session_value
15
+ session[:foo] = "bar"
16
+ head :ok
17
+ end
18
+
19
+ def get_session_value
20
+ render :text => "foo: #{session[:foo].inspect}"
21
+ end
22
+
23
+ def get_session_id
24
+ session[:foo]
25
+ render :text => "#{request.session_options[:id]}"
26
+ end
27
+
28
+ def call_reset_session
29
+ session[:bar]
30
+ reset_session
31
+ session[:bar] = "baz"
32
+ head :ok
33
+ end
34
+
35
+ def rescure_action(e) raise end
36
+ end
37
+
38
+ $COLLECTION = Moped::Session.new(["localhost:28017"])
39
+ $COLLECTION.use :moped_sessions_test
40
+ $COLLECTION = $COLLECTION[:sessions]
41
+
42
+ def test_setting_and_getting_session_value
43
+ with_test_route_set do
44
+ get '/set_session_value'
45
+ assert_response :success
46
+ assert cookies['_session_id']
47
+
48
+ get '/get_session_value'
49
+ assert_response :success
50
+ assert_equal 'foo: "bar"', response.body
51
+ end
52
+ end
53
+
54
+ def test_getting_nil_session_value
55
+ with_test_route_set do
56
+ get '/get_session_value'
57
+ assert_response :success
58
+ assert_equal 'foo: nil', response.body
59
+ end
60
+ end
61
+
62
+ def test_setting_session_value_after_session_reset
63
+ with_test_route_set do
64
+ get '/set_session_value'
65
+ assert_response :success
66
+ assert cookies['_session_id']
67
+ session_id = cookies['_session_id']
68
+
69
+ get '/call_reset_session'
70
+ assert_response :success
71
+ assert_not_equal [], header['Set-Cookie']
72
+
73
+ get '/get_session_value'
74
+ assert_response :success
75
+ assert_equal 'foo: nil', response.body
76
+
77
+ get '/get_session_id'
78
+ assert_response :success
79
+ assert_not_equal session_id, response.body
80
+ end
81
+ end
82
+
83
+ def test_getting_session_id
84
+ with_test_route_set do
85
+ get '/set_session_value'
86
+ assert_response :success
87
+ assert cookies['_session_id']
88
+ session_id = cookies['_session_id']
89
+
90
+ get '/get_session_id'
91
+ assert_response :success
92
+ assert_equal session_id, response.body
93
+ end
94
+ end
95
+
96
+ def test_prevents_session_fixation
97
+ with_test_route_set do
98
+ get '/get_session_value'
99
+ assert_response :success
100
+ assert_equal 'foo: nil', response.body
101
+ session_id = cookies['_session_id']
102
+
103
+ reset!
104
+
105
+ get '/set_session_value', :session_id => session_id
106
+ assert_response :success
107
+ assert_not_equal session_id, cookies['_session_id']
108
+ end
109
+ end
110
+
111
+ def self.build_app(routes = nil)
112
+ RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
113
+ middleware.use "ActionDispatch::Callbacks"
114
+ middleware.use "ActionDispatch::ParamsParser"
115
+ middleware.use "ActionDispatch::Cookies"
116
+ middleware.use "ActionDispatch::Flash"
117
+ middleware.use "ActionDispatch::Head"
118
+ yield(middleware) if block_given?
119
+ end
120
+ end
121
+
122
+ self.app = build_app
123
+
124
+ private
125
+ def test_with_route_set
126
+ with_routing do |set|
127
+ set.draw do |map|
128
+ match ':action' => ::MopedSessionStoreTest::TestController, :key => "_session_id", :collection => COLLECTION
129
+ end
130
+
131
+ ::MopedSessionStoreTest::TestController.class_eval do
132
+ include set.url_helpers
133
+ end
134
+
135
+
136
+ @app = self.class.build_app(set) do |middleware|
137
+ middleware.use ActionDispatch::Session::MopedStore, :key => "_session_id", :collection => $COLLECTION
138
+ end
139
+
140
+ yield
141
+ end
142
+ end
143
+
144
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moped-session_store
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - JGW Maxwell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: moped
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 1.0.0.rc
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0.rc
27
+ none: false
28
+ prerelease: false
29
+ type: :runtime
30
+ description: ! "Moped::SessionStore is a performant ActiveSupport Session store implementation\
31
+ \ for MongoDB\n using the latest Moped driver from Mongoid 3"
32
+ email:
33
+ - jgwmaxwell@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - !binary |-
39
+ LmdpdGlnbm9yZQ==
40
+ - !binary |-
41
+ R2VtZmlsZQ==
42
+ - !binary |-
43
+ TElDRU5TRQ==
44
+ - !binary |-
45
+ UkVBRE1FLm1k
46
+ - !binary |-
47
+ UmFrZWZpbGU=
48
+ - !binary |-
49
+ bGliL2FjdGlvbl9kaXNwYXRjaC9zZXNzaW9uLnJi
50
+ - !binary |-
51
+ bGliL21vcGVkL3Nlc3Npb25fc3RvcmUucmI=
52
+ - !binary |-
53
+ bGliL21vcGVkL3Nlc3Npb25fc3RvcmUvYmFzZS5yYg==
54
+ - !binary |-
55
+ bGliL21vcGVkL3Nlc3Npb25fc3RvcmUvdmVyc2lvbi5yYg==
56
+ - !binary |-
57
+ bW9wZWQtc2Vzc2lvbl9zdG9yZS5nZW1zcGVj
58
+ - !binary |-
59
+ dGVzdC9oZWxwZXIucmI=
60
+ - !binary |-
61
+ dGVzdC90ZXN0X21vbmdvaWRfc2Vzc2lvbl9zdG9yZS5yYg==
62
+ homepage: http://jgwmaxwell.com/open-source/moped-session_store
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib/moped/session_store
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: !binary |-
74
+ MA==
75
+ none: false
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: !binary |-
81
+ MA==
82
+ none: false
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: A performant MongoDB session store on a Moped
89
+ test_files:
90
+ - !binary |-
91
+ dGVzdC9oZWxwZXIucmI=
92
+ - !binary |-
93
+ dGVzdC90ZXN0X21vbmdvaWRfc2Vzc2lvbl9zdG9yZS5yYg==
94
+ ...