activerain-clearance 0.6.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CHANGELOG.textile +144 -0
  2. data/LICENSE +21 -0
  3. data/README.textile +125 -0
  4. data/Rakefile +73 -0
  5. data/TODO.textile +6 -0
  6. data/app/controllers/clearance/confirmations_controller.rb +48 -0
  7. data/app/controllers/clearance/passwords_controller.rb +69 -0
  8. data/app/controllers/clearance/sessions_controller.rb +50 -0
  9. data/app/controllers/clearance/users_controller.rb +31 -0
  10. data/app/models/clearance_mailer.rb +23 -0
  11. data/app/views/clearance_mailer/change_password.html.erb +7 -0
  12. data/app/views/clearance_mailer/confirmation.html.erb +2 -0
  13. data/app/views/passwords/edit.html.erb +23 -0
  14. data/app/views/passwords/new.html.erb +15 -0
  15. data/app/views/sessions/new.html.erb +28 -0
  16. data/app/views/users/_form.html.erb +13 -0
  17. data/app/views/users/new.html.erb +6 -0
  18. data/config/clearance_routes.rb +19 -0
  19. data/generators/clearance/USAGE +1 -0
  20. data/generators/clearance/clearance_generator.rb +41 -0
  21. data/generators/clearance/lib/insert_commands.rb +103 -0
  22. data/generators/clearance/lib/rake_commands.rb +22 -0
  23. data/generators/clearance/templates/README +22 -0
  24. data/generators/clearance/templates/factories.rb +13 -0
  25. data/generators/clearance/templates/migrations/create_users.rb +20 -0
  26. data/generators/clearance/templates/migrations/update_users.rb +41 -0
  27. data/generators/clearance/templates/user.rb +3 -0
  28. data/generators/clearance_features/USAGE +1 -0
  29. data/generators/clearance_features/clearance_features_generator.rb +20 -0
  30. data/generators/clearance_features/templates/features/password_reset.feature +33 -0
  31. data/generators/clearance_features/templates/features/sign_in.feature +42 -0
  32. data/generators/clearance_features/templates/features/sign_out.feature +23 -0
  33. data/generators/clearance_features/templates/features/sign_up.feature +28 -0
  34. data/generators/clearance_features/templates/features/step_definitions/clearance_steps.rb +110 -0
  35. data/generators/clearance_features/templates/features/step_definitions/factory_girl_steps.rb +5 -0
  36. data/generators/clearance_features/templates/features/support/paths.rb +22 -0
  37. data/lib/clearance/authentication.rb +96 -0
  38. data/lib/clearance/extensions/errors.rb +4 -0
  39. data/lib/clearance/extensions/rescue.rb +1 -0
  40. data/lib/clearance/user.rb +143 -0
  41. data/lib/clearance.rb +6 -0
  42. data/rails/init.rb +1 -0
  43. data/shoulda_macros/clearance.rb +261 -0
  44. metadata +129 -0
@@ -0,0 +1,96 @@
1
+ module Clearance
2
+ module Authentication
3
+
4
+ def self.included(controller)
5
+ controller.send(:include, InstanceMethods)
6
+
7
+ controller.class_eval do
8
+ helper_method :current_user
9
+ helper_method :signed_in?
10
+
11
+ hide_action :current_user, :signed_in?
12
+ end
13
+ end
14
+
15
+ module InstanceMethods
16
+ def current_user
17
+ @_current_user ||= (user_from_cookie || user_from_session)
18
+ end
19
+
20
+ def signed_in?
21
+ ! current_user.nil?
22
+ end
23
+
24
+ protected
25
+
26
+ def authenticate
27
+ deny_access unless signed_in?
28
+ end
29
+
30
+ def user_from_session
31
+ if session[:user_id]
32
+ return nil unless user = ::User.find_by_id(session[:user_id])
33
+ return user if user.email_confirmed?
34
+ end
35
+ end
36
+
37
+ def user_from_cookie
38
+ if token = cookies[:remember_token]
39
+ return nil unless user = ::User.find_by_token(token)
40
+ return user if user.remember?
41
+ end
42
+ end
43
+
44
+ def sign_user_in(user)
45
+ sign_in(user)
46
+ end
47
+
48
+ def sign_in(user)
49
+ if user
50
+ session[:user_id] = user.id
51
+ end
52
+ end
53
+
54
+ def remember?
55
+ params[:session] && params[:session][:remember_me] == "1"
56
+ end
57
+
58
+ def remember(user)
59
+ user.remember_me!
60
+ cookies[:remember_token] = { :value => user.token,
61
+ :expires => user.token_expires_at }
62
+ end
63
+
64
+ def forget(user)
65
+ user.forget_me! if user
66
+ cookies.delete :remember_token
67
+ reset_session
68
+ end
69
+
70
+ def redirect_back_or(default)
71
+ session[:return_to] ||= params[:return_to]
72
+ if session[:return_to]
73
+ redirect_to(session[:return_to])
74
+ else
75
+ redirect_to(default)
76
+ end
77
+ session[:return_to] = nil
78
+ end
79
+
80
+ def redirect_to_root
81
+ redirect_to root_url
82
+ end
83
+
84
+ def store_location
85
+ session[:return_to] = request.request_uri if request.get?
86
+ end
87
+
88
+ def deny_access(flash_message = nil, opts = {})
89
+ store_location
90
+ flash[:failure] = flash_message if flash_message
91
+ redirect_to new_session_url
92
+ end
93
+ end
94
+
95
+ end
96
+ end
@@ -0,0 +1,4 @@
1
+ module ActionController
2
+ class Forbidden < StandardError
3
+ end
4
+ end
@@ -0,0 +1 @@
1
+ ActionController::Base.rescue_responses.update('ActionController::Forbidden' => :forbidden)
@@ -0,0 +1,143 @@
1
+ require 'digest/sha1'
2
+
3
+ module Clearance
4
+ module User
5
+
6
+ def self.included(model)
7
+ model.extend(ClassMethods)
8
+
9
+ model.send(:include, InstanceMethods)
10
+ model.send(:include, AttrAccessible)
11
+ model.send(:include, AttrAccessor)
12
+ model.send(:include, Validations)
13
+ model.send(:include, Callbacks)
14
+ end
15
+
16
+ module AttrAccessible
17
+ def self.included(model)
18
+ model.class_eval do
19
+ attr_accessible :email, :password, :password_confirmation
20
+ end
21
+ end
22
+ end
23
+
24
+ module AttrAccessor
25
+ def self.included(model)
26
+ model.class_eval do
27
+ attr_accessor :password, :password_confirmation
28
+ end
29
+ end
30
+ end
31
+
32
+ module Validations
33
+ def self.included(model)
34
+ model.class_eval do
35
+ validates_presence_of :email
36
+ validates_uniqueness_of :email, :case_sensitive => false
37
+ validates_format_of :email, :with => %r{.+@.+\..+}
38
+
39
+ validates_presence_of :password, :if => :password_required?
40
+ validates_confirmation_of :password, :if => :password_required?
41
+ end
42
+ end
43
+ end
44
+
45
+ module Callbacks
46
+ def self.included(model)
47
+ model.class_eval do
48
+ before_save :initialize_salt, :encrypt_password, :initialize_token
49
+ end
50
+ end
51
+ end
52
+
53
+ module InstanceMethods
54
+ def authenticated?(password)
55
+ encrypted_password == encrypt(password)
56
+ end
57
+
58
+ def encrypt(string)
59
+ generate_hash("--#{salt}--#{string}--")
60
+ end
61
+
62
+ def remember?
63
+ token_expires_at && Time.now.utc < token_expires_at
64
+ end
65
+
66
+ def remember_me!
67
+ remember_me_until! 2.weeks.from_now.utc
68
+ end
69
+
70
+ def forget_me!
71
+ clear_token
72
+ save(false)
73
+ end
74
+
75
+ def confirm_email!
76
+ self.email_confirmed = true
77
+ self.token = nil
78
+ save(false)
79
+ end
80
+
81
+ def forgot_password!
82
+ generate_token
83
+ save(false)
84
+ end
85
+
86
+ def update_password(new_password, new_password_confirmation)
87
+ self.password = new_password
88
+ self.password_confirmation = new_password_confirmation
89
+ clear_token if valid?
90
+ save
91
+ end
92
+
93
+ protected
94
+
95
+ def generate_hash(string)
96
+ Digest::SHA1.hexdigest(string)
97
+ end
98
+
99
+ def initialize_salt
100
+ if new_record?
101
+ self.salt = generate_hash("--#{Time.now.utc.to_s}--#{password}--")
102
+ end
103
+ end
104
+
105
+ def encrypt_password
106
+ return if password.blank?
107
+ self.encrypted_password = encrypt(password)
108
+ end
109
+
110
+ def generate_token
111
+ self.token = encrypt("--#{Time.now.utc.to_s}--#{password}--")
112
+ self.token_expires_at = nil
113
+ end
114
+
115
+ def clear_token
116
+ self.token = nil
117
+ self.token_expires_at = nil
118
+ end
119
+
120
+ def initialize_token
121
+ generate_token if new_record?
122
+ end
123
+
124
+ def password_required?
125
+ encrypted_password.blank? || !password.blank?
126
+ end
127
+
128
+ def remember_me_until!(time)
129
+ self.token_expires_at = time
130
+ self.token = encrypt("--#{token_expires_at}--#{password}--")
131
+ save(false)
132
+ end
133
+ end
134
+
135
+ module ClassMethods
136
+ def authenticate(email, password)
137
+ return nil unless user = find_by_email(email)
138
+ return user if user.authenticated?(password)
139
+ end
140
+ end
141
+
142
+ end
143
+ end
data/lib/clearance.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'clearance/extensions/errors'
2
+ require 'clearance/extensions/rescue'
3
+
4
+ require 'clearance/authentication'
5
+ require 'clearance/user'
6
+
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'clearance'
@@ -0,0 +1,261 @@
1
+ module Clearance
2
+ module Shoulda
3
+
4
+ # STATE OF AUTHENTICATION
5
+
6
+ def should_be_signed_in_as(&block)
7
+ should "be signed in as #{block.bind(self).call}" do
8
+ user = block.bind(self).call
9
+ assert_not_nil user,
10
+ "please pass a User. try: should_be_signed_in_as { @user }"
11
+ assert_equal user.id, session[:user_id],
12
+ "session[:user_id] is not set to User's id"
13
+ end
14
+ end
15
+
16
+ def should_be_signed_in_and_email_confirmed_as(&block)
17
+ warn "[DEPRECATION] should_be_signed_in_and_email_confirmed_as: questionable usefulness"
18
+ should_be_signed_in_as &block
19
+
20
+ should "have confirmed email" do
21
+ user = block.bind(self).call
22
+
23
+ assert_not_nil user
24
+ assert_equal user, assigns(:user)
25
+ assert assigns(:user).email_confirmed?
26
+ end
27
+ end
28
+
29
+ def should_not_be_signed_in
30
+ should "not be signed in" do
31
+ assert_nil session[:user_id]
32
+ end
33
+ end
34
+
35
+ def should_deny_access_on(http_method, action, opts = {})
36
+ warn "[DEPRECATION] should_deny_access_on: use a setup & should_deny_access(:flash => ?)"
37
+ flash_message = opts.delete(:flash)
38
+ context "on #{http_method} to #{action}" do
39
+ setup do
40
+ send(http_method, action, opts)
41
+ end
42
+
43
+ should_deny_access(:flash => flash_message)
44
+ end
45
+ end
46
+
47
+ def should_deny_access(opts = {})
48
+ if opts[:flash]
49
+ should_set_the_flash_to opts[:flash]
50
+ else
51
+ should_not_set_the_flash
52
+ end
53
+
54
+ should_redirect_to('new_session_url') { new_session_url }
55
+ end
56
+
57
+ # HTTP FLUENCY
58
+
59
+ def should_forbid(description, &block)
60
+ should "forbid #{description}" do
61
+ assert_raises ActionController::Forbidden do
62
+ instance_eval(&block)
63
+ end
64
+ end
65
+ end
66
+
67
+ # CONTEXTS
68
+
69
+ def signed_in_user_context(&blk)
70
+ warn "[DEPRECATION] signed_in_user_context: creates a Mystery Guest, causes Obscure Test"
71
+ context "A signed in user" do
72
+ setup do
73
+ @user = Factory(:user)
74
+ @user.confirm_email!
75
+ sign_in_as @user
76
+ end
77
+ merge_block(&blk)
78
+ end
79
+ end
80
+
81
+ def public_context(&blk)
82
+ warn "[DEPRECATION] public_context: common case is no-op. call sign_out otherwise"
83
+ context "The public" do
84
+ setup { sign_out }
85
+ merge_block(&blk)
86
+ end
87
+ end
88
+
89
+ # CREATING USERS
90
+
91
+ def should_create_user_successfully
92
+ warn "[DEPRECATION] should_create_user_successfully: not meant to be public, no longer used internally"
93
+ should_assign_to :user
94
+ should_change 'User.count', :by => 1
95
+
96
+ should "send the confirmation email" do
97
+ assert_sent_email do |email|
98
+ email.subject =~ /account confirmation/i
99
+ end
100
+ end
101
+
102
+ should_set_the_flash_to /confirm/i
103
+ should_redirect_to_url_after_create
104
+ end
105
+
106
+ # RENDERING
107
+
108
+ def should_render_nothing
109
+ should "render nothing" do
110
+ assert @response.body.blank?
111
+ end
112
+ end
113
+
114
+ # REDIRECTS
115
+
116
+ def should_redirect_to_url_after_create
117
+ should_redirect_to("the post-create url") do
118
+ @controller.send(:url_after_create)
119
+ end
120
+ end
121
+
122
+ def should_redirect_to_url_after_update
123
+ should_redirect_to("the post-update url") do
124
+ @controller.send(:url_after_update)
125
+ end
126
+ end
127
+
128
+ def should_redirect_to_url_after_destroy
129
+ should_redirect_to("the post-destroy url") do
130
+ @controller.send(:url_after_destroy)
131
+ end
132
+ end
133
+
134
+ # VALIDATIONS
135
+
136
+ def should_validate_confirmation_of(attribute, opts = {})
137
+ warn "[DEPRECATION] should_validate_confirmation_of: not meant to be public, no longer used internally"
138
+ raise ArgumentError if opts[:factory].nil?
139
+
140
+ context "on save" do
141
+ should_validate_confirmation_is_not_blank opts[:factory], attribute
142
+ should_validate_confirmation_is_not_bad opts[:factory], attribute
143
+ end
144
+ end
145
+
146
+ def should_validate_confirmation_is_not_blank(factory, attribute, opts = {})
147
+ warn "[DEPRECATION] should_validate_confirmation_is_not_blank: not meant to be public, no longer used internally"
148
+ should "validate #{attribute}_confirmation is not blank" do
149
+ model = Factory.build(factory, blank_confirmation_options(attribute))
150
+ model.save
151
+ assert_confirmation_error(model, attribute,
152
+ "#{attribute}_confirmation cannot be blank")
153
+ end
154
+ end
155
+
156
+ def should_validate_confirmation_is_not_bad(factory, attribute, opts = {})
157
+ warn "[DEPRECATION] should_validate_confirmation_is_not_bad: not meant to be public, no longer used internally"
158
+ should "validate #{attribute}_confirmation is different than #{attribute}" do
159
+ model = Factory.build(factory, bad_confirmation_options(attribute))
160
+ model.save
161
+ assert_confirmation_error(model, attribute,
162
+ "#{attribute}_confirmation cannot be different than #{attribute}")
163
+ end
164
+ end
165
+
166
+ # FORMS
167
+
168
+ def should_display_a_password_update_form
169
+ warn "[DEPRECATION] should_display_a_password_update_form: not meant to be public, no longer used internally"
170
+ should "have a form for the user's token, password, and password confirm" do
171
+ update_path = ERB::Util.h(
172
+ user_password_path(@user, :token => @user.token)
173
+ )
174
+
175
+ assert_select 'form[action=?]', update_path do
176
+ assert_select 'input[name=_method][value=?]', 'put'
177
+ assert_select 'input[name=?]', 'user[password]'
178
+ assert_select 'input[name=?]', 'user[password_confirmation]'
179
+ end
180
+ end
181
+ end
182
+
183
+ def should_display_a_sign_up_form
184
+ warn "[DEPRECATION] should_display_a_sign_up_form: not meant to be public, no longer used internally"
185
+ should "display a form to sign up" do
186
+ assert_select "form[action=#{users_path}][method=post]",
187
+ true, "There must be a form to sign up" do
188
+ assert_select "input[type=text][name=?]",
189
+ "user[email]", true, "There must be an email field"
190
+ assert_select "input[type=password][name=?]",
191
+ "user[password]", true, "There must be a password field"
192
+ assert_select "input[type=password][name=?]",
193
+ "user[password_confirmation]", true, "There must be a password confirmation field"
194
+ assert_select "input[type=submit]", true,
195
+ "There must be a submit button"
196
+ end
197
+ end
198
+ end
199
+
200
+ def should_display_a_sign_in_form
201
+ warn "[DEPRECATION] should_display_a_sign_in_form: not meant to be public, no longer used internally"
202
+ should 'display a "sign in" form' do
203
+ assert_select "form[action=#{session_path}][method=post]",
204
+ true, "There must be a form to sign in" do
205
+ assert_select "input[type=text][name=?]",
206
+ "session[email]", true, "There must be an email field"
207
+ assert_select "input[type=password][name=?]",
208
+ "session[password]", true, "There must be a password field"
209
+ assert_select "input[type=checkbox][name=?]",
210
+ "session[remember_me]", true, "There must be a 'remember me' check box"
211
+ assert_select "input[type=submit]", true,
212
+ "There must be a submit button"
213
+ end
214
+ end
215
+ end
216
+ end
217
+ end
218
+
219
+ module Clearance
220
+ module Shoulda
221
+ module Helpers
222
+ def sign_in_as(user)
223
+ @controller.class_eval { attr_accessor :current_user }
224
+ @controller.current_user = user
225
+ return user
226
+ end
227
+
228
+ def sign_in
229
+ sign_in_as Factory(:email_confirmed_user)
230
+ end
231
+
232
+ def sign_out
233
+ @controller.class_eval { attr_accessor :current_user }
234
+ @controller.current_user = nil
235
+ end
236
+
237
+ def blank_confirmation_options(attribute)
238
+ warn "[DEPRECATION] blank_confirmation_options: not meant to be public, no longer used internally"
239
+ opts = { attribute => attribute.to_s }
240
+ opts.merge("#{attribute}_confirmation".to_sym => "")
241
+ end
242
+
243
+ def bad_confirmation_options(attribute)
244
+ warn "[DEPRECATION] bad_confirmation_options: not meant to be public, no longer used internally"
245
+ opts = { attribute => attribute.to_s }
246
+ opts.merge("#{attribute}_confirmation".to_sym => "not_#{attribute}")
247
+ end
248
+
249
+ def assert_confirmation_error(model, attribute, message = "confirmation error")
250
+ warn "[DEPRECATION] assert_confirmation_error: not meant to be public, no longer used internally"
251
+ assert model.errors.on(attribute).include?("doesn't match confirmation"),
252
+ message
253
+ end
254
+ end
255
+ end
256
+ end
257
+
258
+ class Test::Unit::TestCase
259
+ include Clearance::Shoulda::Helpers
260
+ end
261
+ Test::Unit::TestCase.extend(Clearance::Shoulda)
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerain-clearance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.6
5
+ platform: ruby
6
+ authors:
7
+ - Dan Croak
8
+ - Mike Burns
9
+ - Jason Morrison
10
+ - Joe Ferris
11
+ - Eugene Bolshakov
12
+ - Nick Quaranto
13
+ - Josh Nichols
14
+ - Mike Breen
15
+ - "Marcel G\xC3\xB6rner"
16
+ - Bence Nagy
17
+ - Ben Mabey
18
+ - Eloy Duran
19
+ - Tim Pope
20
+ - Mihai Anca
21
+ - Mark Cornick
22
+ - Shay Arnett
23
+ autorequire:
24
+ bindir: bin
25
+ cert_chain: []
26
+
27
+ date: 2009-05-17 21:00:00 -07:00
28
+ default_executable:
29
+ dependencies: []
30
+
31
+ description: Rails authentication with email & password.
32
+ email: support@thoughtbot.com
33
+ executables: []
34
+
35
+ extensions: []
36
+
37
+ extra_rdoc_files: []
38
+
39
+ files:
40
+ - CHANGELOG.textile
41
+ - LICENSE
42
+ - Rakefile
43
+ - README.textile
44
+ - TODO.textile
45
+ - app/controllers
46
+ - app/controllers/clearance
47
+ - app/controllers/clearance/confirmations_controller.rb
48
+ - app/controllers/clearance/passwords_controller.rb
49
+ - app/controllers/clearance/sessions_controller.rb
50
+ - app/controllers/clearance/users_controller.rb
51
+ - app/models
52
+ - app/models/clearance_mailer.rb
53
+ - app/views
54
+ - app/views/clearance_mailer
55
+ - app/views/clearance_mailer/change_password.html.erb
56
+ - app/views/clearance_mailer/confirmation.html.erb
57
+ - app/views/passwords
58
+ - app/views/passwords/edit.html.erb
59
+ - app/views/passwords/new.html.erb
60
+ - app/views/sessions
61
+ - app/views/sessions/new.html.erb
62
+ - app/views/users
63
+ - app/views/users/_form.html.erb
64
+ - app/views/users/new.html.erb
65
+ - config/clearance_routes.rb
66
+ - generators/clearance
67
+ - generators/clearance/clearance_generator.rb
68
+ - generators/clearance/lib
69
+ - generators/clearance/lib/insert_commands.rb
70
+ - generators/clearance/lib/rake_commands.rb
71
+ - generators/clearance/templates
72
+ - generators/clearance/templates/factories.rb
73
+ - generators/clearance/templates/migrations
74
+ - generators/clearance/templates/migrations/create_users.rb
75
+ - generators/clearance/templates/migrations/update_users.rb
76
+ - generators/clearance/templates/README
77
+ - generators/clearance/templates/user.rb
78
+ - generators/clearance/USAGE
79
+ - generators/clearance_features
80
+ - generators/clearance_features/clearance_features_generator.rb
81
+ - generators/clearance_features/templates
82
+ - generators/clearance_features/templates/features
83
+ - generators/clearance_features/templates/features/password_reset.feature
84
+ - generators/clearance_features/templates/features/sign_in.feature
85
+ - generators/clearance_features/templates/features/sign_out.feature
86
+ - generators/clearance_features/templates/features/sign_up.feature
87
+ - generators/clearance_features/templates/features/step_definitions
88
+ - generators/clearance_features/templates/features/step_definitions/clearance_steps.rb
89
+ - generators/clearance_features/templates/features/step_definitions/factory_girl_steps.rb
90
+ - generators/clearance_features/templates/features/support
91
+ - generators/clearance_features/templates/features/support/paths.rb
92
+ - generators/clearance_features/USAGE
93
+ - lib/clearance
94
+ - lib/clearance/authentication.rb
95
+ - lib/clearance/extensions
96
+ - lib/clearance/extensions/errors.rb
97
+ - lib/clearance/extensions/rescue.rb
98
+ - lib/clearance/user.rb
99
+ - lib/clearance.rb
100
+ - shoulda_macros/clearance.rb
101
+ - rails/init.rb
102
+ has_rdoc: true
103
+ homepage: http://github.com/thoughtbot/clearance
104
+ post_install_message:
105
+ rdoc_options: []
106
+
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: "0"
114
+ version:
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: "0"
120
+ version:
121
+ requirements: []
122
+
123
+ rubyforge_project:
124
+ rubygems_version: 1.2.0
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Rails authentication with email & password.
128
+ test_files: []
129
+