rack-auto-session-domain 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-auto-session-cookie-domain.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Gavin Joyce
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.
@@ -0,0 +1,24 @@
1
+ rack-auto-session-domain
2
+ ===============================
3
+
4
+ This rack middleware automatically sets the rack session domain to the current domain.
5
+
6
+ eg. env["rack.session.options"][:domain] = 'domain1.com'
7
+
8
+ This is handy if you want your rack app to serve multiple domains and have access to the session data on all subdomains. For example, let's say your app serves the following domains:
9
+
10
+ - www.domain1.com
11
+ - auth.domain1.com
12
+ - billing.domain1.com
13
+
14
+ and..
15
+
16
+ - www.domain2.co.uk
17
+ - auth.domain2.co.uk
18
+ - billing.domain2.co.uk
19
+
20
+ The session cookies will now automatically be accessible across all subdomains on *.domain1.com and *.domain2.co.uk respectively.
21
+
22
+ ## Source
23
+
24
+ https://github.com/GavinJoyce/rack-auto-session-domain
@@ -0,0 +1,4 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => [:spec]
@@ -0,0 +1,30 @@
1
+ require "rack-auto-session-domain/version"
2
+ require 'domainatrix'
3
+
4
+ module Rack
5
+ class AutoSessionDomain
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ env["rack.session.options"] ||= {}
12
+ host = env["SERVER_NAME"]
13
+
14
+ env["rack.session.options"][:domain] = Domainatrix.parse(host).domain_with_tld if valid_host?(host)
15
+
16
+ @app.call(env)
17
+ end
18
+
19
+ private
20
+
21
+ def valid_host?(host)
22
+ return false unless host.include? '.' #exclude hosts such as 'localhost'
23
+ return !is_numeric?(host.gsub(/\D/,'')) #exclude IP addresses
24
+ end
25
+
26
+ def is_numeric?(text)
27
+ true if Float(text) rescue false
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class AutoSessionDomain
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack-auto-session-domain/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rack-auto-session-domain"
8
+ gem.version = Rack::AutoSessionDomain::VERSION
9
+ gem.authors = ["Gavin Joyce"]
10
+ gem.email = ["gavinjoyce@gmail.com"]
11
+ gem.description = "Automatically sets the rack session domain to the current request domain"
12
+ gem.summary = "Automatically sets the rack session domain to the current request domain"
13
+ gem.homepage = "https://github.com/GavinJoyce/rack-auto-session-domain"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'domainatrix', '>= 0.0.11'
21
+ gem.add_development_dependency 'rack'
22
+ gem.add_development_dependency 'rack/test'
23
+ gem.add_development_dependency 'rspec', '~> 2.12'
24
+ end
@@ -0,0 +1,70 @@
1
+ require 'rspec'
2
+ require 'rack/test'
3
+ require 'rack-auto-session-domain'
4
+
5
+ RSpec.configure do |config|
6
+ config.color_enabled = true
7
+ config.formatter = 'documentation'
8
+ end
9
+
10
+ describe Rack::AutoSessionDomain do
11
+ include Rack::Test::Methods
12
+
13
+ let(:app) {
14
+ Rack::AutoSessionDomain.new(
15
+ lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'hello there'] }
16
+ )
17
+ }
18
+
19
+ it "should return a valid response" do
20
+ get '/'
21
+ last_response.should be_ok
22
+ last_response.body.should == 'hello there'
23
+ end
24
+
25
+ it "should ignore the subdomain" do
26
+ get 'http://www.incremental.ie/apps'
27
+ session_domain_from(last_request).should == 'incremental.ie'
28
+ end
29
+
30
+ it "should ignore multiple subdomains" do
31
+ get 'http://ballinteer.dublin.incremental.ie/apps'
32
+ last_request.env["rack.session.options"][:domain].should == 'incremental.ie'
33
+ end
34
+
35
+ it "should set the session domain when there is no subdomain" do
36
+ get 'http://incremental.ie/apps'
37
+ last_request.env["rack.session.options"][:domain].should == 'incremental.ie'
38
+ end
39
+
40
+ it "should ignore request port" do
41
+ get 'http://incremental.ie:8080/apps'
42
+ last_request.env["rack.session.options"][:domain].should == 'incremental.ie'
43
+ end
44
+
45
+ it "should ignore protocol" do
46
+ get 'https://www.incremental.ie/apps'
47
+ last_request.env["rack.session.options"][:domain].should == 'incremental.ie'
48
+ end
49
+
50
+ it "should work with country code second level domains" do
51
+ get 'http://www.bbc.co.uk/sport/0/'
52
+ last_request.env["rack.session.options"][:domain].should == 'bbc.co.uk'
53
+ end
54
+
55
+ it "should ignore localhost" do
56
+ get 'http://localhost/apps'
57
+ last_request.env["rack.session.options"][:domain].should == nil
58
+ end
59
+
60
+ it "should ignore ip addresses" do
61
+ get 'http://127.0.0.1/apps'
62
+ last_request.env["rack.session.options"][:domain].should == nil
63
+ end
64
+
65
+ private
66
+
67
+ def session_domain_from(request)
68
+ request.env["rack.session.options"][:domain]
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-auto-session-domain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gavin Joyce
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: domainatrix
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.11
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.11
30
+ - !ruby/object:Gem::Dependency
31
+ name: rack
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rack/test
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.12'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.12'
78
+ description: Automatically sets the rack session domain to the current request domain
79
+ email:
80
+ - gavinjoyce@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/rack-auto-session-domain.rb
91
+ - lib/rack-auto-session-domain/version.rb
92
+ - rack-auto-session-domain.gemspec
93
+ - spec/rack_auto_session_domain_spec.rb
94
+ homepage: https://github.com/GavinJoyce/rack-auto-session-domain
95
+ licenses: []
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.24
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Automatically sets the rack session domain to the current request domain
118
+ test_files:
119
+ - spec/rack_auto_session_domain_spec.rb