rack_warden 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MTE1MjJmZTU3OGMwMTA4MjQ5Y2M4OGMyMTRiMWNhOTJlNmQyYzYwOQ==
4
+ YmEzNjc1YzgzMjZmMzViZDIxNDliOTAzNDZlMzBkYmUzZTdlMDY4ZA==
5
5
  data.tar.gz: !binary |-
6
- ODg1YTBmMGJjNWJiZWNkZjM3MTYwMTUyMjQ5ZjM0MTkwZmQwNDYxMg==
6
+ NjhlZGE3ZWY4OWI2MzM2MWVhNzY2YTZiNjcxOGI2ZDZkMjRlZDcxZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTY0MjcwMjdjZGUxODM3ODcxYWRhYzFkMzZlMzk2MWM2MTE3NDI1M2Y1Zjcy
10
- N2VhZGEyMjE4NzEyYTYwZDRlMTk5ODQ5OTI1MjdlMDFlNzJjMjM5OWViNGM1
11
- MjZjZDAwYTQzNWNlZDUwYzRkODQzNDU1MGFjYmY4YzY4NjRkMmU=
9
+ ZmU0NTYxZjc0ZDhjMDhmYWFkMzIzMDU3M2ZjYWI1MDhlMmExNTZhYjY5Mzk3
10
+ Njk0ZjA5OTdiYjZmODFiMGVkNThkYjYxODEzZDlkNDZiNTEwMGQzNGFiZGJj
11
+ OTIxOTQ0YjJjZWU3ZjRjYmJiZDE2MmFlYmI4OWY1M2ZhYWM5MzU=
12
12
  data.tar.gz: !binary |-
13
- MjhlMWM4Y2RmZjMzYjdiOWE4MGZmNzhjMjU4MjU3N2ZlNTk2ZjYzYzM2ZDI0
14
- NTY1M2I5NGRmNTE1NDM1YjBhMDFkZWQ4NWJjNGMwNGI0YWE3YTE3ZGY2OTc5
15
- OTI4N2I4ZTA3NTMwMjMxMWJkM2VhNjkzOTY4Nzk4NzkzYmEwZWQ=
13
+ MGVkMzY5ODI2ZGYwZDg4NzNmNzBjMzI3M2VmZDBkMzIzMmRmNDY1MWU1Mzg5
14
+ NDI0NzExODY3NjM1Y2E1MDljYzI3MGU2ZGFlMjgzZjU5Y2Q1ZDU2NzllNzhj
15
+ NzA4MDE3NjBhNGU2NzhhYWQzZDVhMDIzYmM5YjI4ZjQ2NzU4MWI=
data/.gitignore CHANGED
@@ -18,4 +18,5 @@ mkmf./log
18
18
  todo.txt
19
19
  *.bak
20
20
  *.gem
21
+ rack_warden.y*ml
21
22
 
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # RackWarden
2
2
 
3
- RackWarden is a rack middleware mini-app that provides user authentication and management to any rack-based app. RackWarden uses Sinatra for the mini-app, Warden for authentication, and DataMapper for database connections.
3
+ RackWarden is a rack middleware mini-app that provides user authentication and management to any rack-based app (currently supports Sinatra and Rails, with more on the way). This project is in its infancy. It is currently a great starter to get you going with plug-in authentication for your ruby app. Over time it will grow into a more fully featured package while maintaining a focus on simplicity, modularity, and transparency.
4
4
 
5
- RackWarden is in its infancy. It's currently a great starter to get you going with plug-in authentication for your ruby app. Over time it will grow into a more fully featured package while maintaining a focus on simplicity and transparency.
5
+ RackWarden uses Sinatra for the mini-app, Warden for authentication, and DataMapper for database connections. It is based on the sinatra-warden-example at https://github.com/sklise/sinatra-warden-example. If you are new to warden or Sinatra, I highly recommend downloading and experimenting with that example.
6
6
 
7
- RackWarden is based on the sinatra-warden-example at https://github.com/sklise/sinatra-warden-example. If you're new to warden and/or sinatra, I highly recommend downloading and experimenting with that example.
7
+ My goal in developing this software is to have drop-in authentication containing most of the features you see in user/account management sections of a typical web site. But I don't want to be strapped by this module in any of my projects, so it must be customizable. Or rather, overridable. The basics of this flexibility are already in place, and it will be a central theme throughout. See below for examples on overriding and customizing RackWarden.
8
8
 
9
9
 
10
10
  ## Installation
@@ -29,7 +29,7 @@ A few simple steps will have your entire app protected.
29
29
  ### Sinatra
30
30
 
31
31
  class MySinatraApp < Sinatra::Base
32
- use RackWarden
32
+ use RackWarden::App
33
33
 
34
34
  before do
35
35
  require_login
@@ -44,20 +44,45 @@ A few simple steps will have your entire app protected.
44
44
 
45
45
  application.rb or environment.rb
46
46
 
47
- config.middleware.use RackWarden
47
+ config.middleware.use RackWarden::App
48
48
 
49
49
  application-controller.rb
50
50
 
51
51
  before_filter :require_login
52
52
 
53
- ### Others...
54
53
 
55
- ## How it works
56
54
 
57
- ...
55
+ ## Configuration
56
+
57
+ Pass configuration settings to RackWarden through your ``use`` method. The params hash will be translated directly to the app's settings. You can currently specify :database, :views, and :default_route. You can also specify any of the standard Sinatra settings, like :views.
58
+
59
+ If you pass a block with the ``use`` method, the block will be evaluated in the context of the RackWarden::App class. Anything you do in that block is just as if you were writing code in the app class itself. While in the block, you also have access to two relevant objects.
60
+
61
+ use RackWarden::App do |rack_warden_app_instance, parent_app_instance|
62
+ set :somesetting, 'some_value'
63
+ end
64
+
65
+
66
+ ## Customization
67
+
68
+ To customize RackWarden for your specific project, you can set :views to point to a directory within your project. Then create templates that match the names of RackWarden templates, and they will be picked up and rendered. RackWarden looks for templates at the top level of your views directory as a default. You can change or add to this when you define the middleware in your project.
69
+
70
+ use RackWarden::App, :views => File.join(Dir.pwd, 'app/views/rack_warden')
71
+
72
+ Or if you simply want RackWarden to use your own custom layout, pass it a file path in the :layout parameter.
73
+
74
+ use RackWarden::App, :layout => :'layouts/rack_warden_layout.html'
75
+
76
+ Just remember that RackWarden is Sinatra, and any templates you pass must use Sinatra-specific code. For example, Sinatra uses ``url`` instead of Rails' ``url_for``. Also remember that template names in Sinatra must always be symbols.
77
+
78
+ Another way to customize RackWarden is to override its classes and methods, as you would with any other ruby code.
79
+
80
+ And if you want to customize RackWarden more extensively, you can always download the source from github and directly modify the app file and templates. Then point to this modified gem in your project Gemfile.
81
+
82
+ gem 'rack_warden', :path => "../RackWarden/"
83
+
84
+
58
85
 
59
- ## Customizing
60
86
 
61
- ...
62
87
 
63
88
 
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
- require './lib/rack_warden'
3
+ require File.expand_path('../lib/rack_warden', __FILE__)
4
4
 
5
5
  desc "Get version"
6
6
  task :version do
data/config.ru CHANGED
@@ -1,8 +1,11 @@
1
+ ::RACK_WARDEN_STANDALONE = true
2
+
1
3
  require 'bundler'
2
4
 
3
5
  Bundler.require
4
6
 
7
+ require File.expand_path('../lib/rack_warden', __FILE__)
5
8
 
6
- require File.join(File.dirname(__FILE__), 'rack_warden')
7
-
8
- run RackWarden
9
+ map ENV['BASE_URI'] || '/' do
10
+ run RackWarden::App
11
+ end
data/lib/rack_warden.rb CHANGED
@@ -13,6 +13,6 @@ require "rack_warden/app"
13
13
  require "rack_warden/model"
14
14
  require "rack_warden/version"
15
15
 
16
- # module RackWarden
17
- # # Your code goes here...
18
- # end
16
+ module RackWarden
17
+ # Your code goes here...
18
+ end
@@ -1,232 +1,248 @@
1
1
  # require 'bundler'
2
2
  # Bundler.require
3
3
 
4
- class RackWarden < Sinatra::Base
5
- enable :sessions
6
- register Sinatra::Flash
7
-
8
- set :layout, :'rack_warden_layout.html'
9
- set :default_route, '/'
10
- set :database, "sqlite://#{Dir.pwd}/rack_warden.sqlite.db"
4
+ module RackWarden
5
+ class App < Sinatra::Base
6
+ enable :sessions
7
+ register Sinatra::Flash
8
+ set :config_files, [ENV['RACK_WARDEN_CONFIG_FILE'], 'rack_warden.yml', 'rack_warden.yaml'].compact.uniq
9
+ set :layout, :'rack_warden_layout.html'
10
+ set :default_route, '/'
11
+ set :database, "sqlite://#{Dir.pwd}/rack_warden.sqlite.db"
12
+
13
+ # Load config from file, if any exist.
14
+ Hash.new.tap do |hash|
15
+ config_files.each {|c| hash.merge! Psych.load_file(c) rescue nil}
16
+ set hash
17
+ end
11
18
 
12
19
 
13
- # WBR - This will receive params and a block from the parent "use" statement.
14
- # This middleware app has been modified to process the parent use-block in
15
- # the context of the RackWarden class. So you can set settings on RackWarden,
16
- # when you call "use RackWarden"
17
- # Example:
18
- #
19
- # use RackWarden :layout=>:'my_layout' do |rack_warden_instance, parent_app_instance|
20
- # set :myvar, 'something'
21
- # end
22
- #
23
- def initialize(parent_app=nil, *args, &block)
24
- #puts "INITIALIZE RackWarden INSTANCE [parent_app, self, args, block]: #{[parent_app, self, args, block]}"
25
- # extract options.
26
- opts = args.last.is_a?(Hash) ? args.pop : {}
27
- klass = self.class
28
- if parent_app
29
- # append views from opts.
30
- klass.set(:original_views, opts.has_key?(:views) ? klass.views : nil)
31
- #klass.set(:views => [Array(klass.views), opts.delete(:views)].flatten) if opts[:views]
32
- # set app settings with remainder of opts.
33
- klass.set opts if opts.any?
34
- # eval the use-block from the parent app, in context of this app.
35
- klass.instance_exec(self, parent_instance, &block) if block_given?
36
- # do parent_app setup.
37
- setup_parent_app(parent_app, args, opts)
38
- #parent_app.class.helpers(RackWardenHelpers) rescue ApplicationController.send(:include, RackWardenHelpers)
39
- end
40
- # finally, send parent app to super, but don't send the use-block (thus the empty proc)
41
- super(parent_app, &Proc.new{})
42
- end
20
+ # WBR - This will receive params and a block from the parent "use" statement.
21
+ # This middleware app has been modified to process the parent use-block in
22
+ # the context of the RackWarden class. So you can set settings on RackWarden,
23
+ # when you call "use RackWarden::App"
24
+ # Example:
25
+ #
26
+ # use RackWarden::App :layout=>:'my_layout' do |rack_warden_instance, parent_app_instance|
27
+ # set :myvar, 'something'
28
+ # end
29
+ #
30
+ def initialize(parent_app=nil, *args, &block)
31
+ #puts "INITIALIZE RackWarden::App INSTANCE [parent_app, self, args, block]: #{[parent_app, self, args, block]}"
32
+ # extract options.
33
+ opts = args.last.is_a?(Hash) ? args.pop : {}
34
+ klass = self.class
35
+ if parent_app
36
+ # append views from opts.
37
+ klass.set(:original_views, opts.has_key?(:views) ? klass.views : nil)
38
+ #klass.set(:views => [Array(klass.views), opts.delete(:views)].flatten) if opts[:views]
39
+ # set app settings with remainder of opts.
40
+ klass.set opts if opts.any?
41
+ # eval the use-block from the parent app, in context of this app.
42
+ klass.instance_exec(self, parent_instance, &block) if block_given?
43
+ # do parent_app setup.
44
+ setup_parent_app(parent_app, args, opts)
45
+ #parent_app.class.helpers(RackWardenHelpers) rescue ApplicationController.send(:include, RackWardenHelpers)
46
+ end
47
+ # finally, send parent app to super, but don't send the use-block (thus the empty proc)
48
+ super(parent_app, &Proc.new{})
49
+ end
43
50
 
44
- def setup_parent_app(parent_app, args, opts)
45
- puts "RACKWARDEN initializing parent app: #{parent_app}"
46
- puts "RACKWARDEN parent app parents: #{parent_app.class.parents}"
47
- puts "RACKWARDEN parent app ancestors: #{parent_app.class.ancestors}"
48
- klass = self.class
49
- case
50
- when parent_app.class.ancestors.find{|x| x.to_s=='Sinatra::Base'}
51
- parent_app.class.helpers(RackWardenHelpers)
52
- default_parent_views = File.join(Dir.pwd,"views")
53
- when parent_app.class.parents.find{|x| x.to_s=='ActionDispatch'}
54
- ApplicationController.send(:include, RackWardenHelpers)
55
- default_parent_views = File.join(Dir.pwd, "app/views")
56
- end
51
+ def setup_parent_app(parent_app, args, opts)
52
+ puts "RACKWARDEN initializing parent app: #{parent_app}"
53
+ #puts "RACKWARDEN parent app parents: #{parent_app.class.parents}"
54
+ #puts "RACKWARDEN parent app ancestors: #{parent_app.class.ancestors}"
55
+ klass = self.class
56
+ case
57
+ when parent_app.class.ancestors.find{|x| x.to_s=='Sinatra::Base'}
58
+ parent_app.class.helpers(RackWardenHelpers)
59
+ default_parent_views = File.join(Dir.pwd,"views")
60
+ when parent_app.class.parents.find{|x| x.to_s=='ActionDispatch'}
61
+ ApplicationController.send(:include, RackWardenHelpers)
62
+ default_parent_views = File.join(Dir.pwd, "app/views")
63
+ end
57
64
 
58
- new_views = []
59
- original_views = klass.original_views
60
- # append parent rails views folder unless opts.has_key?(:views)
61
- new_views << default_parent_views unless opts.has_key?(:views)
62
- # append original_views, if original_views
63
- new_views << original_views if original_views
64
- klass.set(:views => [Array(klass.views), new_views].flatten.compact.uniq) if new_views.any?
65
- puts "RACKWARDEN views: #{klass.views}"
66
- end
65
+ new_views = []
66
+ original_views = klass.original_views
67
+ # append parent rails views folder unless opts.has_key?(:views)
68
+ new_views << default_parent_views unless opts.has_key?(:views)
69
+ # append original_views, if original_views
70
+ new_views << original_views if original_views
71
+ klass.set(:views => [Array(klass.views), new_views].flatten.compact.uniq) if new_views.any?
72
+ puts "RACKWARDEN views: #{klass.views}"
73
+ end
67
74
 
68
- use Warden::Manager do |config|
69
- # Tell Warden how to save our User info into a session.
70
- # Sessions can only take strings, not Ruby code, we'll store
71
- # the User's `id`
72
- config.serialize_into_session{|user| user.id }
73
- # Now tell Warden how to take what we've stored in the session
74
- # and get a User from that information.
75
- config.serialize_from_session{|id| User.get(id) }
76
-
77
- config.scope_defaults :default,
78
- # "strategies" is an array of named methods with which to
79
- # attempt authentication. We have to define this later.
80
- strategies: [:password],
81
- # The action is a route to send the user to when
82
- # warden.authenticate! returns a false answer. We'll show
83
- # this route below.
84
- action: 'auth/unauthenticated'
85
- # When a user tries to log in and cannot, this specifies the
86
- # app to send the user to.
87
- config.failure_app = self
88
- end
89
-
90
- Warden::Manager.before_failure do |env,opts|
91
- env['REQUEST_METHOD'] = 'POST'
92
- end
93
-
94
- Warden::Strategies.add(:password) do
95
- def valid?
96
- params['user'] && params['user']['username'] && params['user']['password']
75
+ use Warden::Manager do |config|
76
+ # Tell Warden how to save our User info into a session.
77
+ # Sessions can only take strings, not Ruby code, we'll store
78
+ # the User's `id`
79
+ config.serialize_into_session{|user| user.id }
80
+ # Now tell Warden how to take what we've stored in the session
81
+ # and get a User from that information.
82
+ config.serialize_from_session{|id| User.get(id) }
83
+
84
+ config.scope_defaults :default,
85
+ # "strategies" is an array of named methods with which to
86
+ # attempt authentication. We have to define this later.
87
+ strategies: [:password],
88
+ # The action is a route to send the user to when
89
+ # warden.authenticate! returns a false answer. We'll show
90
+ # this route below.
91
+ action: 'auth/unauthenticated'
92
+ # When a user tries to log in and cannot, this specifies the
93
+ # app to send the user to.
94
+ config.failure_app = self
97
95
  end
98
96
 
99
- def authenticate!
100
- user = User.first(username: params['user']['username'])
97
+ Warden::Manager.before_failure do |env,opts|
98
+ env['REQUEST_METHOD'] = 'POST'
99
+ end
101
100
 
102
- if user.nil?
103
- fail!("The username you entered does not exist.")
104
- elsif user.authenticate(params['user']['password'])
105
- success!(user)
106
- else
107
- fail!("Could not log in")
101
+ Warden::Strategies.add(:password) do
102
+ def valid?
103
+ params['user'] && params['user']['username'] && params['user']['password']
104
+ end
105
+
106
+ def authenticate!
107
+ user = User.first(username: params['user']['username'])
108
+
109
+ if user.nil?
110
+ fail!("The username you entered does not exist.")
111
+ elsif user.authenticate(params['user']['password'])
112
+ success!(user)
113
+ else
114
+ fail!("Could not log in")
115
+ end
108
116
  end
109
- end
110
117
 
111
- end
118
+ end
112
119
 
113
- # Also bring these into your main app helpers.
114
- module RackWardenHelpers
115
- # WBR - override. This passes block to be rendered to first template that matches.
116
- def find_template(views, name, engine, &block)
117
- # puts "THE VIEWS: #{views}"
118
- # puts "THE NAME: #{name}"
119
- # puts "THE ENGINE: #{engine}"
120
- # puts "THE BLOCK: #{block}"
121
- Array(views).each { |v| super(v, name, engine, &block) }
122
- end
120
+ # Also bring these into your main app helpers.
121
+ module RackWardenHelpers
122
+ # WBR - override. This passes block to be rendered to first template that matches.
123
+ def find_template(views, name, engine, &block)
124
+ # puts "THE VIEWS: #{views}"
125
+ # puts "THE NAME: #{name}"
126
+ # puts "THE ENGINE: #{engine}"
127
+ # puts "THE BLOCK: #{block}"
128
+ Array(views).each { |v| super(v, name, engine, &block) }
129
+ end
123
130
 
124
- def require_login
125
- warden.authenticate!
126
- end
131
+ def require_login
132
+ warden.authenticate!
133
+ end
127
134
 
128
- def warden
129
- env['warden']
130
- end
135
+ def warden
136
+ env['warden']
137
+ end
131
138
 
132
- def current_user
133
- warden.user
134
- end
139
+ def current_user
140
+ warden.user
141
+ end
135
142
 
136
- def logged_in?
137
- warden.authenticated?
138
- end
143
+ def logged_in?
144
+ warden.authenticated?
145
+ end
139
146
 
140
147
 
141
- # TODO: Shouldn't these be in warden block above? But they don't work there for some reason.
148
+ # TODO: Shouldn't these be in warden block above? But they don't work there for some reason.
142
149
 
143
- def valid_user_input?
144
- params['user'] && params['user']['username'] && params['user']['password']
145
- end
150
+ def valid_user_input?
151
+ params['user'] && params['user']['username'] && params['user']['password']
152
+ end
146
153
 
147
- # TODO: This doesn't work.
148
- def create_user
154
+ # TODO: This doesn't work.
155
+ def create_user
149
156
 
150
- recaptcha
157
+ recaptcha
151
158
 
152
- return unless valid_user_input?
153
- user = User.create(username: params['user']['username'])
154
- user.password = params['user']['password']
155
- user.save && warden.set_user(user)
156
- end
159
+ return unless valid_user_input?
160
+ user = User.create(username: params['user']['username'])
161
+ user.password = params['user']['password']
162
+ user.save && warden.set_user(user)
163
+ end
157
164
 
158
- # reCAPTCHA. See https://www.google.com/recaptcha/admin#site/318693958?setup
159
- def recaptcha(skip_redirect=false)
160
- _recaptcha = ActiveSupport::JSON.decode(open("https://www.google.com/recaptcha/api/siteverify?secret=6LdG4v4SAAAAAJxwcS8pZRG371ZucyYg5yVUji_V&response=#{params['g-recaptcha-response']}&remoteip=#{request.ip}").read)
161
- puts "RECAPTCHA", _recaptcha
162
- #(render action and return) unless recaptcha['success']
163
- unless _recaptcha['success'] || skip_redirect
164
- flash(:rwarden)[:error] = "Please confirm you are human."
165
- redirect back
166
- end
167
- end
165
+ # reCAPTCHA. See https://www.google.com/recaptcha/admin#site/318693958?setup
166
+ def recaptcha(skip_redirect=false)
167
+ _recaptcha = ActiveSupport::JSON.decode(open("https://www.google.com/recaptcha/api/siteverify?secret=6LdG4v4SAAAAAJxwcS8pZRG371ZucyYg5yVUji_V&response=#{params['g-recaptcha-response']}&remoteip=#{request.ip}").read)
168
+ puts "RECAPTCHA", _recaptcha
169
+ #(render action and return) unless recaptcha['success']
170
+ unless _recaptcha['success'] || skip_redirect
171
+ flash(:rwarden)[:error] = "Please confirm you are human."
172
+ redirect back
173
+ end
174
+ end
175
+
176
+ def default_page
177
+ erb :'rack_warden_index.html', :layout=>settings.layout
178
+ end
168
179
 
169
- end # RackWardenHelpers
170
- helpers RackWardenHelpers
180
+ end # RackWardenHelpers
181
+ helpers RackWardenHelpers
171
182
 
183
+ if defined? ::RACK_WARDEN_STANDALONE
184
+ get '/?' do
185
+ default_page
186
+ end
187
+ end
188
+
189
+ get '/auth/?' do
190
+ default_page
191
+ end
192
+
193
+ get '/auth/login' do
194
+ erb :'login_user.html', :layout=>settings.layout
195
+ end
172
196
 
173
- # WBR. I disabled this so upstream apps would work.
174
- get '/auth' do
175
- erb "Warden authentication for any rack based app", :layout=>settings.layout
176
- end
197
+ post '/auth/login' do
198
+ warden.authenticate!
177
199
 
178
- get '/auth/login' do
179
- erb :'login_user.html', :layout=>settings.layout
180
- end
200
+ flash(:rwarden)[:success] = warden.message || "Successful login"
181
201
 
182
- post '/auth/login' do
183
- warden.authenticate!
202
+ puts "RETURN_TO #{session[:return_to]}"
203
+ if session[:return_to].nil?
204
+ redirect url(settings.default_route, false)
205
+ else
206
+ redirect session[:return_to]
207
+ end
208
+ end
184
209
 
185
- flash(:rwarden)[:success] = warden.message || "Successful login"
210
+ get '/auth/logout' do
211
+ warden.raw_session.inspect
212
+ warden.logout
213
+ flash(:rwarden)[:success] = 'You have been logged out'
214
+ redirect url(settings.default_route, false)
215
+ end
186
216
 
187
- puts "RETURN_TO #{session[:return_to]}"
188
- if session[:return_to].nil?
189
- redirect settings.default_route
190
- else
191
- redirect session[:return_to]
217
+ get '/auth/create' do
218
+ erb :'create_user.html', :layout=>settings.layout
192
219
  end
193
- end
194
-
195
- get '/auth/logout' do
196
- warden.raw_session.inspect
197
- warden.logout
198
- flash(:rwarden)[:success] = 'You have been logged out'
199
- redirect settings.default_route
200
- end
201
-
202
- get '/auth/create' do
203
- erb :'create_user.html', :layout=>settings.layout
204
- end
205
-
206
- post '/auth/create' do
207
- if create_user
208
- flash(:rwarden)[:success] = warden.message || "Account created"
209
- redirect session[:return_to] || settings.default_route
210
- else
211
- flash(:rwarden)[:error] = warden.message || "Could not create account"
212
- redirect url('/auth/create', false)
213
- end
214
- end
215
-
216
- post '/auth/unauthenticated' do
217
- # I had to remove the condition, since it was not updating return path when it should have.
218
- session[:return_to] = env['warden.options'][:attempted_path] if !request.xhr? && !env['warden.options'][:attempted_path][/login/]
219
- puts "WARDEN ATTEMPTED PATH: #{env['warden.options'][:attempted_path]}"
220
- puts warden
221
- flash(:rwarden)[:error] = warden.message || "Please login to continue"
222
- redirect url('/auth/login', false)
223
- end
224
-
225
- get '/auth/protected' do
226
- warden.authenticate!
227
-
228
- erb :'rack_warden_protected.html', :layout=>settings.layout
229
- end
230
-
220
+
221
+ post '/auth/create' do
222
+ if create_user
223
+ flash(:rwarden)[:success] = warden.message || "Account created"
224
+ redirect session[:return_to] || url(settings.default_route, false)
225
+ else
226
+ flash(:rwarden)[:error] = warden.message || "Could not create account"
227
+ redirect url('/auth/create', false)
228
+ end
229
+ end
230
+
231
+ post '/auth/unauthenticated' do
232
+ # I had to remove the condition, since it was not updating return path when it should have.
233
+ session[:return_to] = env['warden.options'][:attempted_path] if !request.xhr? && !env['warden.options'][:attempted_path][/login/]
234
+ puts "WARDEN ATTEMPTED PATH: #{env['warden.options'][:attempted_path]}"
235
+ puts warden
236
+ flash(:rwarden)[:error] = warden.message || "Please login to continue"
237
+ redirect url('/auth/login', false)
238
+ end
239
+
240
+ get '/auth/protected' do
241
+ warden.authenticate!
242
+
243
+ erb :'rack_warden_protected.html', :layout=>settings.layout
244
+ end
245
+
246
+ end # App
231
247
  end # RackWarden
232
248
 
@@ -1,5 +1,5 @@
1
1
  #require 'bcrypt'
2
- DataMapper.setup(:default, RackWarden.database)
2
+ DataMapper.setup(:default, RackWarden::App.database)
3
3
 
4
4
  class User
5
5
  include DataMapper::Resource
@@ -1,3 +1,3 @@
1
- class RackWarden < Sinatra::Base
2
- VERSION = "0.0.2"
1
+ module RackWarden
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1 @@
1
+ <h2>Warden authentication for any rack based app<h2>
@@ -15,7 +15,11 @@
15
15
  <body>
16
16
  <h1>Rack Warden</h1>
17
17
 
18
- <p><a href="<%=url('/auth/login', false)%>">Log In</a> | <a href="<%=url('/auth/protected', false)%>">Protected Page</a> | <a href="<%=url('/auth/logout', false)%>">Log Out</a></p>
18
+ <p>
19
+ <a href="<%=url('/auth/login', false)%>">Log In</a> |
20
+ <a href="<%=url('/', false)%>">Home</a> |
21
+ <a href="<%=url('/auth/protected', false)%>">Protected Page</a> |
22
+ <a href="<%=url('/auth/logout', false)%>">Log Out</a></p>
19
23
 
20
24
  <% if flash(:rwarden)[:success] %>
21
25
  <div style="color:green;">
data/rack_warden.gemspec CHANGED
@@ -1,7 +1,8 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'sinatra/base'
4
+ # This breaks bundle-install
5
+ #require 'sinatra/base'
5
6
  require 'rack_warden/version'
6
7
 
7
8
  Gem::Specification.new do |spec|
@@ -9,8 +10,8 @@ Gem::Specification.new do |spec|
9
10
  spec.version = RackWarden::VERSION
10
11
  spec.authors = ["William Richardson"]
11
12
  spec.email = ["wbr@mac.com"]
12
- spec.summary = %q{A warden/sinatra micro-app providing authentication and user management to any rack-based app}
13
- spec.description = %q{A warden/sinatra micro-app providing authentication and user management to any rack-based app.}
13
+ spec.summary = %q{A warden/sinatra mini-app providing authentication and user management for any rack-based app}
14
+ spec.description = %q{A warden/sinatra mini-app providing authentication and user management for any rack-based app.}
14
15
  spec.homepage = ""
15
16
  spec.license = "MIT"
16
17
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack_warden
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - William Richardson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-26 00:00:00.000000000 Z
11
+ date: 2014-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -150,8 +150,8 @@ dependencies:
150
150
  - - ! '>='
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
- description: A warden/sinatra micro-app providing authentication and user management
154
- to any rack-based app.
153
+ description: A warden/sinatra mini-app providing authentication and user management
154
+ for any rack-based app.
155
155
  email:
156
156
  - wbr@mac.com
157
157
  executables: []
@@ -197,6 +197,6 @@ rubyforge_project:
197
197
  rubygems_version: 2.2.2
198
198
  signing_key:
199
199
  specification_version: 4
200
- summary: A warden/sinatra micro-app providing authentication and user management to
200
+ summary: A warden/sinatra mini-app providing authentication and user management for
201
201
  any rack-based app
202
202
  test_files: []