jeremydurham-restful_authentication 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/CHANGELOG +68 -0
  2. data/LICENSE +20 -0
  3. data/README.textile +224 -0
  4. data/Rakefile +54 -0
  5. data/TODO +15 -0
  6. data/generators/authenticated/USAGE +1 -0
  7. data/generators/authenticated/authenticated_generator.rb +481 -0
  8. data/generators/authenticated/lib/insert_routes.rb +54 -0
  9. data/generators/authenticated/templates/_model_partial.html.erb +8 -0
  10. data/generators/authenticated/templates/activation.erb +3 -0
  11. data/generators/authenticated/templates/authenticated_system.rb +189 -0
  12. data/generators/authenticated/templates/authenticated_test_helper.rb +12 -0
  13. data/generators/authenticated/templates/controller.rb +43 -0
  14. data/generators/authenticated/templates/features/accounts.feature +63 -0
  15. data/generators/authenticated/templates/features/sessions.feature +77 -0
  16. data/generators/authenticated/templates/features/step_definitions/ra_env.rb +7 -0
  17. data/generators/authenticated/templates/features/step_definitions/user_steps.rb +31 -0
  18. data/generators/authenticated/templates/helper.rb +2 -0
  19. data/generators/authenticated/templates/login.html.erb +14 -0
  20. data/generators/authenticated/templates/machinist_spec.rb +5 -0
  21. data/generators/authenticated/templates/machinist_test.rb +5 -0
  22. data/generators/authenticated/templates/mailer.rb +25 -0
  23. data/generators/authenticated/templates/migration.rb +26 -0
  24. data/generators/authenticated/templates/model.rb +83 -0
  25. data/generators/authenticated/templates/model_controller.rb +85 -0
  26. data/generators/authenticated/templates/model_helper.rb +93 -0
  27. data/generators/authenticated/templates/model_helper_spec.rb +157 -0
  28. data/generators/authenticated/templates/observer.rb +11 -0
  29. data/generators/authenticated/templates/signup.html.erb +19 -0
  30. data/generators/authenticated/templates/signup_notification.erb +8 -0
  31. data/generators/authenticated/templates/site_keys.rb +38 -0
  32. data/generators/authenticated/templates/spec/blueprints/user.rb +6 -0
  33. data/generators/authenticated/templates/spec/controllers/access_control_spec.rb +89 -0
  34. data/generators/authenticated/templates/spec/controllers/authenticated_system_spec.rb +107 -0
  35. data/generators/authenticated/templates/spec/controllers/sessions_controller_spec.rb +138 -0
  36. data/generators/authenticated/templates/spec/controllers/users_controller_spec.rb +196 -0
  37. data/generators/authenticated/templates/spec/fixtures/users.yml +60 -0
  38. data/generators/authenticated/templates/spec/helpers/users_helper_spec.rb +141 -0
  39. data/generators/authenticated/templates/spec/models/user_spec.rb +298 -0
  40. data/generators/authenticated/templates/test/functional_test.rb +84 -0
  41. data/generators/authenticated/templates/test/mailer_test.rb +31 -0
  42. data/generators/authenticated/templates/test/model_functional_test.rb +91 -0
  43. data/generators/authenticated/templates/test/unit_test.rb +177 -0
  44. data/lib/authentication.rb +40 -0
  45. data/lib/authentication/by_cookie_token.rb +82 -0
  46. data/lib/authentication/by_password.rb +64 -0
  47. data/lib/authorization.rb +14 -0
  48. data/lib/authorization/aasm_roles.rb +63 -0
  49. data/lib/authorization/stateful_roles.rb +62 -0
  50. data/lib/restful_authentication.rb +3 -0
  51. data/lib/trustification.rb +14 -0
  52. data/lib/trustification/email_validation.rb +20 -0
  53. data/notes/AccessControl.txt +2 -0
  54. data/notes/Authentication.txt +5 -0
  55. data/notes/Authorization.txt +154 -0
  56. data/notes/RailsPlugins.txt +78 -0
  57. data/notes/SecurityFramework.graffle +0 -0
  58. data/notes/SecurityFramework.png +0 -0
  59. data/notes/SecurityPatterns.txt +163 -0
  60. data/notes/Tradeoffs.txt +126 -0
  61. data/notes/Trustification.txt +49 -0
  62. data/restful_authentication.gemspec +32 -0
  63. data/tasks/auth.rake +33 -0
  64. metadata +128 -0
@@ -0,0 +1,54 @@
1
+ Rails::Generator::Commands::Create.class_eval do
2
+ def route_resource(*resources)
3
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
4
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
5
+
6
+ logger.route "map.resource #{resource_list}"
7
+ unless options[:pretend]
8
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
9
+ "#{match}\n map.resource #{resource_list}\n"
10
+ end
11
+ end
12
+ end
13
+
14
+ def route_name(name, path, route_options = {})
15
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
16
+
17
+ logger.route "map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
18
+ unless options[:pretend]
19
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
20
+ "#{match}\n map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ Rails::Generator::Commands::Destroy.class_eval do
27
+ def route_resource(*resources)
28
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
29
+ look_for = "\n map.resource #{resource_list}\n"
30
+ logger.route "map.resource #{resource_list}"
31
+ unless options[:pretend]
32
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
33
+ end
34
+ end
35
+
36
+ def route_name(name, path, route_options = {})
37
+ look_for = "\n map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
38
+ logger.route "map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
39
+ unless options[:pretend]
40
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
41
+ end
42
+ end
43
+ end
44
+
45
+ Rails::Generator::Commands::List.class_eval do
46
+ def route_resource(*resources)
47
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
48
+ logger.route "map.resource #{resource_list}"
49
+ end
50
+
51
+ def route_name(name, path, options = {})
52
+ logger.route "map.#{name} '#{path}', :controller => '{options[:controller]}', :action => '#{options[:action]}'"
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ <%% if logged_in? -%>
2
+ <div id="<%= file_name %>-bar-greeting">Logged in as <%%= link_to_current_<%= file_name %> :content_method => :login %></div>
3
+ <div id="<%= file_name %>-bar-action" >(<%%= link_to "Log out", logout_path, { :title => "Log out" } %>)</div>
4
+ <%% else -%>
5
+ <div id="<%= file_name %>-bar-greeting"><%%= link_to_login_with_IP 'Not logged in', :style => 'border: none;' %></div>
6
+ <div id="<%= file_name %>-bar-action" ><%%= link_to "Log in", login_path, { :title => "Log in" } %> /
7
+ <%%= link_to "Sign up", signup_path, { :title => "Create an account" } %></div>
8
+ <%% end -%>
@@ -0,0 +1,3 @@
1
+ <%%=h @<%= file_name %>.login %>, your account has been activated. Welcome aboard!
2
+
3
+ <%%=h @url %>
@@ -0,0 +1,189 @@
1
+ module AuthenticatedSystem
2
+ protected
3
+ # Returns true or false if the <%= file_name %> is logged in.
4
+ # Preloads @current_<%= file_name %> with the <%= file_name %> model if they're logged in.
5
+ def logged_in?
6
+ !!current_<%= file_name %>
7
+ end
8
+
9
+ # Accesses the current <%= file_name %> from the session.
10
+ # Future calls avoid the database because nil is not equal to false.
11
+ def current_<%= file_name %>
12
+ @current_<%= file_name %> ||= (login_from_session || login_from_basic_auth || login_from_cookie) unless @current_<%= file_name %> == false
13
+ end
14
+
15
+ # Store the given <%= file_name %> id in the session.
16
+ def current_<%= file_name %>=(new_<%= file_name %>)
17
+ session[:<%= file_name %>_id] = new_<%= file_name %> ? new_<%= file_name %>.id : nil
18
+ @current_<%= file_name %> = new_<%= file_name %> || false
19
+ end
20
+
21
+ # Check if the <%= file_name %> is authorized
22
+ #
23
+ # Override this method in your controllers if you want to restrict access
24
+ # to only a few actions or if you want to check if the <%= file_name %>
25
+ # has the correct rights.
26
+ #
27
+ # Example:
28
+ #
29
+ # # only allow nonbobs
30
+ # def authorized?
31
+ # current_<%= file_name %>.login != "bob"
32
+ # end
33
+ #
34
+ def authorized?(action = action_name, resource = nil)
35
+ logged_in?
36
+ end
37
+
38
+ # Filter method to enforce a login requirement.
39
+ #
40
+ # To require logins for all actions, use this in your controllers:
41
+ #
42
+ # before_filter :login_required
43
+ #
44
+ # To require logins for specific actions, use this in your controllers:
45
+ #
46
+ # before_filter :login_required, :only => [ :edit, :update ]
47
+ #
48
+ # To skip this in a subclassed controller:
49
+ #
50
+ # skip_before_filter :login_required
51
+ #
52
+ def login_required
53
+ authorized? || access_denied
54
+ end
55
+
56
+ # Redirect as appropriate when an access request fails.
57
+ #
58
+ # The default action is to redirect to the login screen.
59
+ #
60
+ # Override this method in your controllers if you want to have special
61
+ # behavior in case the <%= file_name %> is not authorized
62
+ # to access the requested action. For example, a popup window might
63
+ # simply close itself.
64
+ def access_denied
65
+ respond_to do |format|
66
+ format.html do
67
+ store_location
68
+ redirect_to new_<%= controller_routing_name %>_path
69
+ end
70
+ # format.any doesn't work in rails version < http://dev.rubyonrails.org/changeset/8987
71
+ # Add any other API formats here. (Some browsers, notably IE6, send Accept: */* and trigger
72
+ # the 'format.any' block incorrectly. See http://bit.ly/ie6_borken or http://bit.ly/ie6_borken2
73
+ # for a workaround.)
74
+ format.any(:json, :xml) do
75
+ request_http_basic_authentication 'Web Password'
76
+ end
77
+ end
78
+ end
79
+
80
+ # Store the URI of the current request in the session.
81
+ #
82
+ # We can return to this location by calling #redirect_back_or_default.
83
+ def store_location
84
+ session[:return_to] = request.request_uri
85
+ end
86
+
87
+ # Redirect to the URI stored by the most recent store_location call or
88
+ # to the passed default. Set an appropriately modified
89
+ # after_filter :store_location, :only => [:index, :new, :show, :edit]
90
+ # for any controller you want to be bounce-backable.
91
+ def redirect_back_or_default(default)
92
+ redirect_to(session[:return_to] || default)
93
+ session[:return_to] = nil
94
+ end
95
+
96
+ # Inclusion hook to make #current_<%= file_name %> and #logged_in?
97
+ # available as ActionView helper methods.
98
+ def self.included(base)
99
+ base.send :helper_method, :current_<%= file_name %>, :logged_in?, :authorized? if base.respond_to? :helper_method
100
+ end
101
+
102
+ #
103
+ # Login
104
+ #
105
+
106
+ # Called from #current_<%= file_name %>. First attempt to login by the <%= file_name %> id stored in the session.
107
+ def login_from_session
108
+ self.current_<%= file_name %> = <%= class_name %>.find_by_id(session[:<%= file_name %>_id]) if session[:<%= file_name %>_id]
109
+ end
110
+
111
+ # Called from #current_<%= file_name %>. Now, attempt to login by basic authentication information.
112
+ def login_from_basic_auth
113
+ authenticate_with_http_basic do |login, password|
114
+ self.current_<%= file_name %> = <%= class_name %>.authenticate(login, password)
115
+ end
116
+ end
117
+
118
+ #
119
+ # Logout
120
+ #
121
+
122
+ # Called from #current_<%= file_name %>. Finaly, attempt to login by an expiring token in the cookie.
123
+ # for the paranoid: we _should_ be storing <%= file_name %>_token = hash(cookie_token, request IP)
124
+ def login_from_cookie
125
+ <%= file_name %> = cookies[:auth_token] && <%= class_name %>.find_by_remember_token(cookies[:auth_token])
126
+ if <%= file_name %> && <%= file_name %>.remember_token?
127
+ self.current_<%= file_name %> = <%= file_name %>
128
+ handle_remember_cookie! false # freshen cookie token (keeping date)
129
+ self.current_<%= file_name %>
130
+ end
131
+ end
132
+
133
+ # This is ususally what you want; resetting the session willy-nilly wreaks
134
+ # havoc with forgery protection, and is only strictly necessary on login.
135
+ # However, **all session state variables should be unset here**.
136
+ def logout_keeping_session!
137
+ # Kill server-side auth cookie
138
+ @current_<%= file_name %>.forget_me if @current_<%= file_name %>.is_a? <%= class_name %>
139
+ @current_<%= file_name %> = false # not logged in, and don't do it for me
140
+ kill_remember_cookie! # Kill client-side auth cookie
141
+ session[:<%= file_name %>_id] = nil # keeps the session but kill our variable
142
+ # explicitly kill any other session variables you set
143
+ end
144
+
145
+ # The session should only be reset at the tail end of a form POST --
146
+ # otherwise the request forgery protection fails. It's only really necessary
147
+ # when you cross quarantine (logged-out to logged-in).
148
+ def logout_killing_session!
149
+ logout_keeping_session!
150
+ reset_session
151
+ end
152
+
153
+ #
154
+ # Remember_me Tokens
155
+ #
156
+ # Cookies shouldn't be allowed to persist past their freshness date,
157
+ # and they should be changed at each login
158
+
159
+ # Cookies shouldn't be allowed to persist past their freshness date,
160
+ # and they should be changed at each login
161
+
162
+ def valid_remember_cookie?
163
+ return nil unless @current_<%= file_name %>
164
+ (@current_<%= file_name %>.remember_token?) &&
165
+ (cookies[:auth_token] == @current_<%= file_name %>.remember_token)
166
+ end
167
+
168
+ # Refresh the cookie auth token if it exists, create it otherwise
169
+ def handle_remember_cookie!(new_cookie_flag)
170
+ return unless @current_<%= file_name %>
171
+ case
172
+ when valid_remember_cookie? then @current_<%= file_name %>.refresh_token # keeping same expiry date
173
+ when new_cookie_flag then @current_<%= file_name %>.remember_me
174
+ else @current_<%= file_name %>.forget_me
175
+ end
176
+ send_remember_cookie!
177
+ end
178
+
179
+ def kill_remember_cookie!
180
+ cookies.delete :auth_token
181
+ end
182
+
183
+ def send_remember_cookie!
184
+ cookies[:auth_token] = {
185
+ :value => @current_<%= file_name %>.remember_token,
186
+ :expires => @current_<%= file_name %>.remember_token_expires_at }
187
+ end
188
+
189
+ end
@@ -0,0 +1,12 @@
1
+ module AuthenticatedTestHelper
2
+ def log_in
3
+ <%= file_name %> = <%= class_name %>.make
4
+ @request.session[:<%= file_name %>_id] = <%= file_name %>.id
5
+ end
6
+
7
+ def authorize
8
+ <%= file_name %> = <%= class_name %>.make
9
+ @request.env["HTTP_AUTHORIZATION"] = ActionController::HttpAuthentication::Basic.encode_credentials(<%= file_name %>.login, <%= file_name %>.password)
10
+ end
11
+
12
+ end
@@ -0,0 +1,43 @@
1
+ # This controller handles the login/logout function of the site.
2
+ class <%= controller_class_name %>Controller < ApplicationController
3
+ # Be sure to include AuthenticationSystem in Application Controller instead
4
+ include AuthenticatedSystem
5
+
6
+ # render new.html.erb
7
+ def new
8
+ end
9
+
10
+ def create
11
+ logout_keeping_session!
12
+ <%= file_name %> = <%= class_name %>.authenticate(params[:login], params[:password])
13
+ if <%= file_name %>
14
+ # Protects against session fixation attacks, causes request forgery
15
+ # protection if user resubmits an earlier form using back
16
+ # button. Uncomment if you understand the tradeoffs.
17
+ # reset_session
18
+ self.current_<%= file_name %> = <%= file_name %>
19
+ new_cookie_flag = (params[:remember_me] == "1")
20
+ handle_remember_cookie! new_cookie_flag
21
+ redirect_back_or_default('/')
22
+ flash[:notice] = "Logged in successfully"
23
+ else
24
+ note_failed_signin
25
+ @login = params[:login]
26
+ @remember_me = params[:remember_me]
27
+ render :action => 'new'
28
+ end
29
+ end
30
+
31
+ def destroy
32
+ logout_killing_session!
33
+ flash[:notice] = "You have been logged out."
34
+ redirect_back_or_default('/')
35
+ end
36
+
37
+ protected
38
+ # Track failed login attempts
39
+ def note_failed_signin
40
+ flash[:error] = "Couldn't log you in as '#{params[:login]}'"
41
+ logger.warn "Failed login for '#{params[:login]}' from #{request.remote_ip} at #{Time.now.utc}"
42
+ end
43
+ end
@@ -0,0 +1,63 @@
1
+ Feature: Sign up
2
+ As a <%= file_name %>
3
+ I want to sign up
4
+ So that I can log in
5
+
6
+ Scenario: Navigating to the Sign Up page
7
+ Given I am not logged in
8
+ When I am on the home page
9
+ And I follow "Sign up"
10
+ Then I should be on the sign up page
11
+
12
+ Scenario: Create fails when creating an existing <%= file_name %>
13
+ Given I am not logged in
14
+ And someone with the login "testing" already exists
15
+ When I am on the sign up page
16
+ And I fill in "Login" with "testing"
17
+ And I fill in "Email" with "test@testing.com"
18
+ And I fill in "Password" with "password"
19
+ And I fill in "Confirm Password" with "password"
20
+ And I press "Sign up"
21
+ Then I should see "Login has already been taken"
22
+ And I should not be logged in
23
+
24
+ Scenario: Create fails when given an invalid email
25
+ Given I am not logged in
26
+ When I am on the sign up page
27
+ And I fill in "Login" with "testing"
28
+ And I fill in "Email" with "invalid.email"
29
+ And I fill in "Password" with "password"
30
+ And I fill in "Confirm Password" with "password"
31
+ And I press "Sign up"
32
+ Then I should see "Email should look like an email address"
33
+ And I should not be logged in
34
+
35
+ Scenario: Create fails when no password is given
36
+ Given I am not logged in
37
+ When I am on the sign up page
38
+ And I fill in "Login" with "testing"
39
+ And I fill in "Email" with "test@testing.com"
40
+ And I press "Sign up"
41
+ Then I should see "Password can't be blank"
42
+ And I should not be logged in
43
+
44
+ Scenario: Create fails when no password confirmation is given
45
+ Given I am not logged in
46
+ When I am on the sign up page
47
+ And I fill in "Login" with "testing"
48
+ And I fill in "Email" with "test@testing.com"
49
+ And I fill in "Password" with "password"
50
+ And I press "Sign up"
51
+ Then I should see "Password confirmation can't be blank"
52
+ And I should not be logged in
53
+
54
+ Scenario: Create successful when given valid parameters
55
+ Given I am not logged in
56
+ When I am on the sign up page
57
+ And I fill in "Login" with "testing"
58
+ And I fill in "Email" with "test@testing.com"
59
+ And I fill in "Password" with "password"
60
+ And I fill in "Confirm Password" with "password"
61
+ And I press "Sign up"
62
+ Then I should be on the home page
63
+ And I should be logged in
@@ -0,0 +1,77 @@
1
+ Feature: Logging in
2
+ As a <%= file_name %>
3
+ I want to log in
4
+ So that I can use the application
5
+
6
+ Scenario: Navigating to the Login page
7
+ Given I am not logged in
8
+ When I follow "Log in"
9
+ Then I should be on the login page
10
+
11
+ Scenario: Existing <%= file_name %> can log in
12
+ Given I am not logged in
13
+ And someone with the login "testing" already exists
14
+ When I am on the login page
15
+ And I fill in "Login" with "testing"
16
+ And I fill in "Password" with "testing"
17
+ And I press "Log in"
18
+ Then I should be on the home page
19
+ And I should be logged in
20
+ And I should not have a remember token
21
+
22
+ Scenario: Existing <%= file_name %> can log in and be remembered
23
+ Given I am not logged in
24
+ And someone with the login "testing" already exists
25
+ And I am on the login page
26
+ When I fill in "Login" with "testing"
27
+ And I fill in "Password" with "testing"
28
+ And I check "remember_me"
29
+ And I press "Log in"
30
+ Then I should be on the home page
31
+ And I should be logged in
32
+ And I should have a remember token
33
+
34
+ Scenario: Logged in <%= file_name %> who fails log in should be logged out
35
+ Given I am logged in
36
+ And I am on the login page
37
+ When I fill in "Login" with "testing"
38
+ And I fill in "Password" with "testing1234fail"
39
+ And I press "Log in"
40
+ Then I should see "Couldn't log you in as 'testing'"
41
+ And I should not be logged in
42
+
43
+ Scenario: Logging in with an invalid login should fail
44
+ Given I am not logged in
45
+ And someone with the login "testing" already exists
46
+ And I am on the login page
47
+ When I fill in "Login" with "testing123"
48
+ And I fill in "Password" with "testing"
49
+ And I press "Log in"
50
+ Then I should see "Couldn't log you in as 'testing123'"
51
+ And I should not be logged in
52
+
53
+ Scenario: Logging in with an invalid password should fail
54
+ Given I am not logged in
55
+ And someone with the login "testing" already exists
56
+ When I go to the login page
57
+ And I fill in "Login" with "testing"
58
+ And I fill in "Password" with "testing123"
59
+ And I press "Log in"
60
+ Then I should see "Couldn't log you in as 'testing'"
61
+ And I should not be logged in
62
+
63
+ Scenario: Logged out <%= file_name %> can log out
64
+ Given I am not logged in
65
+ And I am on the home page
66
+ When I go to the logout page
67
+ Then I should see "You have been logged out"
68
+ And I should be on the home page
69
+ And I should not be logged in
70
+
71
+ Scenario: Logged in <%= file_name %> can log out
72
+ Given I am logged in
73
+ And I am on the home page
74
+ When I follow "Log out"
75
+ Then I should see "You have been logged out"
76
+ And I should be on the home page
77
+ And I should not be logged in