Skip to content

Commit 44830ea

Browse files
rizwanrezajosevalim
authored andcommitted
Add support for multi-subdomain session by setting cookie host in session cookie so you can share session between www.example.com, example.com and user.example.com. [rails#4818 state:resolved]
This reverts commit 330a890.
1 parent b69a2db commit 44830ea

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

actionpack/CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
Rails 3.0.0 [Release Candidate] (unreleased)*
1+
*Rails 3.0.0 [Release Candidate] (unreleased)*
2+
3+
* Add support for multi-subdomain session by setting cookie host in session cookie so you can share session between www.example.com, example.com and user.example.com. #4818 [Guillermo Álvarez]
24

35
* Removed textilize, textilize_without_paragraph and markdown helpers. [Santiago Pastorino]
46

7+
58
*Rails 3.0.0 [beta 4] (June 8th, 2010)*
69

710
* Remove middleware laziness [José Valim]

actionpack/lib/action_dispatch/middleware/session/abstract_store.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ def call(env)
121121
unless options[:expire_after].nil?
122122
cookie[:expires] = Time.now + options.delete(:expire_after)
123123
end
124-
124+
125+
if options[:domain] == :all
126+
top_level_domain = env["HTTP_HOST"].split('.')[-2..-1].join('.')
127+
options[:domain] = ".#{top_level_domain}"
128+
end
129+
125130
request = ActionDispatch::Request.new(env)
126131
set_cookie(request, cookie.merge!(options))
127132
end

actionpack/lib/action_dispatch/middleware/session/cookie_store.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ module Session
3434
# integrity defaults to 'SHA1' but may be any digest provided by OpenSSL,
3535
# such as 'MD5', 'RIPEMD160', 'SHA256', etc.
3636
#
37+
# * <tt>:domain</tt>: Restrict the session cookie to certain domain level.
38+
# If you use a schema like www.example.com and wants to share session
39+
# with user.example.com set <tt>:domain</tt> to <tt>:all</tt>
40+
#
41+
# :domain => nil # Does not sets cookie domain. (default)
42+
# :domain => :all # Allow the cookie for the top most level
43+
# domain and subdomains.
44+
#
3745
# To generate a secret key for an existing application, run
3846
# "rake secret" and set the key in config/environment.rb.
3947
#

actionpack/test/dispatch/session/cookie_store_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,35 @@ def test_session_store_with_expire_after
185185
end
186186
end
187187

188+
def test_session_store_with_explicit_domain
189+
with_test_route_set(:domain => "example.es") do
190+
get '/set_session_value'
191+
assert_match /domain=example\.es/, headers['Set-Cookie']
192+
headers['Set-Cookie']
193+
end
194+
end
195+
196+
def test_session_store_without_domain
197+
with_test_route_set do
198+
get '/set_session_value'
199+
assert_no_match /domain\=/, headers['Set-Cookie']
200+
end
201+
end
202+
203+
def test_session_store_with_nil_domain
204+
with_test_route_set(:domain => nil) do
205+
get '/set_session_value'
206+
assert_no_match /domain\=/, headers['Set-Cookie']
207+
end
208+
end
209+
210+
def test_session_store_with_all_domains
211+
with_test_route_set(:domain => :all) do
212+
get '/set_session_value'
213+
assert_match /domain=\.example\.com/, headers['Set-Cookie']
214+
end
215+
end
216+
188217
private
189218

190219
# Overwrite get to send SessionSecret in env hash

0 commit comments

Comments
 (0)