scottmotte-merb_auth_slice_multisite 0.8.3 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION.yml +1 -1
- data/config/init.rb +1 -0
- data/lib/merb-auth-remember-me/strategies/remember_me.rb +55 -12
- data/lib/merb_auth_slice_multisite.rb +2 -6
- data/spec/mixins/authenticated_user_spec.rb +33 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/strategies/remember_me_spec.rb +62 -0
- metadata +5 -2
data/VERSION.yml
CHANGED
data/config/init.rb
CHANGED
@@ -1,12 +1,55 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
1
|
+
class RememberMe < Merb::Authentication::Strategy
|
2
|
+
def run!
|
3
|
+
login_from_cookie
|
4
|
+
end
|
5
|
+
|
6
|
+
def current_user
|
7
|
+
@current_user
|
8
|
+
end
|
9
|
+
|
10
|
+
def current_user=(new_user)
|
11
|
+
@current_user = new_user
|
12
|
+
end
|
13
|
+
|
14
|
+
# Called from #current_user. Finaly, attempt to login by an expiring token in the cookie.
|
15
|
+
# for the paranoid: we _should_ be storing user_token = hash(cookie_token, request IP)
|
16
|
+
def login_from_cookie
|
17
|
+
current_user = cookies[:auth_token] && Merb::Authentication.user_class.first(:conditions => ["remember_token = ?", cookies[:auth_token]])
|
18
|
+
if current_user && current_user.remember_token?
|
19
|
+
handle_remember_cookie! false # freshen cookie token (keeping date)
|
20
|
+
current_user
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Remember_me Tokens
|
26
|
+
#
|
27
|
+
# Cookies shouldn't be allowed to persist past their freshness date,
|
28
|
+
# and they should be changed at each login
|
29
|
+
|
30
|
+
# Cookies shouldn't be allowed to persist past their freshness date,
|
31
|
+
# and they should be changed at each login
|
32
|
+
|
33
|
+
def valid_remember_cookie?
|
34
|
+
return nil unless current_user
|
35
|
+
(current_user.remember_token?) &&
|
36
|
+
(cookies[:auth_token] == current_user.remember_token)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Refresh the cookie auth token if it exists, create it otherwise
|
40
|
+
def handle_remember_cookie! new_cookie_flag
|
41
|
+
return unless current_user
|
42
|
+
case
|
43
|
+
when valid_remember_cookie? then current_user.refresh_token # keeping same expiry date
|
44
|
+
when new_cookie_flag then current_user.remember_me
|
45
|
+
else current_user.forget_me
|
46
|
+
end
|
47
|
+
send_remember_cookie!
|
48
|
+
end
|
49
|
+
|
50
|
+
def send_remember_cookie!
|
51
|
+
cookies.set_cookie(:auth_token, current_user.remember_token, :expires => current_user.remember_token_expires_at.to_time)
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
end
|
@@ -54,13 +54,9 @@ if defined?(Merb::Plugins)
|
|
54
54
|
Merb::Authentication.after_authentication do |user,request,params|
|
55
55
|
if params[:remember_me] == "1"
|
56
56
|
user.remember_me
|
57
|
-
request.cookies.set_cookie(
|
58
|
-
:auth_token,
|
59
|
-
user.remember_token,
|
60
|
-
:expires => user.remember_token_expires_at.to_time
|
61
|
-
)
|
57
|
+
request.cookies.set_cookie(:auth_token, user.remember_token, :expires => user.remember_token_expires_at.to_time)
|
62
58
|
end
|
63
|
-
user
|
59
|
+
user
|
64
60
|
end # Merb::Authentication.after_authentication
|
65
61
|
end
|
66
62
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "Authenticated user" do
|
4
|
+
|
5
|
+
before :all do
|
6
|
+
@user = User.new(valid_user_attributes)
|
7
|
+
@user.remember_token_expires_at.should be_nil
|
8
|
+
@user.remember_token.should be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should add the 'remember_token_expires_at' property to the user model" do
|
12
|
+
@user.should respond_to(:remember_token_expires_at)
|
13
|
+
@user.should respond_to(:remember_token_expires_at=)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should add the 'remember_token' property to the user model" do
|
17
|
+
@user.should respond_to(:remember_token)
|
18
|
+
@user.should respond_to(:remember_token=)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should save token and expires_at" do
|
22
|
+
@user.remember_me
|
23
|
+
@user.remember_token_expires_at.should_not be_nil
|
24
|
+
@user.remember_token.should_not be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should save expires_at as 2 weeks later" do
|
28
|
+
@user.remember_me
|
29
|
+
@user.remember_token_expires_at.should eql((Time.now+2.weeks).to_datetime)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "Remember me strategy" do
|
4
|
+
def do_valid_login
|
5
|
+
put("/login", {:remember_me => "1", :pass_auth => true})
|
6
|
+
end
|
7
|
+
|
8
|
+
def do_valid_login_without_remember_me
|
9
|
+
put("/login", {:pass_auth => true})
|
10
|
+
end
|
11
|
+
|
12
|
+
def do_invalid_login
|
13
|
+
put("/login", { :pass_auth => false})
|
14
|
+
end
|
15
|
+
|
16
|
+
def do_home_with_auth_token
|
17
|
+
get("/", { :pass_auth => true} ) do |controller|
|
18
|
+
controller.request.cookies[:auth_token] = "auth_token_string"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
before :each do
|
23
|
+
@user = mock(Merb::Authentication.user_class, :remember_me => true)
|
24
|
+
User.stub!(:first).and_return(@user)
|
25
|
+
@user.stub!(:remember_token?).and_return(true)
|
26
|
+
@user.stub!(:remember_token).and_return(Time.now + 1.week)
|
27
|
+
@user.stub!(:remember_token_expires_at).and_return(Time.now)
|
28
|
+
@user.stub!(:forget_me).and_return(true)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should save remember_token and remember_token_expires_at if remember_me == '1'" do
|
32
|
+
Merb::Authentication.user_class.should_receive(:first).and_return(@user)
|
33
|
+
@user.should_receive(:remember_me)
|
34
|
+
@user.remember_token.should_not be_nil
|
35
|
+
@user.remember_token_expires_at.should_not be_nil
|
36
|
+
do_valid_login.should redirect_to('/')
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not remember me unless remember_me == '1'" do
|
40
|
+
Merb::Authentication.user_class.should_receive(:first).and_return(true)
|
41
|
+
@user.should_not_receive(:remember_me)
|
42
|
+
do_valid_login_without_remember_me.should redirect_to('/')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should log in automatically if auth_token exists" do
|
46
|
+
Merb::Authentication.user_class.should_receive(:first).and_return(@user)
|
47
|
+
do_home_with_auth_token.should be_successful
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should raise unauthenticated if auth_token doesn't exist" do
|
51
|
+
lambda do
|
52
|
+
do_invalid_login
|
53
|
+
end.should raise_error(Merb::Controller::Unauthenticated, "Could not log in")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should clear auth_token after loging out" do
|
57
|
+
delete('/logout') do |controller|
|
58
|
+
controller.cookies.should_receive(:delete).with(:auth_token)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scottmotte-merb_auth_slice_multisite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- scottmotte
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-09 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -47,10 +47,13 @@ files:
|
|
47
47
|
- spec/mailers
|
48
48
|
- spec/mailers/send_password_mailer_spec.rb
|
49
49
|
- spec/mixins
|
50
|
+
- spec/mixins/authenticated_user_spec.rb
|
50
51
|
- spec/mixins/user_belongs_to_site_spec.rb
|
51
52
|
- spec/models
|
52
53
|
- spec/models/site_spec.rb
|
53
54
|
- spec/spec_helper.rb
|
55
|
+
- spec/strategies
|
56
|
+
- spec/strategies/remember_me_spec.rb
|
54
57
|
- app/controllers
|
55
58
|
- app/controllers/application.rb
|
56
59
|
- app/controllers/exceptions.rb
|