sinatra_warden 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,5 +19,6 @@ rdoc
19
19
  pkg
20
20
  bin
21
21
  vendor
22
+ .bundle
22
23
 
23
24
  ## PROJECT::SPECIFIC
data/Gemfile CHANGED
@@ -18,5 +18,5 @@ group :test do
18
18
  gem 'dm-core', '~> 0.10.1'
19
19
  gem 'bcrypt-ruby', :require => 'bcrypt'
20
20
  gem 'haml'
21
- gem 'rack-flash', :require => 'rack-flash'
21
+ gem 'rack-flash', '~> 0.1.1', :require => 'rack-flash'
22
22
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.1
@@ -56,7 +56,10 @@ module Sinatra
56
56
  #
57
57
  # @param [String] path to redirect to if user is unauthenticated
58
58
  def authorize!(failure_path=nil)
59
- redirect(failure_path ? failure_path : options.auth_failure_path) unless authenticated?
59
+ unless authenticated?
60
+ session[:return_to] = request.path if options.auth_use_referrer
61
+ redirect(failure_path ? failure_path : options.auth_failure_path)
62
+ end
60
63
  end
61
64
 
62
65
  end
@@ -69,6 +72,10 @@ module Sinatra
69
72
 
70
73
  app.set :auth_failure_path, '/'
71
74
  app.set :auth_success_path, '/'
75
+ # Setting this to true will store last request URL
76
+ # into a user's session so that to redirect back to it
77
+ # upon successful authentication
78
+ app.set :auth_use_referrer, false
72
79
 
73
80
  app.set :auth_error_message, "Could not log you in."
74
81
  app.set :auth_success_message, "You have logged in successfully."
@@ -80,6 +87,7 @@ module Sinatra
80
87
 
81
88
  app.post '/unauthenticated/?' do
82
89
  status 401
90
+ warden.custom_failure! if warden.config.failure_app == self.class
83
91
  env['x-rack.flash'][:error] = options.auth_error_message if defined?(Rack::Flash)
84
92
  options.auth_use_erb ? erb(options.auth_login_template) : haml(options.auth_login_template)
85
93
  end
@@ -107,7 +115,8 @@ module Sinatra
107
115
  app.post '/login/?' do
108
116
  authenticate
109
117
  env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
110
- redirect options.auth_success_path
118
+ redirect options.auth_use_referrer && session[:return_to] ? session.delete(:return_to) :
119
+ options.auth_success_path
111
120
  end
112
121
 
113
122
  app.get '/logout/?' do
@@ -49,3 +49,20 @@ class TestingLogin < Sinatra::Base
49
49
  end
50
50
 
51
51
  end
52
+
53
+ class TestingLoginWithReferrer < TestingLogin
54
+ set :auth_use_referrer, true
55
+ end
56
+
57
+ class TestingLoginAsRackApp < TestingLogin
58
+ use Rack::Session::Cookie
59
+ use Warden::Manager do |manager|
60
+ manager.default_strategies :password
61
+ manager.failure_app = TestingLoginAsRackApp
62
+ manager.serialize_into_session { |user| user.id }
63
+ manager.serialize_from_session { |id| User.get(id) }
64
+ end
65
+ use Rack::Flash
66
+
67
+ set :auth_failure_path, '/login'
68
+ end
@@ -27,6 +27,65 @@ describe "Sinatra::Warden" do
27
27
  get '/logout'
28
28
  last_request.env['warden'].authenticated?.should == false
29
29
  end
30
+
31
+ context "auth_use_referrer is disabled" do
32
+ it "should not store :return_to" do
33
+ get '/dashboard'
34
+ follow_redirect!
35
+ last_request.session[:return_to].should be_nil
36
+ end
37
+
38
+ it "should redirect to a default success URL" do
39
+ get '/dashboard'
40
+ follow_redirect!
41
+ post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
42
+ follow_redirect!
43
+ last_request.path.should == '/welcome'
44
+ end
45
+ end
46
+
47
+ context "when auth_use_referrer is set to true" do
48
+ def app; app_with_referrer; end
49
+
50
+ it "should store referrer in user's session" do
51
+ get '/dashboard'
52
+ follow_redirect!
53
+ last_request.session[:return_to].should == "/dashboard"
54
+ end
55
+
56
+ it "should redirect to stored return_to URL" do
57
+ get '/dashboard'
58
+ follow_redirect!
59
+ post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
60
+ follow_redirect!
61
+ last_request.path.should == '/dashboard'
62
+ end
63
+
64
+ it "should remove :return_to from session" do
65
+ get '/dashboard'
66
+ follow_redirect!
67
+ post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
68
+ follow_redirect!
69
+ last_request.session[:return_to].should be_nil
70
+ end
71
+
72
+ it "should default to :auth_success_path if there wasn't a return_to" do
73
+ post '/login', 'email' => 'justin.smestad@gmail.com', 'password' => 'thedude'
74
+ follow_redirect!
75
+ last_request.path.should == '/welcome'
76
+ end
77
+ end
78
+
79
+ context "TestingLoginAsRackApp" do
80
+ def app; @app ||= TestingLoginAsRackApp; end
81
+
82
+ # what happens here is you'll eventually get
83
+ # "stack too deep" error if the following test fails
84
+ it "should not get in a loop" do
85
+ post '/login', :email => 'bad', :password => 'password'
86
+ last_request.path.should == '/unauthenticated'
87
+ end
88
+ end
30
89
  end
31
90
 
32
91
  context "the helpers" do
@@ -23,8 +23,21 @@ Spec::Runner.configure do |config|
23
23
  DataMapper.auto_migrate!
24
24
  end
25
25
 
26
+ # default app
26
27
  def app
27
- @app ||= Rack::Builder.app do
28
+ @app ||= define_app TestingLogin
29
+ end
30
+
31
+ # app with auth_use_referrer enabled
32
+ def app_with_referrer
33
+ @app ||= define_app TestingLoginWithReferrer
34
+ end
35
+
36
+ private
37
+
38
+ # :which should be a sinatra app
39
+ def define_app(which)
40
+ Rack::Builder.app do
28
41
  use Rack::Session::Cookie
29
42
  use Warden::Manager do |manager|
30
43
  manager.default_strategies :password
@@ -33,7 +46,7 @@ Spec::Runner.configure do |config|
33
46
  manager.serialize_from_session { |id| User.get(id) }
34
47
  end
35
48
  use Rack::Flash
36
- run TestingLogin
49
+ run which
37
50
  end
38
51
  end
39
52
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 0
9
- version: 0.3.0
8
+ - 1
9
+ version: 0.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Justin Smestad
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-04-23 00:00:00 -06:00
18
+ date: 2010-04-25 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency