janus 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/README.rdoc +77 -154
  2. data/lib/generators/janus/install_generator.rb +19 -0
  3. data/lib/generators/janus/resource_generator.rb +64 -0
  4. data/lib/generators/templates/confirmations/new.html.erb +16 -0
  5. data/lib/generators/templates/confirmations_controller.erb +3 -0
  6. data/lib/generators/templates/janus.en.yml +62 -0
  7. data/lib/generators/templates/janus.rb +25 -0
  8. data/lib/generators/templates/model.erb +8 -0
  9. data/lib/generators/templates/passwords/edit.html.erb +21 -0
  10. data/lib/generators/templates/passwords/new.html.erb +16 -0
  11. data/lib/generators/templates/passwords_controller.erb +3 -0
  12. data/lib/generators/templates/registrations/edit.html.erb +31 -0
  13. data/lib/generators/templates/registrations/new.html.erb +26 -0
  14. data/lib/generators/templates/registrations_controller.erb +17 -0
  15. data/lib/generators/templates/sessions/new.html.erb +30 -0
  16. data/lib/generators/templates/sessions_controller.erb +11 -0
  17. data/lib/janus.rb +1 -0
  18. data/lib/janus/config.rb +10 -4
  19. data/lib/janus/controllers/confirmations_controller.rb +6 -6
  20. data/lib/janus/controllers/helpers.rb +4 -4
  21. data/lib/janus/controllers/passwords_controller.rb +3 -3
  22. data/lib/janus/controllers/registrations_controller.rb +12 -9
  23. data/lib/janus/controllers/sessions_controller.rb +15 -7
  24. data/lib/janus/helper.rb +1 -1
  25. data/lib/janus/hooks.rb +6 -6
  26. data/lib/janus/hooks/rememberable.rb +2 -2
  27. data/lib/janus/hooks/remote_authenticatable.rb +1 -1
  28. data/lib/janus/manager.rb +5 -5
  29. data/lib/janus/models/base.rb +2 -2
  30. data/lib/janus/models/confirmable.rb +7 -4
  31. data/lib/janus/models/database_authenticatable.rb +26 -16
  32. data/lib/janus/models/rememberable.rb +12 -9
  33. data/lib/janus/models/remote_authenticatable.rb +21 -18
  34. data/lib/janus/models/trackable.rb +11 -8
  35. data/lib/janus/routes.rb +22 -22
  36. data/lib/janus/strategies.rb +3 -3
  37. data/lib/janus/strategies/database_authenticatable.rb +1 -1
  38. data/lib/janus/strategies/rememberable.rb +1 -1
  39. data/lib/janus/strategies/remote_authenticatable.rb +1 -1
  40. data/lib/janus/test_helper.rb +6 -2
  41. metadata +19 -36
@@ -1,180 +1,103 @@
1
1
  = Janus
2
2
 
3
- Janus is an authentication engine for Ruby on Rails 3 and is an alternative
4
- to the Warden + Devise combo, without the Rack middleware. The whole project
5
- is inspired by the Warden and Devise API (in order to somehow compatible)
6
- but is quite different since everything happens within ActionDispatch and not
7
- at the Rack level.
8
-
9
- This Rails instead of Rack difference, allows to actually have the main logic
10
- within plain Rails controllers. For instance the database authentication is
11
- called from SessionsController, and it's not just another strategy operating
12
- at the Rack level within Warden (which requires to check that it's being
13
- called from the correct URL). There ain't no factory to add strategy modules
14
- to your models too. You must manually include the necessary ones.
15
-
16
- Another difference is that you must actually create the necessary controllers,
17
- models, mailers and views within your project (extending the default ones).
18
- You will eventually need to have those controllers anyway, and having those
19
- from the beginning allows to skip some configuration. The burdensome of
20
- manually creating all those classes should eventually be leveraged by using
21
- some rails generators.
22
-
23
- Janus also provides a finer control over setting and unsetting a user than
24
- Warden provides. Janus uses +login+ and +logout+ to actually sign the user
25
- in and out, just like Warden, but actually uses +set_user+ and +unset_user+
26
- to manually set the session, without dispatching the +after_login+ and
27
- +after_logout+ hooks, of course.
28
-
29
- Emails are also sent from the controllers, not from the models, because I
30
- believe this is actually the job of controllers, not models.
3
+ Janus is an authentication engine for Ruby on Rails 3+ to painlessly handle
4
+ users in your apps. It comes with everything needed, from the migrations to the
5
+ controllers, plus some different strategies to keep user signed in.
6
+
7
+ Janus also tries to be somewhat compatible with Devise's API and conventions,
8
+ because there was no reason to change it completely. Thought there are some
9
+ differences, like controllers and views being required in your apps, and emails
10
+ being sent from the controllers and never from the models.
31
11
 
32
12
  == Features
33
13
 
34
- The main feature is of course having a framework for authenticating users
35
- painleslly. Yet a very usefull feature is the cross domain authentication
36
- --which allows a user to single sign in and out across top level domains.
14
+ - full auth system with strategies and hooks;
15
+ - scoped auth for parallel authentications (like +users+, +admin_users+, etc.);
16
+ - abstract controllers ready to use;
17
+ - generators to have everything generated automatically;
18
+ - use only what you need at anytime.
19
+
20
+ As for the strategies and hooks:
21
+
22
+ - {DatabaseAuthenticatable}[http://rdoc.info/github/ysbaddaden/janus/Janus/Models/DatabaseAuthenticatable]
23
+ to auth users with passwords (plus registration and password reset);
24
+ - {RemoteAuthenticatable}[http://rdoc.info/github/ysbaddaden/janus/Janus/Models/RemoteAuthenticatable]
25
+ to keep users signed in across top level domains;
26
+ - {Confirmable}[http://rdoc.info/github/ysbaddaden/janus/Janus/Models/RemoteAuthenticatable]
27
+ to have users confirm their emails upon registration;
28
+ - {Rememberable}[http://rdoc.info/github/ysbaddaden/janus/Janus/Models/Rememberable]
29
+ to keep users authentified;
30
+ - {Trackable}[http://rdoc.info/github/ysbaddaden/janus/Janus/Models/Trackable]
31
+
32
+ == Getting Started
33
+
34
+ First add the janus gem to your Gemfile, then run `bundle` to install it:
35
+
36
+ gem 'janus'
37
+ gem 'bcrypt-ruby'
38
+ # gem 'scrypt'
39
+
40
+ You'll also need either the `bcrypt-ruby` or scrypt` gems, depending on which
41
+ library you want to use to encrypt the passwords. Janus uses bcrypt by default,
42
+ to be compatible with Devise, but you may prefer scrypt, which is stronger.
43
+
44
+ Run the <tt>janus:install</tt> generator to setup janus in your app:
45
+
46
+ $ rails generate janus:install
37
47
 
38
- How is the cross domain authentication strategy usefull? Let's imagine you
39
- host of blogging website where users may host their blogs on other domains.
40
- Since you don't rely on subdomains, it will be a pain to keep the user
41
- authentified, because you can't rely on '.' domain cookie trick.
48
+ Then create your first authenticatable resource, let's say +User+:
42
49
 
43
- This is where RemoteAuthentication comes in, and allows you to painlessly
44
- keep your users connected across the main website and their blogs. This
45
- without actually tracking connections since this strategy takes advantage
46
- of the +set_user+ and +unset_user+ methods, because they're not really
47
- signing in, they just stay authentified across domains.
50
+ $ rails generate janus:resource user
48
51
 
49
- So, Janus provides the following API:
52
+ You may notice that Janus also generates all the controllers and views. This is
53
+ because you will eventually need those to customize some behavior and having
54
+ them around from the beginning is great.
50
55
 
51
- - full authentication system with strategies and hooks
52
- - scoped authentications with parallel authentication (like `users`, `admin_users`, etc.)
53
- - database authentication with password encryption (bcrypt) and validation
54
- - remote authentication for cross domain single sign in / sign out
55
- - abstract controllers for session management, registration, email confirmation and password reset
56
- - route generation for the above controllers
56
+ You may run the routes rake task, to see what routes were added by Janus.
57
57
 
58
- And for the strategies and hooks:
58
+ === Helpers & Filters
59
+
60
+ - authenticate_user!
61
+ - user_signed_in?
62
+ - current_user
63
+
64
+ === Strategies
65
+
66
+ You may customize the strategies for the <tt>janus:resource</tt> generator, like an
67
+ AdminUser that may only be created and managed from the console:
68
+
69
+ $ rails generate janus:resource AdminUser session password remember
70
+
71
+ Here is the list of all the current strategies:
72
+
73
+ - +session+ — get users signed in and out (email/password combinaison)
74
+ - +remember+ — keep users signed in across sessions
75
+ - +registration+ — get users registered
76
+ - +confirmation+ — emails may be confirmed after registration
77
+ - +password+ — reset password (using an email exchanged token)
78
+ - +track+ — track current and previous user's sign in date and IP
79
+ - +remote+ — keeps users signed in different top level domains
59
80
 
60
- - DatabaseAuthenticatable
61
- - RemoteAuthenticatable
62
- - Confirmable
63
- - Rememberable
64
- - Trackable (note that login through Janus::Manager#set_user won't track the user).
65
81
 
66
82
  == TODO
67
83
 
68
- - Simple configuration to use scrypt instead of bcrypt for password encryption.
84
+ - Differenciate mailers per resource, by looking for User::Mailer or AdminUser::Mailer classes.
69
85
  - Reconfirmable when email changes.
70
- - TokenAuthenticatable.
71
- - Remember me on remote authenticated domains.
72
- - Differenciate mailers per resource, by looking for Users::Mailer class.
73
- - Generators: `janus:install` and `janus <scope>`.
74
- - Integrate OmniAuth, or shall we let the user do it himself?
75
- - Providing an OAuth 2.0 server whould be cool.
76
-
77
- == Install
78
-
79
- There is no automated way to install Janus yet, because generators are missing.
80
- Also remember that Janus is only compatible with Rails 3+.
81
-
82
- First add the gem to your Gemfile:
83
-
84
- $ gem 'janus'
85
-
86
- Configure your user models by including all or a selection of the Janus::Models
87
- modules:
88
-
89
- class User < ActiveRecord::Base
90
- include Janus::Models::Base
91
- include Janus::Models::DatabaseAuthenticatable
92
- include Janus::Models::RemoteAuthenticatable
93
- include Janus::Models::Confirmable
94
- include Janus::Models::Rememberable
95
- include Janus::Models::Trackable
96
- end
97
-
98
- class Admin < ActiveRecord::Base
99
- include Janus::Models::Base
100
- include Janus::Models::DatabaseAuthenticatable
101
- end
102
-
103
- Configure your routes:
104
-
105
- Name::Application.routes.map do
106
- janus :users, :session => true, :registration => true, :password => true, :confirmation => true
107
- janus :admins, :session => true
108
- root :to => "home#index"
109
- end
110
-
111
- Create the required controllers:
112
-
113
- class Users::SessionsController < Janus::SessionsController
114
- respond_to :html
115
- end
116
-
117
- class Users::RegistrationsController < Janus::RegistrationsController
118
- respond_to :html
119
- end
120
-
121
- class Users::PasswordsController < Janus::PasswordsController
122
- respond_to :html
123
- end
124
-
125
- class Users::ConfirmationsController < Janus::ConfirmationsController
126
- respond_to :html
127
- end
128
-
129
- class Admins::SessionsController < Janus::SessionsController
130
- respond_to :html
131
- end
132
-
133
- Copy the views from test/rails_app to your application:
134
-
135
- mkdir name/app/views/users/
136
- cp -r janus/test/rails_app/app/views/users/sessions name/app/views/users/
137
- cp -r janus/test/rails_app/app/views/users/registrations name/app/views/users/
138
- cp -r janus/test/rails_app/app/views/users/confirmations name/app/views/users/
139
- cp -r janus/test/rails_app/app/views/users/registrations name/app/views/users/
140
-
141
- mkdir name/app/views/admins/
142
- cp -r janus/test/rails_app/app/views/users/sessions name/app/views/users/
143
-
144
- Have a look to the test app in <tt>test/rails_app</tt> for additional help:
145
-
146
- app/controllers/application_controller.rb
147
- app/controller/users/confirmations_controller.rb
148
- app/controller/users/passwords_controller.rb
149
- app/controller/users/registrations_controller.rb
150
- app/controller/users/sessions_controller.rb
151
- app/mailers/janus_mailer.rb
152
- app/models/remote_token.rb
153
- app/models/user.rb
154
- app/views/janus_mailer/confirmation_instructions.html.erb
155
- app/views/janus_mailer/confirmation_instructions.text.erb
156
- app/views/janus_mailer/reset_password_instructions.html.erb
157
- app/views/janus_mailer/reset_password_instructions.text.erb
158
- app/views/users/confirmations/new.html.erb
159
- app/views/users/passwords/new.html.erb
160
- app/views/users/passwords/edit.html.erb
161
- app/views/users/registrations/new.html.erb
162
- app/views/users/registrations/edit.html.erb
163
- app/views/users/sessions/new.html.erb
164
- config/initializers/janus.rb
165
- config/locales/janus.en.yml
166
- config/routes.rb
167
- db/migrate/*.rb
86
+ - Simple configuration to use scrypt instead of bcrypt for password encryption.
87
+ - TokenAuthenticatable strategy.
88
+ - Rememberable across top level domains.
89
+ - Omniauthable (or shall we let the user do it himself?)
90
+ - Providing an OAuth 1.0 service whould be cool.
168
91
 
169
92
  == License
170
93
 
171
94
  Janus is distributed under the MIT-License.
172
95
 
173
- == Authors
96
+ == Credits
174
97
 
175
98
  Most of the API and some code like password encryption is copied from
176
99
  Devise: http://github.com/plataformatec/devise.git and Warden:
177
100
  http://github.com/hassox/warden
178
101
 
179
- - Julien Portalier <ysbaddaden@gmail.com>
102
+ - Julien Portalier <julien@portalier.com>
180
103
 
@@ -0,0 +1,19 @@
1
+ require 'securerandom'
2
+
3
+ module Janus
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../../templates', __FILE__)
7
+
8
+ desc "Configures Janus into your app"
9
+
10
+ def copy_initializer
11
+ template 'janus.rb', 'config/initializers/janus.rb'
12
+ end
13
+
14
+ def copy_locale
15
+ template 'janus.en.yml', 'config/locales/janus.en.yml'
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,64 @@
1
+ module Janus
2
+ module Generators
3
+ class ResourceGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../../templates', __FILE__)
5
+
6
+ argument :strategies, :type => :array, :banner => "strategy strategy",
7
+ :default => %w{session registration confirmation password remember}
8
+
9
+ desc "Generates an authenticatable resource (with migration," <<
10
+ "routes, strategies and views)"
11
+
12
+ def create_resource
13
+ attributes = [singular_name]
14
+ attributes += %w{email:string encrypted_password:string}
15
+ attributes += %w{remember_token:string:uniq remember_created_at:datetime} if strategies.include?('remember')
16
+ attributes += %w{confirmation_token:string:uniq confirmation_sent_at:datetime confirmed_at:datetime} if strategies.include?('confirmation')
17
+ attributes += %w{reset_password_token:string:uniq reset_password_sent_at:datetime} if strategies.include?('password')
18
+ attributes += %w{session_token:string:uniq} if strategies.include?('remote')
19
+ attributes += %w{sign_in_count:integer last_sign_in_at:datetime last_sign_in_ip:string current_sign_in_at:datetime current_sign_in_ip:string} if strategies.include?('track')
20
+ generate('model', attributes.join(' '))
21
+
22
+ modules = [
23
+ " include Janus::Models::Base",
24
+ " include Janus::Models::DatabaseAuthenticatable",
25
+ ]
26
+ modules << " include Janus::Models::Rememberable" if strategies.include?('remember')
27
+ modules << " include Janus::Models::Confirmable" if strategies.include?('confirmation')
28
+ modules << " include Janus::Models::Trackable" if strategies.include?('track')
29
+ modules << " include Janus::Models::RemoteAuthenticatable" if strategies.include?('remote')
30
+ inject_into_class "app/models/#{singular_name}.rb", class_name, modules.join("\n") + "\n"
31
+ end
32
+
33
+ def create_controllers_and_views
34
+ if strategies.include?('session')
35
+ template 'sessions_controller.erb', "app/controllers/#{plural_name}/sessions_controller.rb"
36
+ template 'sessions/new.html.erb', "app/views/#{plural_name}/sessions/new.html.erb"
37
+ end
38
+ if strategies.include?('registration')
39
+ template 'registrations_controller.erb', "app/controllers/#{plural_name}/registrations_controller.rb"
40
+ template 'registrations/new.html.erb', "app/views/#{plural_name}/registrations/new.html.erb"
41
+ template 'registrations/edit.html.erb', "app/views/#{plural_name}/registrations/edit.html.erb"
42
+ end
43
+ if strategies.include?('confirmation')
44
+ template 'confirmations_controller.erb', "app/controllers/#{plural_name}/confirmations_controller.rb"
45
+ template 'confirmations/new.html.erb', "app/views/#{plural_name}/confirmations/new.html.erb"
46
+ end
47
+ if strategies.include?('password')
48
+ template 'passwords_controller.erb', "app/controllers/#{plural_name}/passwords_controller.rb"
49
+ template 'passwords/new.html.erb', "app/views/#{plural_name}/passwords/new.html.erb"
50
+ template 'passwords/edit.html.erb', "app/views/#{plural_name}/passwords/edit.html.erb"
51
+ end
52
+ end
53
+
54
+ def add_janus_route
55
+ route "janus :#{plural_name}, " + controllers.map { |ctrl| ":#{ctrl} => true" }.join(', ')
56
+ end
57
+
58
+ private
59
+ def controllers
60
+ strategies & %w{session registration confirmation password}
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,16 @@
1
+ <h1><%%= t 'janus.confirmations.new.resend_confirmation_instructions' %></h1>
2
+
3
+ <%%= form_for @<%= singular_name %>, :url => <%= singular_name %>_confirmation_path, :method => :post do |f| %>
4
+ <%%= janus_error_messages %>
5
+
6
+ <%% <%= class_name %>.authentication_keys.each do |key| %>
7
+ <div class="field">
8
+ <%%= f.label key %>
9
+ <%%= f.text_field key %>
10
+ </div>
11
+ <%% end %>
12
+
13
+ <div class="actions">
14
+ <%%= f.submit t('janus.confirmations.new.send_instructions_btn') %>
15
+ </div>
16
+ <%% end %>
@@ -0,0 +1,3 @@
1
+ class <%= class_name.pluralize %>::ConfirmationsController < Janus::ConfirmationsController
2
+ respond_to :html
3
+ end
@@ -0,0 +1,62 @@
1
+ en:
2
+ # activerecord:
3
+ # attributes:
4
+ # user:
5
+ # email: "Email"
6
+ # password: "Password"
7
+ # password_confirmation: "Confirm password"
8
+ # current_password: "Current password"
9
+ # errors:
10
+ # messages:
11
+ # not_found: "not found"
12
+
13
+ flash:
14
+ janus:
15
+ passwords:
16
+ create:
17
+ email_sent: "Instructions to reset your password were sent to your email account."
18
+ user_not_found: "Error: no such user."
19
+ update:
20
+ password_updated: "Your password was successfully resetted."
21
+ invalid_token: "Error: invalid token."
22
+
23
+ janus:
24
+ mailer:
25
+ hello: "Hello,"
26
+ reset_password_instructions:
27
+ subject: "Instructions to change your password"
28
+ infos: "Somebody requested to change your password. To do so just click the following link:"
29
+ change_password_link: "Change my password"
30
+ please_ignore_your_password_wont_change: "If you didn't make this request, please delete this email immediately. Your password won't change until you click the link and change your password."
31
+
32
+ confirmation_instructions:
33
+ subject: "Confirm your account"
34
+ confirm: "You may confirm your registration by clicking the following link:"
35
+ confirm_my_account: "Confirm my account"
36
+
37
+ sessions:
38
+ new:
39
+ sign_in: "Sign in"
40
+ sign_in_btn: "Sign in"
41
+
42
+ registrations:
43
+ new:
44
+ sign_up: "Sign up"
45
+ sign_up_btn: "Sign up"
46
+ edit:
47
+ my_account: "My account"
48
+ save_changes_btn: "Save changes"
49
+
50
+ confirmations:
51
+ new:
52
+ resend_confirmation_instructions: "Resend confirmation instructions"
53
+ send_instructions_btn: "Send instructions"
54
+
55
+ passwords:
56
+ new:
57
+ forgot_password: "Forgot your password?"
58
+ send_instructions_btn: "Send instructions"
59
+ edit:
60
+ change_password: "Change your password"
61
+ change_password_btn: "Change my password"
62
+
@@ -0,0 +1,25 @@
1
+ Janus.config do |config|
2
+ config.contact_email = "contact@some-example-domain.com"
3
+
4
+ # DatabaseAuthenticatable
5
+ config.authentication_keys = [ :email ]
6
+
7
+ # you may use bcrypt:
8
+ config.encryptor = :bcrypt
9
+ config.stretches = 10
10
+ config.pepper = <%= SecureRandom.hex(64).inspect %>
11
+
12
+ # or you prefer scrypt:
13
+ # config.encryptor = :scrypt
14
+ # config.scrypt_options = { :max_time => 0.25 }
15
+
16
+ # Confirmable
17
+ # config.confirmation_key = :confirm_token
18
+
19
+ # Rememberable
20
+ # config.remember_for = 1.year
21
+ # config.extend_remember_period = false
22
+
23
+ # RemoteAuthenticatable
24
+ # config.remote_authentication_key = :auth_token
25
+ end