/
authentication.rb
404 lines (329 loc) · 11 KB
/
authentication.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# This is separate from base because it's long and distracts
# from understanding the bigger picture. This is based on a mix
# of experience, the the authlogic rdoc (http://authlogic.rubyforge.org/)
# and a tutorial written by Ben Johnson, authlogic's author
# (http://www.binarylogic.com/2008/11/3/tutorial-authlogic-basic-setup)
#
# I understand that a lot of things in here are brittle and also that
# there are many other authentication ideas to consider. I just wanted
# to have a running start on this part of a new project, so here it is.
# Install/update the gem, if necessary
gem 'authlogic'
# Depend on the gem inside the environment.rb. Note, this is kind of
# brittle, and I should find a better way to do this.
file 'config/environment.rb',
%q{# Be sure to restart your server when you modify this file
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.2' unless defined? RAILS_GEM_VERSION
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
# Specify gems that this application depends on and have them installed with rake gems:install
# config.gem "bj"
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
# config.gem "sqlite3-ruby", :lib => "sqlite3"
# config.gem "aws-s3", :lib => "aws/s3"
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Skip frameworks you're not going to use. To use Rails without a database,
# you must remove the Active Record framework.
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
# Activate observers that should always be running
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names.
config.time_zone = 'UTC'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
config.gem "authlogic"
end
}
# Generate the user session
generate "session user_session"
# Generate the user
file "db/migrate/#{Time.now.strftime('%Y%m%d%H%M%S_create_users.rb')}",
%q{class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :email, :null => false
t.string :crypted_password, :null => false
t.string :password_salt, :null => false
t.string :persistence_token, :null => false
t.string :single_access_token, :null => false
t.string :perishable_token, :null => false
t.integer :login_count, :null => false, :default => 0
t.integer :failed_login_count, :null => false, :default => 0
t.datetime :last_request_at
t.datetime :current_login_at
t.datetime :last_login_at
t.string :current_login_ip
t.string :last_login_ip
t.timestamps
end
end
def self.down
drop_table :users
end
end
}
run "mkdir -p app/models"
file "app/models/user.rb",
%q{class User < ActiveRecord::Base
acts_as_authentic do |c|
# c.my_config_option = my_value
end
end
}
# Create a spec stub for the user.
run "mkdir -p spec/models"
file "spec/models/user_spec.rb",
%q{require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe User do
before(:each) do
@valid_attributes = {
:email => "user@example.com",
:password => "password",
:password_confirmation => "password",
:persistence_token => "value for persistence_token",
:single_access_token => "value for single_access_token",
:perishable_token => "value for perishable_token"
}
end
it "should create a new instance given valid attributes" do
user_model
end
end
def user_model(opts={})
@user = User.create!(@valid_attributes.merge(opts))
end
}
# Create a user session controller
file "app/controllers/user_sessions_controller.rb",
%q{class UserSessionsController < ApplicationController
# before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => :destroy
def new
@user_session = UserSession.new
end
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default account_url
else
render :action => :new
end
end
def destroy
current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_back_or_default new_user_session_url
end
end
}
# Create a users controller
file "app/controllers/users_controller.rb",
%q{class UsersController < ApplicationController
# before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => [:show, :edit, :update]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Account registered!"
redirect_back_or_default account_url
else
render :action => :new
end
end
def show
@user = @current_user
end
def edit
@user = @current_user
end
def update
@user = @current_user # makes our views "cleaner" and more consistent
if @user.update_attributes(params[:user])
flash[:notice] = "Account updated!"
redirect_to account_url
else
render :action => :edit
end
end
end
}
# Setup the views
run "mkdir -p app/views/password_resets"
file "app/views/password_resets/edit.html.erb",
%q{<h1>Change My Password</h1>
<% form_for @user, :url => password_reset_path, :method => :put do |f| %>
<%= f.error_messages %>
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<br />
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %><br />
<br />
<%= f.submit "Update my password and log me in" %>
<% end %>
}
file "app/views/password_resets/new.html.erb",
%q{<h1>Forgot Password</h1>
Fill out the form below and instructions to reset your password will be emailed to you:<br />
<br />
<% form_tag password_resets_path do %>
<label>Email:</label><br />
<%= text_field_tag "email" %><br />
<br />
<%= submit_tag "Reset my password" %>
<% end %>
}
run "mkdir -p app/views/user_sessions"
file "app/views/user_sessions/new.html.erb",
%q{<h1>Login</h1>
<% form_for @user_session, :url => user_session_path do |f| %>
<%= f.error_messages %>
<%= f.label :email %><br />
<%= f.text_field :email %><br />
<br />
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<br />
<%= f.check_box :remember_me %><%= f.label :remember_me %><br />
<br />
<%= f.submit "Login" %>
<% end %>
}
run "mkdir -p app/views/users"
file "app/views/users/_form.html.erb",
%q{<%= form.label :email %><br />
<%= form.text_field :email %><br />
<br />
<%= form.label :password, form.object.new_record? ? nil : "Change password" %><br />
<%= form.password_field :password %><br />
<br />
<%= form.label :password_confirmation %><br />
<%= form.password_field :password_confirmation %><br />
<br />
}
file "app/views/users/edit.html.erb",
%q{<h1>Edit My Account</h1>
<% form_for @user, :url => account_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
<%= f.submit "Update" %>
<% end %>
<br /><%= link_to "My Profile", account_path %>
}
file "app/views/users/new.html.erb",
%q{<h1>Register</h1>
<% form_for @user, :url => account_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :object => f %>
<%= f.submit "Register" %>
<% end %>
}
file "app/views/users/show.html.erb",
%q{<p>
<b>Email:</b>
<%=h @user.email %>
</p>
<p>
<b>Login count:</b>
<%=h @user.login_count %>
</p>
<p>
<b>Last request at:</b>
<%=h @user.last_request_at %>
</p>
<p>
<b>Last login at:</b>
<%=h @user.last_login_at %>
</p>
<p>
<b>Current login at:</b>
<%=h @user.current_login_at %>
</p>
<p>
<b>Last login ip:</b>
<%=h @user.last_login_ip %>
</p>
<p>
<b>Current login ip:</b>
<%=h @user.current_login_ip %>
</p>
<%= link_to 'Edit', edit_account_path %>
}
# Create an authentication module
file "lib/authentication.rb",
%q{module Authentication
def self.included(base)
base.send(:filter_parameter_logging, :password, :password_confirmation)
base.send(:helper_method, :current_user_session, :current_user)
end
# private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to new_user_session_url
return false
end
end
def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to account_url
return false
end
end
def store_location
session[:return_to] = request.request_uri
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
end
}
# Adjust the application controller
file "app/controllers/application_controller.rb",
%q{
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# An authlogic authentication module found in lib/authentication.rb
include Authentication
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
end
}
# Create some routes
route "map.resource :user_session"
route "map.resource :account, :controller => 'users'"
route "map.resources :users"
route "map.register '/register', :controller => 'users', :action => 'new'"
route "map.signup '/signup', :controller => 'users', :action => 'new'"
route "map.login '/login', :controller => 'user_sessions', :action => 'new'"
route "map.logout '/logout', :controller => 'user_sessions', :action => 'destroy'"