thumblemonks-sso_what 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 }
data/sso_what.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "sso_what"
3
- s.version = "0.2.0"
3
+ s.version = "0.2.1"
4
4
  s.date = "2009-03-05"
5
5
  s.summary = "Rails extension to help with cookies in a system with sub-domains"
6
6
  s.email = %w[gus@gusg.us gabriel.gironda@gmail.com]
@@ -23,15 +23,15 @@ Gem::Specification.new do |s|
23
23
  MIT-LICENSE
24
24
  README.markdown
25
25
  Rakefile
26
- lib/centro/base_host.rb
27
26
  lib/sso_what.rb
28
- lib/thumblemonks/subdomain_cookie_jar.rb
27
+ lib/thumblemonks/base_domain_cookie.rb
28
+ lib/thumblemonks/base_domain_session.rb
29
29
  sso_what.gemspec
30
30
  ]
31
31
 
32
32
  s.test_files = %w[
33
- test/base_host_test.rb
34
- test/subdomain_cookie_jar_test.rb
33
+ test/base_domain_cookie_test.rb
34
+ test/base_domain_session_test.rb
35
35
  test/test_helper.rb
36
36
  ]
37
37
 
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thumblemonks-sso_what
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Knowlden
@@ -29,9 +29,9 @@ files:
29
29
  - MIT-LICENSE
30
30
  - README.markdown
31
31
  - Rakefile
32
- - lib/centro/base_host.rb
33
32
  - lib/sso_what.rb
34
- - lib/thumblemonks/subdomain_cookie_jar.rb
33
+ - lib/thumblemonks/base_domain_cookie.rb
34
+ - lib/thumblemonks/base_domain_session.rb
35
35
  - sso_what.gemspec
36
36
  has_rdoc: true
37
37
  homepage: http://github.com/thumblemonks/sso_what
@@ -65,6 +65,6 @@ signing_key:
65
65
  specification_version: 2
66
66
  summary: Rails extension to help with cookies in a system with sub-domains
67
67
  test_files:
68
- - test/base_host_test.rb
69
- - test/subdomain_cookie_jar_test.rb
68
+ - test/base_domain_cookie_test.rb
69
+ - test/base_domain_session_test.rb
70
70
  - test/test_helper.rb