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
data/CHANGELOG ADDED
@@ -0,0 +1,68 @@
1
+ h1. Internal Changes to code
2
+
3
+ As always, this is just a copy-and-pasted version of the CHANGELOG file in the source code tree.
4
+
5
+ h2. Changes for the May, 2008 version of restful-authentication
6
+
7
+ h3. Changes to user model
8
+
9
+ * recently_activated? belongs only if stateful
10
+ * Gave migration a 40-char limit on remember_token & an index on users by login
11
+ * **Much** stricter login and email validation
12
+ * put length constraints in migration too
13
+ * password in 6, 40
14
+ * salt and remember_token now much less predictability
15
+
16
+ h3. Changes to session_controller
17
+
18
+ * use uniform logout function
19
+ * use uniform remember_cookie functions
20
+ * avoid calling logged_in? which will auto-log-you-in (safe in the face of
21
+ logout! call, but idiot-proof)
22
+ * Moved reset_session into only the "now logged in" branch
23
+ ** wherever it goes, it has to be in front of the current_user= call
24
+ ** See more in README-Tradeoffs.txt
25
+ * made a place to take action on failed login attempt
26
+ * recycle login and remember_me setting on failed login
27
+ * nil'ed out the password field in 'new' view
28
+
29
+ h3. Changes to users_controller
30
+
31
+ * use uniform logout function
32
+ * use uniform remember_cookie functions
33
+ * Moved reset_session into only the "now logged in" branch
34
+ ** wherever it goes, it has to be in front of the current_user= call
35
+ ** See more in README-Tradeoffs.txt
36
+ * made the implicit login only happen for non-activationed sites
37
+ * On a failed signup, kick you back to the signin screen (but strip out the password & confirmation)
38
+ * more descriptive error messages in activate()
39
+
40
+ h3. users_helper
41
+
42
+ * link_to_user, link_to_current_user, link_to_signin_with_IP
43
+ * if_authorized(action, resource, &block) view function (with appropriate
44
+ warning)
45
+
46
+ h3. authenticated_system
47
+
48
+ * Made authorized? take optional arguments action=nil, resource=nil, *args
49
+ This makes its signature better match traditional approaches to access control
50
+ eg Reference Monitor in "Security Patterns":http://www.securitypatterns.org/patterns.html)
51
+ * authorized? should be a helper too
52
+ * added uniform logout! methods
53
+ * format.any (as found in access_denied) doesn't work until
54
+ http://dev.rubyonrails.org/changeset/8987 lands.
55
+ * cookies are now refreshed each time we cross the logged out/in barrier, as
56
+ "best":http://palisade.plynt.com/issues/2004Jul/safe-auth-practices/
57
+ "practice":http://www.owasp.org/index.php/Session_Management#Regeneration_of_Session_Tokens
58
+
59
+ h3. Other
60
+
61
+ * Used escapes <%= %> in email templates (among other reasons, so courtenay's
62
+ "'dumbass' test":http://tinyurl.com/684g9t doesn't complain)
63
+ * Added site key to generator, users.yml.
64
+ * Made site key generation idempotent in the most crude and hackish way
65
+ * 100% coverage apart from the stateful code. (needed some access_control
66
+ checks, and the http_auth stuff)
67
+ * Stories!
68
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 rick olson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,224 @@
1
+ h1. "Restful Authentication Generator":http://github.com/technoweenie/restful-authentication
2
+
3
+ This widely-used plugin provides a foundation for securely managing user
4
+ authentication:
5
+ * Login / logout
6
+ * Secure password handling
7
+ * Account activation by validating email
8
+ * Account approval / disabling by admin
9
+ * Rudimentary hooks for authorization and access control.
10
+
11
+ Several features were updated in May, 2008.
12
+ * "Stable newer version":http://github.com/technoweenie/restful-authentication/tree/master
13
+ * "'Classic' (backward-compatible) version":http://github.com/technoweenie/restful-authentication/tree/classic
14
+ * "Experimental version":http://github.com/technoweenie/restful-authentication/tree/modular (Much more modular, needs testing & review)
15
+
16
+ !! important: if you upgrade your site, existing user account !!
17
+ !! passwords will stop working unless you use --old-passwords !!
18
+
19
+ ***************************************************************************
20
+
21
+ h2. Issue Tracker
22
+
23
+ Please submit any bugs or annoyances on the lighthouse tracker at
24
+ * "http://rails_security.lighthouseapp.com/projects/15332-restful_authentication/overview":http://rails_security.lighthouseapp.com/projects/15332-restful_authentication/overview
25
+
26
+ For anything simple enough, please github message both maintainers: Rick Olson
27
+ ("technoweenie":http://github.com/technoweenie) and Flip Kromer
28
+ ("mrflip":http://github.com/mrflip).
29
+
30
+ ***************************************************************************
31
+
32
+ h2. Documentation
33
+
34
+ This page has notes on
35
+ * "Installation":#INSTALL
36
+ * "New Features":#AWESOME
37
+ * "After installing":#POST-INSTALL
38
+
39
+ See the "wiki":http://github.com/technoweenie/restful-authentication/wikis/home
40
+ (or the notes/ directory) if you want to learn more about:
41
+
42
+ * "Extensions, Addons and Alternatives":addons such as HAML templates
43
+ * "Security Design Patterns":security-patterns with "snazzy diagram":http://github.com/technoweenie/restful-authentication/tree/master/notes/SecurityFramework.png
44
+ * [[Authentication]] -- Lets a visitor identify herself (and lay claim to her corresponding Roles and measure of Trust)
45
+ * "Trust Metrics":Trustification -- Confidence we can rely on the outcomes of this visitor's actions.
46
+ * [[Authorization]] and Policy -- Based on trust and identity, what actions may this visitor perform?
47
+ * [[Access Control]] -- How the Authorization policy is actually enforced in your code (A: hopefully without turning it into a spaghetti of if thens)
48
+ * [[Rails Plugins]] for Authentication, Trust, Authorization and Access Control
49
+ * [[Tradeoffs]] -- for the paranoid or the curious, a rundown of tradeoffs made in the code
50
+ * [[CHANGELOG]] -- Summary of changes to internals
51
+ * [[TODO]] -- Ideas for how you can help
52
+
53
+ These best version of the release notes are in the notes/ directory in the
54
+ "source code":http://github.com/technoweenie/restful-authentication/tree/master
55
+ -- look there for the latest version. The wiki versions are taken (manually)
56
+ from there.
57
+
58
+ ***************************************************************************
59
+
60
+ <a id="AWESOME"/> </a>
61
+ h2. Exciting new features
62
+
63
+ h3. Stories
64
+
65
+ There are now "Cucumber":http://wiki.github.com/aslakhellesoy/cucumber/home features that allow expressive, enjoyable tests for the
66
+ authentication code. The flexible code for resource testing in stories was
67
+ extended from "Ben Mabey's.":http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/
68
+
69
+ h3. Modularize to match security design patterns:
70
+
71
+ * Authentication (currently: password, browser cookie token, HTTP basic)
72
+ * Trust metric (email validation)
73
+ * Authorization (stateful roles)
74
+ * Leave a flexible framework that will play nicely with other access control / policy definition / trust metric plugins
75
+
76
+ h3. Other
77
+
78
+ * Added a few helper methods for linking to user pages
79
+ * Uniform handling of logout, remember_token
80
+ * Stricter email, login field validation
81
+ * Minor security fixes -- see CHANGELOG
82
+
83
+ ***************************************************************************
84
+
85
+ h2. Non-backwards compatible Changes
86
+
87
+ Here are a few changes in the May 2008 release that increase "Defense in Depth"
88
+ but may require changes to existing accounts
89
+
90
+ * If you have an existing site, none of these changes are compelling enough to
91
+ warrant migrating your userbase.
92
+ * If you are generating for a new site, all of these changes are low-impact.
93
+ You should apply them.
94
+
95
+ h3. Passwords
96
+
97
+ The new password encryption (using a site key salt and stretching) will break
98
+ existing user accounts' passwords. We recommend you use the --old-passwords
99
+ option or write a migration tool and submit it as a patch. See the
100
+ [[Tradeoffs]] note for more information.
101
+
102
+ h3. Validations
103
+
104
+ By default, email and usernames are validated against a somewhat strict pattern; your users' values may be now illegal. Adjust to suit.
105
+
106
+ ***************************************************************************
107
+
108
+ <a id="INSTALL"/> </a>
109
+ h2. Installation
110
+
111
+ This is a basic restful authentication generator for rails, taken from
112
+ acts as authenticated. Currently it requires Rails 1.2.6 or above.
113
+
114
+ **IMPORTANT FOR RAILS > 2.1 USERS** To avoid a @NameError@ exception ("lighthouse tracker ticket":http://rails_security.lighthouseapp.com/projects/15332-restful_authentication/tickets/2-not-a-valid-constant-name-errors#ticket-2-2), check out the code to have an _underscore_ and not _dash_ in its name:
115
+ * either use <code>git clone git://github.com/technoweenie/restful-authentication.git restful_authentication</code>
116
+ * or rename the plugin's directory to be <code>restful_authentication</code> after fetching it.
117
+
118
+ To use the generator:
119
+
120
+ ./script/generate authenticated user sessions \
121
+ --include-activation \
122
+ --stateful \
123
+ --rspec \
124
+ --skip-migration \
125
+ --skip-routes \
126
+ --old-passwords
127
+
128
+ * The first parameter specifies the model that gets created in signup (typically
129
+ a user or account model). A model with migration is created, as well as a
130
+ basic controller with the create method. You probably want to say "User" here.
131
+
132
+ * The second parameter specifies the session controller name. This is the
133
+ controller that handles the actual login/logout function on the site.
134
+ (probably: "Session").
135
+
136
+ * --include-activation: Generates the code for a ActionMailer and its respective
137
+ Activation Code through email.
138
+
139
+ * --stateful: Builds in support for acts_as_state_machine and generates
140
+ activation code. (@--stateful@ implies @--include-activation@). Based on the
141
+ idea at [[http://www.vaporbase.com/postings/stateful_authentication]]. Passing
142
+ @--skip-migration@ will skip the user migration, and @--skip-routes@ will skip
143
+ resource generation -- both useful if you've already run this generator.
144
+ (Needs the "acts_as_state_machine plugin":http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/,
145
+ but new installs should probably run with @--aasm@ instead.)
146
+
147
+ * --aasm: Works the same as stateful but uses the "updated aasm gem":http://github.com/rubyist/aasm/tree/master
148
+
149
+ * --rspec: Generate RSpec tests and Stories in place of standard rails tests.
150
+ This requires the
151
+ "RSpec and Rspec-on-rails plugins":http://rspec.info/
152
+ (make sure you "./script/generate rspec" after installing RSpec.) The rspec
153
+ and story suite are much more thorough than the rails tests, and changes are
154
+ unlikely to be backported.
155
+
156
+ * --old-passwords: Use the older password scheme (see [[#COMPATIBILITY]], above)
157
+
158
+ * --skip-migration: Don't generate a migration file for this model
159
+
160
+ * --skip-routes: Don't generate a resource line in @config/routes.rb@
161
+
162
+ ***************************************************************************
163
+ <a id="POST-INSTALL"/> </a>
164
+ h2. After installing
165
+
166
+ The below assumes a Model named 'User' and a Controller named 'Session'; please
167
+ alter to suit. There are additional security minutae in @notes/README-Tradeoffs@
168
+ -- only the paranoid or the curious need bother, though.
169
+
170
+ * Add these familiar login URLs to your @config/routes.rb@ if you like:
171
+
172
+ <pre><code>
173
+ map.signup '/signup', :controller => 'users', :action => 'new'
174
+ map.login '/login', :controller => 'session', :action => 'new'
175
+ map.logout '/logout', :controller => 'session', :action => 'destroy'
176
+ </code></pre>
177
+
178
+ * With @--include-activation@, also add to your @config/routes.rb@:
179
+
180
+ <pre><code>
181
+ map.activate '/activate/:activation_code', :controller => 'users', :action => 'activate', :activation_code => nil
182
+ </code></pre>
183
+
184
+ and add an observer to @config/environment.rb@:
185
+
186
+ <pre><code>
187
+ config.active_record.observers = :user_observer
188
+ </code></pre>
189
+
190
+ Pay attention, may be this is not an issue for everybody, but if you should
191
+ have problems, that the sent activation_code does match with that in the
192
+ database stored, reload your user object before sending its data through email
193
+ something like:
194
+
195
+ <pre><code>
196
+ class UserObserver < ActiveRecord::Observer
197
+ def after_create(user)
198
+ user.reload
199
+ UserMailer.deliver_signup_notification(user)
200
+ end
201
+ def after_save(user)
202
+ user.reload
203
+ UserMailer.deliver_activation(user) if user.recently_activated?
204
+ end
205
+ end
206
+ </code></pre>
207
+
208
+
209
+ * With @--stateful@, add an observer to config/environment.rb:
210
+
211
+ <pre><code>
212
+ config.active_record.observers = :user_observer
213
+ </code></pre>
214
+
215
+ and modify the users resource line to read
216
+
217
+ map.resources :users, :member => { :suspend => :put,
218
+ :unsuspend => :put,
219
+ :purge => :delete }
220
+
221
+ * If you use a public repository for your code (such as github, rubyforge,
222
+ gitorious, etc.) make sure to NOT post your site_keys.rb (add a line like
223
+ '/config/initializers/site_keys.rb' to your .gitignore or do the svn ignore
224
+ dance), but make sure you DO keep it backed up somewhere safe.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the restful_authentication plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the restful_authentication plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'RestfulAuthentication'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ gemspec = eval(File.read("#{File.dirname(__FILE__)}/restful_authentication.gemspec"))
26
+ PKG_NAME = gemspec.name
27
+ PKG_VERSION = gemspec.version
28
+
29
+ Rake::GemPackageTask.new(gemspec) do |pkg|
30
+ pkg.need_zip = true
31
+ pkg.need_tar = true
32
+ end
33
+
34
+ desc "Update gemspec from existing one by regenerating path globs specified in *.gemspec.yml or defaults to liberal file globs."
35
+ task :gemspec_update do
36
+ if (gemspec_file = Dir['*.gemspec'][0])
37
+ original_gemspec = eval(File.read(gemspec_file))
38
+ if File.exists?("#{gemspec_file}.yml")
39
+ require 'yaml'
40
+ YAML::load_file("#{gemspec_file}.yml").each do |attribute, globs|
41
+ original_gemspec.send("#{attribute}=", FileList[globs])
42
+ end
43
+ else
44
+ # liberal defaults
45
+ original_gemspec.files = FileList["**/*"]
46
+ test_directories = original_gemspec.test_files.grep(/\//).map {|e| e[/^[^\/]+/]}.compact.uniq
47
+ original_gemspec.test_files = FileList["{#{test_directories.join(',')}}/**/*"] unless test_directories.empty?
48
+ end
49
+ File.open(gemspec_file, 'w') {|f| f.write(original_gemspec.to_ruby) }
50
+ puts "Updated gemspec."
51
+ else
52
+ puts "No existing gemspec file found."
53
+ end
54
+ end
data/TODO ADDED
@@ -0,0 +1,15 @@
1
+
2
+ h3. Authentication security projects for a later date
3
+
4
+
5
+ * Track 'failed logins this hour' and demand a captcha after say 5 failed logins
6
+ ("RECAPTCHA plugin.":http://agilewebdevelopment.com/plugins/recaptcha)
7
+ "De-proxy-ficate IP address": http://wiki.codemongers.com/NginxHttpRealIpModule
8
+
9
+ * Make cookie spoofing a little harder: we set the user's cookie to
10
+ (remember_token), but store digest(remember_token, request_IP). A CSRF cookie
11
+ spoofer has to then at least also spoof the user's originating IP
12
+ (see "Secure Programs HOWTO":http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO/web-authentication.html)
13
+
14
+ * Log HTTP request on authentication / authorization failures
15
+ http://palisade.plynt.com/issues/2004Jul/safe-auth-practices
@@ -0,0 +1 @@
1
+ ./script/generate authenticated USERMODEL CONTROLLERNAME
@@ -0,0 +1,481 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/lib/insert_routes.rb")
2
+ require 'digest/sha1'
3
+ class AuthenticatedGenerator < Rails::Generator::NamedBase
4
+ default_options :skip_migration => false,
5
+ :skip_routes => false,
6
+ :old_passwords => false,
7
+ :include_activation => false
8
+
9
+ attr_reader :controller_name,
10
+ :controller_class_path,
11
+ :controller_file_path,
12
+ :controller_class_nesting,
13
+ :controller_class_nesting_depth,
14
+ :controller_class_name,
15
+ :controller_singular_name,
16
+ :controller_plural_name,
17
+ :controller_routing_name, # new_session_path
18
+ :controller_routing_path, # /session/new
19
+ :controller_controller_name, # sessions
20
+ :controller_file_name
21
+ alias_method :controller_table_name, :controller_plural_name
22
+ attr_reader :model_controller_name,
23
+ :model_controller_class_path,
24
+ :model_controller_file_path,
25
+ :model_controller_class_nesting,
26
+ :model_controller_class_nesting_depth,
27
+ :model_controller_class_name,
28
+ :model_controller_singular_name,
29
+ :model_controller_plural_name,
30
+ :model_controller_routing_name, # new_user_path
31
+ :model_controller_routing_path, # /users/new
32
+ :model_controller_controller_name # users
33
+ alias_method :model_controller_file_name, :model_controller_singular_name
34
+ alias_method :model_controller_table_name, :model_controller_plural_name
35
+
36
+ def initialize(runtime_args, runtime_options = {})
37
+ super
38
+
39
+ @rspec = has_rspec?
40
+
41
+ @controller_name = (args.shift || 'sessions').pluralize
42
+ @model_controller_name = @name.pluralize
43
+
44
+ # sessions controller
45
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
46
+ @controller_class_name_without_nesting, @controller_file_name, @controller_plural_name = inflect_names(base_name)
47
+ @controller_singular_name = @controller_file_name.singularize
48
+ if @controller_class_nesting.empty?
49
+ @controller_class_name = @controller_class_name_without_nesting
50
+ else
51
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
52
+ end
53
+ @controller_routing_name = @controller_singular_name
54
+ @controller_routing_path = @controller_file_path.singularize
55
+ @controller_controller_name = @controller_plural_name
56
+
57
+ # model controller
58
+ base_name, @model_controller_class_path, @model_controller_file_path, @model_controller_class_nesting, @model_controller_class_nesting_depth = extract_modules(@model_controller_name)
59
+ @model_controller_class_name_without_nesting, @model_controller_singular_name, @model_controller_plural_name = inflect_names(base_name)
60
+
61
+ if @model_controller_class_nesting.empty?
62
+ @model_controller_class_name = @model_controller_class_name_without_nesting
63
+ else
64
+ @model_controller_class_name = "#{@model_controller_class_nesting}::#{@model_controller_class_name_without_nesting}"
65
+ end
66
+ @model_controller_routing_name = @table_name
67
+ @model_controller_routing_path = @model_controller_file_path
68
+ @model_controller_controller_name = @model_controller_plural_name
69
+
70
+ load_or_initialize_site_keys
71
+
72
+ if options[:dump_generator_attribute_names]
73
+ dump_generator_attribute_names
74
+ end
75
+ end
76
+
77
+ def manifest
78
+ recorded_session = record do |m|
79
+ # Check for class naming collisions.
80
+ m.class_collisions controller_class_path, "#{controller_class_name}Controller", # Sessions Controller
81
+ "#{controller_class_name}Helper"
82
+ m.class_collisions model_controller_class_path, "#{model_controller_class_name}Controller", # Model Controller
83
+ "#{model_controller_class_name}Helper"
84
+ m.class_collisions class_path, "#{class_name}", "#{class_name}Mailer", "#{class_name}MailerTest", "#{class_name}Observer"
85
+ m.class_collisions [], 'AuthenticatedSystem', 'AuthenticatedTestHelper'
86
+
87
+ # Controller, helper, views, and test directories.
88
+ m.directory File.join('app/models', class_path)
89
+ m.directory File.join('app/controllers', controller_class_path)
90
+ m.directory File.join('app/controllers', model_controller_class_path)
91
+ m.directory File.join('app/helpers', controller_class_path)
92
+ m.directory File.join('app/views', controller_class_path, controller_file_name)
93
+ m.directory File.join('app/views', class_path, "#{file_name}_mailer") if options[:include_activation]
94
+
95
+ m.directory File.join('app/controllers', model_controller_class_path)
96
+ m.directory File.join('app/helpers', model_controller_class_path)
97
+ m.directory File.join('app/views', model_controller_class_path, model_controller_file_name)
98
+ m.directory File.join('config/initializers')
99
+
100
+ if @rspec
101
+ m.directory File.join('spec/controllers', controller_class_path)
102
+ m.directory File.join('spec/controllers', model_controller_class_path)
103
+ m.directory File.join('spec/models', class_path)
104
+ m.directory File.join('spec/helpers', model_controller_class_path)
105
+ m.directory File.join('spec/blueprints', class_path)
106
+ m.directory 'features'
107
+ m.directory File.join('features', 'step_definitions')
108
+ else
109
+ m.directory File.join('test/functional', controller_class_path)
110
+ m.directory File.join('test/functional', model_controller_class_path)
111
+ m.directory File.join('test/unit', class_path)
112
+ m.directory File.join('test/blueprints', class_path)
113
+ end
114
+
115
+ m.template 'model.rb',
116
+ File.join('app/models',
117
+ class_path,
118
+ "#{file_name}.rb")
119
+
120
+ if options[:include_activation]
121
+ %w( mailer observer ).each do |model_type|
122
+ m.template "#{model_type}.rb", File.join('app/models',
123
+ class_path,
124
+ "#{file_name}_#{model_type}.rb")
125
+ end
126
+ end
127
+
128
+ m.template 'controller.rb',
129
+ File.join('app/controllers',
130
+ controller_class_path,
131
+ "#{controller_file_name}_controller.rb")
132
+
133
+ m.template 'model_controller.rb',
134
+ File.join('app/controllers',
135
+ model_controller_class_path,
136
+ "#{model_controller_file_name}_controller.rb")
137
+
138
+ m.template 'authenticated_system.rb',
139
+ File.join('lib', 'authenticated_system.rb')
140
+
141
+ m.template 'authenticated_test_helper.rb',
142
+ File.join('lib', 'authenticated_test_helper.rb')
143
+
144
+ m.template 'site_keys.rb', site_keys_file
145
+
146
+ if @rspec
147
+ # RSpec Specs
148
+ m.template 'spec/controllers/users_controller_spec.rb',
149
+ File.join('spec/controllers',
150
+ model_controller_class_path,
151
+ "#{model_controller_file_name}_controller_spec.rb")
152
+ m.template 'spec/controllers/sessions_controller_spec.rb',
153
+ File.join('spec/controllers',
154
+ controller_class_path,
155
+ "#{controller_file_name}_controller_spec.rb")
156
+ m.template 'spec/controllers/access_control_spec.rb',
157
+ File.join('spec/controllers',
158
+ controller_class_path,
159
+ "access_control_spec.rb")
160
+ m.template 'spec/controllers/authenticated_system_spec.rb',
161
+ File.join('spec/controllers',
162
+ controller_class_path,
163
+ "authenticated_system_spec.rb")
164
+ m.template 'spec/helpers/users_helper_spec.rb',
165
+ File.join('spec/helpers',
166
+ model_controller_class_path,
167
+ "#{table_name}_helper_spec.rb")
168
+ m.template 'spec/models/user_spec.rb',
169
+ File.join('spec/models',
170
+ class_path,
171
+ "#{file_name}_spec.rb")
172
+ m.template 'spec/blueprints/user.rb',
173
+ File.join('spec/blueprints',
174
+ class_path,
175
+ "#{file_name}.rb")
176
+
177
+ # Cucumber features
178
+ m.template 'features/step_definitions/user_steps.rb',
179
+ File.join('features/step_definitions/', "#{file_name}_steps.rb")
180
+ m.template 'features/accounts.feature',
181
+ File.join('features', 'accounts.feature')
182
+ m.template 'features/sessions.feature',
183
+ File.join('features', 'sessions.feature')
184
+ m.template 'features/step_definitions/ra_env.rb',
185
+ File.join('features', 'step_definitions', 'ra_env.rb')
186
+ m.template 'machinist_spec.rb',
187
+ File.join("config", "initializers", "machinist.rb")
188
+
189
+ else
190
+ m.template 'test/functional_test.rb',
191
+ File.join('test/functional',
192
+ controller_class_path,
193
+ "#{controller_file_name}_controller_test.rb")
194
+ m.template 'test/model_functional_test.rb',
195
+ File.join('test/functional',
196
+ model_controller_class_path,
197
+ "#{model_controller_file_name}_controller_test.rb")
198
+ m.template 'test/unit_test.rb',
199
+ File.join('test/unit',
200
+ class_path,
201
+ "#{file_name}_test.rb")
202
+ m.template 'spec/blueprints/user.rb',
203
+ File.join('test/blueprints',
204
+ class_path,
205
+ "#{file_name}.rb")
206
+ m.template 'machinist_test.rb',
207
+ File.join("config", "initializers", "machinist.rb")
208
+ if options[:include_activation]
209
+ m.template 'test/mailer_test.rb', File.join('test/unit', class_path, "#{file_name}_mailer_test.rb")
210
+ end
211
+ end
212
+
213
+ m.template 'helper.rb',
214
+ File.join('app/helpers',
215
+ controller_class_path,
216
+ "#{controller_file_name}_helper.rb")
217
+
218
+ m.template 'model_helper.rb',
219
+ File.join('app/helpers',
220
+ model_controller_class_path,
221
+ "#{model_controller_file_name}_helper.rb")
222
+
223
+
224
+ # Controller templates
225
+ m.template 'login.html.erb', File.join('app/views', controller_class_path, controller_file_name, "new.html.erb")
226
+ m.template 'signup.html.erb', File.join('app/views', model_controller_class_path, model_controller_file_name, "new.html.erb")
227
+ m.template '_model_partial.html.erb', File.join('app/views', model_controller_class_path, model_controller_file_name, "_#{file_name}_bar.html.erb")
228
+
229
+ if options[:include_activation]
230
+ # Mailer templates
231
+ %w( activation signup_notification ).each do |action|
232
+ m.template "#{action}.erb",
233
+ File.join('app/views', "#{file_name}_mailer", "#{action}.erb")
234
+ end
235
+ end
236
+
237
+ unless options[:skip_migration]
238
+ m.migration_template 'migration.rb', 'db/migrate', :assigns => {
239
+ :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
240
+ }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
241
+ end
242
+ unless options[:skip_routes]
243
+ # Note that this fails for nested classes -- you're on your own with setting up the routes.
244
+ m.route_resource controller_singular_name
245
+ m.route_resources model_controller_plural_name
246
+ m.route_name('signup', '/signup', {:controller => model_controller_plural_name, :action => 'new'})
247
+ m.route_name('register', '/register', {:controller => model_controller_plural_name, :action => 'create'})
248
+ m.route_name('login', '/login', {:controller => controller_controller_name, :action => 'new'})
249
+ m.route_name('logout', '/logout', {:controller => controller_controller_name, :action => 'destroy'})
250
+ end
251
+ end
252
+
253
+ #
254
+ # Post-install notes
255
+ #
256
+ action = File.basename($0) # grok the action from './script/generate' or whatever
257
+ case action
258
+ when "generate"
259
+ puts "Ready to generate."
260
+ puts ("-" * 70)
261
+ puts "Once finished, don't forget to:"
262
+ puts
263
+ if options[:include_activation]
264
+ puts "- Add an observer to config/environment.rb"
265
+ puts " config.active_record.observers = :#{file_name}_observer"
266
+ end
267
+ if options[:aasm]
268
+ puts "- Install the acts_as_state_machine gem:"
269
+ puts " sudo gem sources -a http://gems.github.com (If you haven't already)"
270
+ puts " sudo gem install rubyist-aasm"
271
+ elsif options[:stateful]
272
+ puts "- Install the acts_as_state_machine plugin:"
273
+ puts " svn export http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk vendor/plugins/acts_as_state_machine"
274
+ end
275
+ puts
276
+ puts ("-" * 70)
277
+ puts
278
+ puts "- Add the following to your config/environments/cucumber.rb: "
279
+ puts
280
+ puts %( config.gem 'pickle')
281
+ puts
282
+ puts "- Add routes to these resources. In config/routes.rb, insert routes like:"
283
+ puts %( map.signup '/signup', :controller => '#{model_controller_file_name}', :action => 'new')
284
+ puts %( map.login '/login', :controller => '#{controller_file_name}', :action => 'new')
285
+ puts %( map.logout '/logout', :controller => '#{controller_file_name}', :action => 'destroy')
286
+ if options[:include_activation]
287
+ puts %( map.activate '/activate/:activation_code', :controller => '#{model_controller_file_name}', :action => 'activate', :activation_code => nil)
288
+ end
289
+ if options[:stateful]
290
+ puts " and modify the map.resources :#{model_controller_file_name} line to include these actions:"
291
+ puts " map.resources :#{model_controller_file_name}, :member => { :suspend => :put, :unsuspend => :put, :purge => :delete }"
292
+ end
293
+ puts
294
+ puts ("-" * 70)
295
+ puts
296
+ if $rest_auth_site_key_from_generator.blank?
297
+ puts "You've set a nil site key. This preserves existing users' passwords,"
298
+ puts "but allows dictionary attacks in the unlikely event your database is"
299
+ puts "compromised and your site code is not. See the README for more."
300
+ elsif $rest_auth_keys_are_new
301
+ puts "We've create a new site key in #{site_keys_file}. If you have existing"
302
+ puts "user accounts their passwords will no longer work (see README). As always,"
303
+ puts "keep this file safe but don't post it in public."
304
+ else
305
+ puts "We've reused the existing site key in #{site_keys_file}. As always,"
306
+ puts "keep this file safe but don't post it in public."
307
+ end
308
+ puts
309
+ puts ("-" * 70)
310
+ when "destroy"
311
+ puts
312
+ puts ("-" * 70)
313
+ puts
314
+ puts "Thanks for using restful_authentication"
315
+ puts
316
+ puts "Don't forget to comment out the observer line in environment.rb"
317
+ puts " (This was optional so it may not even be there)"
318
+ puts " # config.active_record.observers = :#{file_name}_observer"
319
+ puts
320
+ puts ("-" * 70)
321
+ puts
322
+ else
323
+ puts "Didn't understand the action '#{action}' -- you might have missed the 'after running me' instructions."
324
+ end
325
+
326
+ #
327
+ # Do the thing
328
+ #
329
+ recorded_session
330
+ end
331
+
332
+ def has_rspec?
333
+ spec_dir = File.join(RAILS_ROOT, 'spec')
334
+ options[:rspec] ||= (File.exist?(spec_dir) && File.directory?(spec_dir)) unless (options[:rspec] == false)
335
+ end
336
+
337
+ #
338
+ # !! These must match the corresponding routines in by_password.rb !!
339
+ #
340
+ def secure_digest(*args)
341
+ Digest::SHA1.hexdigest(args.flatten.join('--'))
342
+ end
343
+ def make_token
344
+ secure_digest(Time.now, (1..10).map{ rand.to_s })
345
+ end
346
+ def password_digest(password, salt)
347
+ digest = $rest_auth_site_key_from_generator
348
+ $rest_auth_digest_stretches_from_generator.times do
349
+ digest = secure_digest(digest, salt, password, $rest_auth_site_key_from_generator)
350
+ end
351
+ digest
352
+ end
353
+
354
+ #
355
+ # Try to be idempotent:
356
+ # pull in the existing site key if any,
357
+ # seed it with reasonable defaults otherwise
358
+ #
359
+ def load_or_initialize_site_keys
360
+ case
361
+ when defined? REST_AUTH_SITE_KEY
362
+ if (options[:old_passwords]) && ((! REST_AUTH_SITE_KEY.blank?) || (REST_AUTH_DIGEST_STRETCHES != 1))
363
+ raise "You have a site key, but --old-passwords will overwrite it. If this is really what you want, move the file #{site_keys_file} and re-run."
364
+ end
365
+ $rest_auth_site_key_from_generator = REST_AUTH_SITE_KEY
366
+ $rest_auth_digest_stretches_from_generator = REST_AUTH_DIGEST_STRETCHES
367
+ when options[:old_passwords]
368
+ $rest_auth_site_key_from_generator = nil
369
+ $rest_auth_digest_stretches_from_generator = 1
370
+ $rest_auth_keys_are_new = true
371
+ else
372
+ $rest_auth_site_key_from_generator = make_token
373
+ $rest_auth_digest_stretches_from_generator = 10
374
+ $rest_auth_keys_are_new = true
375
+ end
376
+ end
377
+ def site_keys_file
378
+ File.join("config", "initializers", "site_keys.rb")
379
+ end
380
+
381
+ protected
382
+ # Override with your own usage banner.
383
+ def banner
384
+ "Usage: #{$0} authenticated ModelName [ControllerName]"
385
+ end
386
+
387
+ def add_options!(opt)
388
+ opt.separator ''
389
+ opt.separator 'Options:'
390
+ opt.on("--skip-migration",
391
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
392
+ opt.on("--include-activation",
393
+ "Generate signup 'activation code' confirmation via email") { |v| options[:include_activation] = true }
394
+ opt.on("--stateful",
395
+ "Use acts_as_state_machine. Assumes --include-activation") { |v| options[:include_activation] = options[:stateful] = true }
396
+ opt.on("--aasm",
397
+ "Use (gem) aasm. Assumes --include-activation") { |v| options[:include_activation] = options[:stateful] = options[:aasm] = true }
398
+ opt.on("--rspec",
399
+ "Force rspec mode (checks for RAILS_ROOT/spec by default)") { |v| options[:rspec] = true }
400
+ opt.on("--no-rspec",
401
+ "Force test (not RSpec mode") { |v| options[:rspec] = false }
402
+ opt.on("--skip-routes",
403
+ "Don't generate a resource line in config/routes.rb") { |v| options[:skip_routes] = v }
404
+ opt.on("--old-passwords",
405
+ "Use the older password encryption scheme (see README)") { |v| options[:old_passwords] = v }
406
+ opt.on("--dump-generator-attrs",
407
+ "(generator debug helper)") { |v| options[:dump_generator_attribute_names] = v }
408
+ end
409
+
410
+ def dump_generator_attribute_names
411
+ generator_attribute_names = [
412
+ :table_name,
413
+ :file_name,
414
+ :class_name,
415
+ :controller_name,
416
+ :controller_class_path,
417
+ :controller_file_path,
418
+ :controller_class_nesting,
419
+ :controller_class_nesting_depth,
420
+ :controller_class_name,
421
+ :controller_singular_name,
422
+ :controller_plural_name,
423
+ :controller_routing_name, # new_session_path
424
+ :controller_routing_path, # /session/new
425
+ :controller_controller_name, # sessions
426
+ :controller_file_name,
427
+ :controller_table_name, :controller_plural_name,
428
+ :model_controller_name,
429
+ :model_controller_class_path,
430
+ :model_controller_file_path,
431
+ :model_controller_class_nesting,
432
+ :model_controller_class_nesting_depth,
433
+ :model_controller_class_name,
434
+ :model_controller_singular_name,
435
+ :model_controller_plural_name,
436
+ :model_controller_routing_name, # new_user_path
437
+ :model_controller_routing_path, # /users/new
438
+ :model_controller_controller_name, # users
439
+ :model_controller_file_name, :model_controller_singular_name,
440
+ :model_controller_table_name, :model_controller_plural_name,
441
+ ]
442
+ generator_attribute_names.each do |attr|
443
+ puts "%-40s %s" % ["#{attr}:", self.send(attr)] # instance_variable_get("@#{attr.to_s}"
444
+ end
445
+
446
+ end
447
+ end
448
+
449
+ # ./script/generate authenticated FoonParent::Foon SporkParent::Spork -p --force --rspec --dump-generator-attrs
450
+ # table_name: foon_parent_foons
451
+ # file_name: foon
452
+ # class_name: FoonParent::Foon
453
+ # controller_name: SporkParent::Sporks
454
+ # controller_class_path: spork_parent
455
+ # controller_file_path: spork_parent/sporks
456
+ # controller_class_nesting: SporkParent
457
+ # controller_class_nesting_depth: 1
458
+ # controller_class_name: SporkParent::Sporks
459
+ # controller_singular_name: spork
460
+ # controller_plural_name: sporks
461
+ # controller_routing_name: spork
462
+ # controller_routing_path: spork_parent/spork
463
+ # controller_controller_name: sporks
464
+ # controller_file_name: sporks
465
+ # controller_table_name: sporks
466
+ # controller_plural_name: sporks
467
+ # model_controller_name: FoonParent::Foons
468
+ # model_controller_class_path: foon_parent
469
+ # model_controller_file_path: foon_parent/foons
470
+ # model_controller_class_nesting: FoonParent
471
+ # model_controller_class_nesting_depth: 1
472
+ # model_controller_class_name: FoonParent::Foons
473
+ # model_controller_singular_name: foons
474
+ # model_controller_plural_name: foons
475
+ # model_controller_routing_name: foon_parent_foons
476
+ # model_controller_routing_path: foon_parent/foons
477
+ # model_controller_controller_name: foons
478
+ # model_controller_file_name: foons
479
+ # model_controller_singular_name: foons
480
+ # model_controller_table_name: foons
481
+ # model_controller_plural_name: foons