sso_what 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2008 {Centro}[www.centro.net]
2
+
3
+ Authored by:
4
+
5
+ {Gabriel Gironda}[gabriel.gironda@gmail.com]
6
+ {Josh Davison}[josh.davison@centro.net]
7
+ {Justin Knowlden}[gus@gusg.us]
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining
10
+ a copy of this software and associated documentation files (the
11
+ "Software"), to deal in the Software without restriction, including
12
+ without limitation the rights to use, copy, modify, merge, publish,
13
+ distribute, sublicense, and/or sell copies of the Software, and to
14
+ permit persons to whom the Software is furnished to do so, subject to
15
+ the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be
18
+ included in all copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # SSO What
2
+
3
+ SSO What enables your Rails app to function well with sub-domains solely in the context of managing cookies. SSO What has to do two things (which makes us angry since it should only be one):
4
+
5
+ 1. Support a dynamic domain that works across sub-domains for session cookies
6
+ 2. Support a cookie domain that works across sub-domains for all cookies other than a session cookie
7
+
8
+ ### Session Cookies
9
+
10
+ SSO What is used to generate a session domain for single sign-on which works regardless of sub-domains and different hosts in different environments. So. If you have two servers, `kill.for.thrills.hypo.luxa` and `thrill.olympics.hypo.luxa`, the session domain would resolve to `.hypo.luxa`.
11
+
12
+ If your staging environment uses `kill.for.thrills.alien.jourgensen` it will automatically use `.alien.jourgensen` without further configuration.
13
+
14
+ To enable this feature set the session options on `ActionController` using the following line in your environment file:
15
+
16
+ config.action_controller.session = {:base_domain => true}
17
+
18
+ If you want to set a specific domain for all session cookies, this has nothing to do with SSO What. But, you can use built-in Rails behavior and do this in your environment file:
19
+
20
+ config.action_controller.session = {:domain => 'thrill.olympics.hypo.luxa'}
21
+
22
+ ### All other cookies
23
+
24
+ SSO What will force (for now since it's not configurable) all cookies that you set without an explicit domain to work across sub-domains. For example, if the domain your app is serving is `foo.example.com` and you set a cookie named `bar` but don't provide a domain, SSO What will kick in and set the domain to `.example.com`. Without SSO What, the domain would be `foo.example.com`.
25
+
26
+ If you provide an explicit domain with your cookie, SSO What just watches everything go by. It may cry a little, though :(
27
+
28
+ # Requirements
29
+
30
+ Shoulda and Mocha are required to run the tests.
31
+
32
+ ## License
33
+
34
+ Copyright (c) 2008 {Centro}[www.centro.net], released under the MIT license.
35
+
36
+ Authored by:
37
+
38
+ {Gabriel Gironda}[gabriel.gironda@gmail.com]
39
+ {Josh Davison}[josh.davison@centro.net]
40
+ {Justin Knowlden}[gus@gusg.us]
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the sso_what plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the sso_what plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'SsoWhat'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,2 @@
1
+ require 'thumblemonks/base_domain_session'
2
+ require 'thumblemonks/base_domain_cookie'
@@ -0,0 +1,27 @@
1
+ module ThumbleMonks
2
+ module SsoWhat
3
+ module BaseDomainCookie
4
+ def self.included(klass)
5
+ klass.alias_method_chain :set_cookie, :domain_override
6
+ end
7
+
8
+ def set_cookie_with_domain_override(key, value)
9
+ value = {:value => value} unless value.is_a?(Hash)
10
+ domain_requested, host = value[:domain], request.host
11
+ unless domain_requested || host_has_no_tld?(host)
12
+ domain_requested = host.gsub(/^(.*\.)?([a-z0-9-]+\.[a-z]+)$/i, '\2')
13
+ value[:domain] = ".#{domain_requested}"
14
+ end
15
+ set_cookie_without_domain_override(key, value)
16
+ end
17
+
18
+ private
19
+
20
+ def host_has_no_tld?(host)
21
+ host =~ /^[a-z0-9-]+$/i
22
+ end
23
+ end # DomainOverride
24
+ end # SsoWhat
25
+ end # ThumbleMonks
26
+
27
+ Rack::Response.instance_eval { include ThumbleMonks::SsoWhat::BaseDomainCookie }
@@ -0,0 +1,19 @@
1
+ module Centro
2
+ module SsoWhat
3
+ module BaseDomainSession
4
+ def self.included(klass)
5
+ klass.alias_method_chain :call, :domain_override
6
+ end
7
+
8
+ def call_with_domain_override(env)
9
+ if @default_options[:base_domain]
10
+ base_host = env["HTTP_HOST"].scan(/[0-9a-z-]+\.[0-9a-z-]+(?=:|$)/i).first
11
+ @default_options[:domain] = base_host ? ".#{base_host}" : base_host
12
+ end
13
+ call_without_domain_override(env)
14
+ end
15
+ end # MultiDomainSession
16
+ end # AbstractStore
17
+ end # Centro
18
+
19
+ ActionController::Session::AbstractStore.instance_eval { include Centro::SsoWhat::BaseDomainSession }
@@ -0,0 +1,39 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "sso_what"
3
+ s.version = "0.2.1"
4
+ s.date = "2009-03-05"
5
+ s.summary = "Rails extension to help with cookies in a system with sub-domains"
6
+ s.email = %w[gus@gusg.us gabriel.gironda@gmail.com]
7
+ s.homepage = "http://github.com/thumblemonks/sso_what"
8
+ s.description = "Rails extension to help with cookies in a system with sub-domains"
9
+ s.authors = %w[Justin\ Knowlden Gabriel\ Gironda]
10
+
11
+ s.rubyforge_project = %q{sso_what}
12
+
13
+ s.has_rdoc = true
14
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "SSO What", "--main", "README.markdown"]
15
+ s.extra_rdoc_files = %w[README.markdown MIT-LICENSE]
16
+
17
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to?(:required_rubygems_version=)
18
+ s.rubygems_version = "1.3.1"
19
+ s.require_paths = ["lib"]
20
+
21
+ # run git ls-files to get an updated list
22
+ s.files = %w[
23
+ MIT-LICENSE
24
+ README.markdown
25
+ Rakefile
26
+ lib/sso_what.rb
27
+ lib/thumblemonks/base_domain_cookie.rb
28
+ lib/thumblemonks/base_domain_session.rb
29
+ sso_what.gemspec
30
+ ]
31
+
32
+ s.test_files = %w[
33
+ test/base_domain_cookie_test.rb
34
+ test/base_domain_session_test.rb
35
+ test/test_helper.rb
36
+ ]
37
+
38
+ s.post_install_message = %q{Choosy ministries choose Thumble Monks}
39
+ end
@@ -0,0 +1,50 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class BaseDomainCookieTest < Test::Unit::TestCase
4
+ def setup
5
+ # @response = OpenStruct.new(:headers => { "Set-Cookie" => [] })
6
+ # @controller = OpenStruct.new(:request => @request, :response => @response)
7
+ @test_request = OpenStruct.new
8
+ @response = Rack::Response.new
9
+ @response.stubs(:request).returns(@test_request)
10
+ end
11
+
12
+ context "if domain is provided" do
13
+ should "not do anyhing" do
14
+ @response.set_cookie("fanta", {:value => "wanna fanta", :domain => 'foo.bar'})
15
+ assert_equal 'domain=foo.bar', domain_for_the_first_cookie_found
16
+ end
17
+ end
18
+
19
+ context "if domain is not provided" do
20
+ context "and value is a string" do
21
+ should "set the domain to request.host but with subdomain support" do
22
+ @test_request.expects(:host).returns('foo-bar.baz')
23
+ @response.set_cookie("fanta", "wanna fanta")
24
+ assert_equal 'domain=.foo-bar.baz', domain_for_the_first_cookie_found
25
+ end
26
+ end
27
+
28
+ should "set the domain to request.host but with subdomain support" do
29
+ @test_request.expects(:host).returns('foo-bar.baz')
30
+ @response.set_cookie("fanta", {:value => "wanna fanta"})
31
+ assert_equal 'domain=.foo-bar.baz', domain_for_the_first_cookie_found
32
+ end
33
+
34
+ should "remove subdomains" do
35
+ @test_request.expects(:host).returns('thomas.f00.bar')
36
+ @response.set_cookie("fanta", {:value => "wanna fanta"})
37
+ assert_equal 'domain=.f00.bar', domain_for_the_first_cookie_found
38
+ end
39
+
40
+ should "do nothing for one word hosts" do
41
+ @test_request.expects(:host).returns('localhost')
42
+ @response.set_cookie("fanta", {:value => "wanna fanta"})
43
+ assert_nil domain_for_the_first_cookie_found
44
+ end
45
+ end
46
+
47
+ def domain_for_the_first_cookie_found
48
+ @response["Set-Cookie"].scan(/domain=[a-z0-9.-]+/i).first
49
+ end
50
+ end
@@ -0,0 +1,60 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class BaseDomainSessionTest < Test::Unit::TestCase
4
+ # Macros
5
+
6
+ def self.store_with_multi_domain(multi_domain_option, &block)
7
+ context "session store with multi-domain set to #{multi_domain_option.inspect}" do
8
+ setup do
9
+ @response = [nil, {"Set-Cookie" => nil}] # second element is the header
10
+ @app = stub(:call => @response)
11
+ options = {:base_domain => multi_domain_option, :expire_after => 3600}
12
+ @store = TestSessionStore.new(@app, options)
13
+ end
14
+ yield if block_given?
15
+ end
16
+ end
17
+
18
+ def self.should_expect_cookie_domain_for_http_host(http_host, cookie_domain)
19
+ should "return #{cookie_domain} for #{http_host}" do
20
+ env = {"HTTP_HOST" => http_host}
21
+ @store.call(env)
22
+ expected = cookie_domain.nil? ? nil : "domain=#{cookie_domain}"
23
+ assert_equal expected, domain_for_the_first_cookie_found
24
+ end
25
+ end
26
+
27
+ def domain_for_the_first_cookie_found
28
+ set_cookie = @response[1]["Set-Cookie"]
29
+ set_cookie && set_cookie.scan(/domain=[a-z0-9.-]+/i).first
30
+ end
31
+
32
+ # Tests
33
+
34
+ store_with_multi_domain(true) do
35
+ should_expect_cookie_domain_for_http_host "foohost", nil
36
+ should_expect_cookie_domain_for_http_host "foohost.bar", ".foohost.bar"
37
+ should_expect_cookie_domain_for_http_host "gah.foohost.bar", ".foohost.bar"
38
+ should_expect_cookie_domain_for_http_host "publisher.gah.foohost.bar", ".foohost.bar"
39
+ should_expect_cookie_domain_for_http_host "gah.foohost.bar:3000", ".foohost.bar"
40
+ end
41
+
42
+ store_with_multi_domain(false) do
43
+ should_expect_cookie_domain_for_http_host "gah.foohost.bar", nil
44
+ end
45
+
46
+ store_with_multi_domain(nil) do
47
+ should_expect_cookie_domain_for_http_host "gah.foohost.bar", nil
48
+ end
49
+
50
+ end
51
+
52
+ class TestSessionStore < ActionController::Session::AbstractStore
53
+ def get_session(env, sid)
54
+ [sid, {:foo => "bar"}]
55
+ end
56
+
57
+ def set_session(env, sid, session_data)
58
+ true
59
+ end
60
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+ require 'ostruct'
3
+ require 'rubygems'
4
+ require 'shoulda'
5
+ require 'mocha'
6
+ require 'action_controller'
7
+
8
+ require 'sso_what'
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sso_what
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Justin Knowlden
8
+ - Gabriel Gironda
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-03-05 00:00:00 -06:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Rails extension to help with cookies in a system with sub-domains
18
+ email:
19
+ - gus@gusg.us
20
+ - gabriel.gironda@gmail.com
21
+ executables: []
22
+
23
+ extensions: []
24
+
25
+ extra_rdoc_files:
26
+ - README.markdown
27
+ - MIT-LICENSE
28
+ files:
29
+ - MIT-LICENSE
30
+ - README.markdown
31
+ - Rakefile
32
+ - lib/sso_what.rb
33
+ - lib/thumblemonks/base_domain_cookie.rb
34
+ - lib/thumblemonks/base_domain_session.rb
35
+ - sso_what.gemspec
36
+ has_rdoc: true
37
+ homepage: http://github.com/thumblemonks/sso_what
38
+ licenses: []
39
+
40
+ post_install_message: Choosy ministries choose Thumble Monks
41
+ rdoc_options:
42
+ - --line-numbers
43
+ - --inline-source
44
+ - --title
45
+ - SSO What
46
+ - --main
47
+ - README.markdown
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "1.2"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project: sso_what
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Rails extension to help with cookies in a system with sub-domains
69
+ test_files:
70
+ - test/base_domain_cookie_test.rb
71
+ - test/base_domain_session_test.rb
72
+ - test/test_helper.rb