sorcery 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sorcery might be problematic. Click here for more details.

Files changed (66) hide show
  1. data/README.rdoc +89 -59
  2. data/VERSION +1 -1
  3. data/lib/generators/sorcery_migration/sorcery_migration_generator.rb +24 -0
  4. data/lib/generators/sorcery_migration/templates/activity_logging.rb +17 -0
  5. data/lib/generators/sorcery_migration/templates/brute_force_protection.rb +11 -0
  6. data/lib/generators/sorcery_migration/templates/core.rb +16 -0
  7. data/lib/generators/sorcery_migration/templates/oauth.rb +14 -0
  8. data/lib/generators/sorcery_migration/templates/remember_me.rb +15 -0
  9. data/lib/generators/sorcery_migration/templates/reset_password.rb +13 -0
  10. data/lib/generators/sorcery_migration/templates/user_activation.rb +17 -0
  11. data/lib/sorcery.rb +8 -0
  12. data/lib/sorcery/controller/adapters/sinatra.rb +97 -0
  13. data/lib/sorcery/controller/submodules/http_basic_auth.rb +10 -6
  14. data/lib/sorcery/controller/submodules/oauth.rb +6 -3
  15. data/lib/sorcery/controller/submodules/oauth/oauth1.rb +11 -4
  16. data/lib/sorcery/controller/submodules/oauth/oauth2.rb +1 -1
  17. data/lib/sorcery/model/submodules/activity_logging.rb +1 -1
  18. data/lib/sorcery/model/submodules/brute_force_protection.rb +0 -4
  19. data/lib/sorcery/sinatra.rb +14 -0
  20. data/lib/sorcery/test_helpers.rb +8 -52
  21. data/lib/sorcery/test_helpers/rails.rb +57 -0
  22. data/lib/sorcery/test_helpers/sinatra.rb +131 -0
  23. data/sorcery.gemspec +77 -3
  24. data/spec/Gemfile +1 -1
  25. data/spec/Gemfile.lock +2 -2
  26. data/spec/rails3/app_root/Gemfile +2 -4
  27. data/spec/rails3/app_root/Gemfile.lock +2 -5
  28. data/spec/rails3/app_root/spec/controller_oauth2_spec.rb +2 -0
  29. data/spec/rails3/app_root/spec/controller_oauth_spec.rb +6 -1
  30. data/spec/rails3/app_root/spec/controller_session_timeout_spec.rb +2 -2
  31. data/spec/rails3/app_root/spec/spec_helper.rb +1 -0
  32. data/spec/sinatra/Gemfile +12 -0
  33. data/spec/sinatra/Gemfile.lock +134 -0
  34. data/spec/sinatra/Rakefile +10 -0
  35. data/spec/sinatra/authentication.rb +3 -0
  36. data/spec/sinatra/db/migrate/activation/20101224223622_add_activation_to_users.rb +17 -0
  37. data/spec/sinatra/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
  38. data/spec/sinatra/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
  39. data/spec/sinatra/db/migrate/core/20101224223620_create_users.rb +16 -0
  40. data/spec/sinatra/db/migrate/oauth/20101224223628_create_authentications.rb +14 -0
  41. data/spec/sinatra/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
  42. data/spec/sinatra/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
  43. data/spec/sinatra/filters.rb +21 -0
  44. data/spec/sinatra/myapp.rb +133 -0
  45. data/spec/sinatra/sorcery_mailer.rb +25 -0
  46. data/spec/sinatra/spec/controller_activity_logging_spec.rb +85 -0
  47. data/spec/sinatra/spec/controller_brute_force_protection_spec.rb +69 -0
  48. data/spec/sinatra/spec/controller_http_basic_auth_spec.rb +53 -0
  49. data/spec/sinatra/spec/controller_oauth2_spec.rb +119 -0
  50. data/spec/sinatra/spec/controller_oauth_spec.rb +121 -0
  51. data/spec/sinatra/spec/controller_remember_me_spec.rb +64 -0
  52. data/spec/sinatra/spec/controller_session_timeout_spec.rb +52 -0
  53. data/spec/sinatra/spec/controller_spec.rb +120 -0
  54. data/spec/sinatra/spec/spec.opts +4 -0
  55. data/spec/sinatra/spec/spec_helper.rb +44 -0
  56. data/spec/sinatra/spec/user_activation_spec.rb +188 -0
  57. data/spec/sinatra/spec/user_activity_logging_spec.rb +36 -0
  58. data/spec/sinatra/spec/user_brute_force_protection_spec.rb +76 -0
  59. data/spec/sinatra/spec/user_oauth_spec.rb +39 -0
  60. data/spec/sinatra/spec/user_remember_me_spec.rb +66 -0
  61. data/spec/sinatra/spec/user_reset_password_spec.rb +178 -0
  62. data/spec/sinatra/spec/user_spec.rb +317 -0
  63. data/spec/sinatra/user.rb +6 -0
  64. data/spec/sinatra/views/test_login.erb +4 -0
  65. data/spec/untitled folder +18 -0
  66. metadata +76 -2
@@ -1,34 +1,103 @@
1
1
  = sorcery
2
- Magical Authentication for Rails 3.
2
+ Magical Authentication for Rails 3 and Sinatra.
3
3
 
4
4
  Inspired by restful_authentication, Authlogic and Devise.
5
5
  Crypto code taken almost unchanged from Authlogic.
6
- OAuth code inspired by OmniAuth.
6
+ OAuth code inspired by OmniAuth and Ryan Bates's railscasts about it.
7
+
7
8
 
8
9
  == Summary
9
10
 
10
- Sorcery aims to make your life easier by giving you an easy API to write your own user authentication flow with.
11
- It does this with a few goals in mind:
12
11
 
13
- * Less is more - less than 20 simple methods to remember for the entire feature-set make the lib easy to 'get'.
14
- * No built-in or generated code - use the API inside *your own* MVC structures, and don't fight to fix someone else's.
12
+ Sorcery is a stripped-down, bare-bones authentication library, with which you can write your own authentication flow.
13
+ It was built with a few goals in mind:
14
+
15
+ * Less is more - less than 20 public methods to remember for the entire feature-set make the lib easy to 'get'.
16
+ * No built-in or generated code - use the library's methods inside *your own* MVC structures, and don't fight to fix someone else's.
15
17
  * Magic yes, Voodoo no - the lib should be easy to hack for most developers.
16
18
  * Configuration over Confusion - Simple & short configuration as possible, not drowning in syntactic sugar.
17
19
  * Keep MVC cleanly separated - DB is for models, sessions are for controllers. Models stay unaware of sessions.
18
20
 
19
21
  Hopefully, I've achieved this. If not, let me know.
20
22
 
23
+
21
24
  == Useful Links:
22
25
 
23
- Example app using sorcery: https://github.com/NoamB/sorcery-example-app
24
26
 
25
- Documentation: http://rubydoc.info/gems/sorcery/0.2.0/frames
27
+ Example Rails 3 app using sorcery: https://github.com/NoamB/sorcery-example-app
28
+
29
+ Example Sinatra app using sorcery: https://github.com/NoamB/sorcery-example-app-sinatra
30
+
31
+ Documentation: http://rubydoc.info/gems/sorcery/0.3.0/frames
26
32
 
27
33
  Check out the tutorials in the github wiki!
28
34
 
35
+
36
+ == Installation:
37
+
38
+
39
+ If using bundler, first add 'sorcery' to your Gemfile:
40
+
41
+ gem "sorcery"
42
+
43
+ And run
44
+
45
+ bundle install
46
+
47
+ Otherwise simply
48
+
49
+ gem install sorcery
50
+
51
+
52
+ == Configuration:
53
+
54
+
55
+ 1. config/application.rb
56
+
57
+ config.sorcery.submodules = [:user_activation, :remember_me] # add the submodules you want to use
58
+ # You can also configure here any controller and any controller-submodule option here. For example:
59
+ config.sorcery.session_timeout = 10.minutes
60
+
61
+ 2. app/models/user.rb (or another model of your choice, but a User class is assumed by default)
62
+
63
+ activate_sorcery! do |config|
64
+ config.user_activation_mailer = MyMailer
65
+ config.username_attribute_name = :email
66
+ end
67
+
68
+ 3. app/controllers/application_controller.rb (OPTIONAL: this is actually needed only in some cases)
69
+
70
+ activate_sorcery! do |config|
71
+ config.session_timeout = 10.minutes
72
+ end
73
+
74
+ The configuration options vary with the submodules you've chosen to use, so check the documentation or the wiki tutorials regarding the specific submodule.
75
+
76
+ For your convenience, Sorcery includes a migrations generator for Rails, which can be used like so:
77
+
78
+ rails g sorcery_migration [list of submodules]
79
+
80
+ For example, for only the core functionality use:
81
+
82
+ rails g sorcery_migration core
83
+
84
+ To generate migrations for both the core AND 'remember_me' submodule:
85
+
86
+ rails g sorcery_migration core remember_me
87
+
88
+ These migrations use the default fields. You can choose to use these migrations or make your own tables and fields. Sorcery tries not to impose a database structure and naming scheme on your application.
89
+
90
+
91
+ == Usage
92
+
93
+
94
+ Please see the tutorials in the github wiki.
95
+
96
+
29
97
  == Full Features List by module:
30
98
 
31
- Core (see lib/sorcery/model/model.rb and lib/sorcery/controller/controller.rb):
99
+
100
+ Core (see lib/sorcery/model.rb and lib/sorcery/controller.rb):
32
101
  * login/logout, optional return user to requested url on login, configurable redirect for non-logged-in users.
33
102
  * password encryption, algorithms: bcrypt(default), md5, sha1, sha256, sha512, aes256, custom(yours!), none. Configurable stretches and salt.
34
103
  * configurable attribute names for username, password and email.
@@ -72,62 +141,20 @@ Oauth (see lib/sorcery/controller/submodules/oauth.rb):
72
141
  * OAuth1 and OAuth2 support (currently twitter & facebook)
73
142
  * configurable db field names and authentications table.
74
143
 
144
+
75
145
  == Next Planned Features:
76
146
 
147
+
77
148
  I've got many plans which include (by priority):
78
- * Sinatra support
149
+ * Scoping logins (to a subdomain or another arbitrary field)
150
+ * Simple auth (no user)
151
+ * Switching authentication mode at runtime (Maintenance mode)
79
152
  * Mongoid support
80
153
  * Configurable Auto login on registration/activation
81
154
  * Other reset password strategies (security questions?)
82
155
  * Other brute force protection strategies (captcha)
83
156
  * Have an idea? Let me know, and it might get into the gem!
84
157
 
85
- == Installation:
86
-
87
- You can either git clone and then 'rake install' to live on the edge (unstable),
88
-
89
- Or simply (stable):
90
-
91
- gem install sorcery
92
-
93
- == Configuration:
94
-
95
- First add 'sorcery' to your Gemfile:
96
-
97
- gem "sorcery"
98
-
99
- And run
100
-
101
- bundle install
102
-
103
- There are 2 required places to configure the plugin, and an optional one:
104
- 1. config/application.rb
105
-
106
- config.sorcery.submodules = [:user_activation, :remember_me] # add the modules you want to use
107
-
108
- You can also configure here any controller and any controller-submodule option here.
109
- For example:
110
-
111
- config.sorcery.session_timeout = 10.minutes
112
-
113
-
114
- 2. app/models/user.rb (or another model of your choice)
115
-
116
- activate_sorcery! do |config|
117
- config.user_activation_mailer = MyMailer
118
- config.username_attribute_name = :email
119
- end
120
-
121
- 3. app/controllers/application_controller.rb (OPTIONAL: this is actually needed only in some cases)
122
-
123
- activate_sorcery! do |config|
124
- config.session_timeout = 10.minutes
125
- end
126
-
127
- Also check the migrations in the example app to see what database fields are expected.
128
-
129
- The configuration options vary with the modules you've chosen to use.
130
-
131
158
 
132
159
  == Contributing to sorcery
133
160
 
@@ -137,19 +164,22 @@ For this:
137
164
 
138
165
  * Fork the project.
139
166
  * Make your feature addition or bug fix.
140
- * Add tests for it. This is important so I don’t break it in a future version unintentionally.
167
+ * Add tests for it. I've used RSpec so far, please remain consistent with it.
141
168
  * Commit, do not mess with Rakefiles, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
142
169
  * Send me a pull request. Bonus points for topic branches.
143
170
 
144
171
  If you feel my work has made your life easier, and you would like to thank me through a donation, my paypal email is in the contact details.
145
172
 
173
+
146
174
  == Contact
147
175
 
176
+ Feel free to ask questions using these contact details:
177
+
148
178
  email: nbenari@gmail.com ( also for paypal )
149
179
  twitter: @nbenari
150
180
 
181
+
151
182
  == Copyright
152
183
 
153
- Copyright (c) 2010 Noam Ben Ari (nbenari@gmail.com). See LICENSE.txt for further details.
154
- Released with permission from Kontera (http://www.kontera.com), where I work.
155
184
 
185
+ Copyright (c) 2010 Noam Ben Ari (nbenari@gmail.com). See LICENSE.txt for further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class SorceryMigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.join(File.dirname(__FILE__), 'templates')
8
+ argument :submodules, :type => :array, :required => true
9
+
10
+ def self.next_migration_number(dirname)
11
+ if ActiveRecord::Base.timestamped_migrations
12
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
13
+ else
14
+ "%.3d" % (current_migration_number(dirname) + 1)
15
+ end
16
+ end
17
+
18
+ def create_migration_file
19
+ self.submodules.each do |submodule|
20
+ migration_template "#{submodule}.rb", "db/migrate/sorcery_#{submodule}.rb"
21
+ sleep 1 # for the timestamp to change
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ class SorceryActivityLogging < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :last_login_at, :datetime, :default => nil
4
+ add_column :users, :last_logout_at, :datetime, :default => nil
5
+ add_column :users, :last_activity_at, :datetime, :default => nil
6
+
7
+ add_index :users, [:last_logout_at, :last_activity_at]
8
+ end
9
+
10
+ def self.down
11
+ remove_index :users, [:last_logout_at, :last_activity_at]
12
+
13
+ remove_column :users, :last_activity_at
14
+ remove_column :users, :last_logout_at
15
+ remove_column :users, :last_login_at
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ class SorceryBruteForceProtection < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :failed_logins_count, :integer, :default => 0
4
+ add_column :users, :lock_expires_at, :datetime, :default => nil
5
+ end
6
+
7
+ def self.down
8
+ remove_column :users, :lock_expires_at
9
+ remove_column :users, :failed_logins_count
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ class SorceryCore < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.string :username, :null => false
5
+ t.string :email, :default => nil
6
+ t.string :crypted_password, :default => nil
7
+ t.string :salt, :default => nil
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :users
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ class SorceryOauth < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :authentications do |t|
4
+ t.integer :user_id, :null => false
5
+ t.string :provider, :uid, :null => false
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :authentications
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ class SorceryRememberMe < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :remember_me_token, :string, :default => nil
4
+ add_column :users, :remember_me_token_expires_at, :datetime, :default => nil
5
+
6
+ add_index :users, :remember_me_token
7
+ end
8
+
9
+ def self.down
10
+ remove_index :users, :remember_me_token
11
+
12
+ remove_column :users, :remember_me_token_expires_at
13
+ remove_column :users, :remember_me_token
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ class SorceryResetPassword < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :reset_password_token, :string, :default => nil
4
+ add_column :users, :reset_password_token_expires_at, :datetime, :default => nil
5
+ add_column :users, :reset_password_email_sent_at, :datetime, :default => nil
6
+ end
7
+
8
+ def self.down
9
+ remove_column :users, :reset_password_email_sent_at
10
+ remove_column :users, :reset_password_token_expires_at
11
+ remove_column :users, :reset_password_token
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ class SorceryUserActivation < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :activation_state, :string, :default => nil
4
+ add_column :users, :activation_token, :string, :default => nil
5
+ add_column :users, :activation_token_expires_at, :datetime, :default => nil
6
+
7
+ add_index :users, :activation_token
8
+ end
9
+
10
+ def self.down
11
+ remove_index :users, :activation_token
12
+
13
+ remove_column :users, :activation_token_expires_at
14
+ remove_column :users, :activation_token
15
+ remove_column :users, :activation_state
16
+ end
17
+ end
@@ -29,6 +29,9 @@ module Sorcery
29
29
  end
30
30
  end
31
31
  end
32
+ module Adapters
33
+ autoload :Sinatra, 'sorcery/controller/adapters/sinatra'
34
+ end
32
35
  end
33
36
  module CryptoProviders
34
37
  autoload :AES256, 'sorcery/crypto_providers/aes256'
@@ -39,6 +42,11 @@ module Sorcery
39
42
  autoload :SHA512, 'sorcery/crypto_providers/sha512'
40
43
  end
41
44
  autoload :TestHelpers, 'sorcery/test_helpers'
45
+ module TestHelpers
46
+ autoload :Rails, 'sorcery/test_helpers/rails'
47
+ autoload :Sinatra, 'sorcery/test_helpers/sinatra'
48
+ end
42
49
 
43
50
  require 'sorcery/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
51
+ require 'sorcery/sinatra' if defined?(Sinatra)
44
52
  end
@@ -0,0 +1,97 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Adapters
4
+ # This module does the magic of translating Rails commands to Sinatra.
5
+ # This way the Rails code doesn't change, but it actually now calls Sinatra calls.
6
+ module Sinatra
7
+ def self.included(base)
8
+ base.class_eval do
9
+ class << self
10
+ # prepend a filter
11
+ def prepend_filter(type, path = nil, options = {}, &block)
12
+ return filters[type].unshift block unless path
13
+ path, options = //, path if path.respond_to?(:each_pair)
14
+ block, *arguments = compile!(type, path, block, options)
15
+ prepend_filter(type) do
16
+ process_route(*arguments) { instance_eval(&block) }
17
+ end
18
+ end
19
+
20
+ def after_filter(filter)
21
+ after do
22
+ send(filter)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ ::Sinatra::Request.class_eval do
29
+ def authorization
30
+ env['HTTP_AUTHORIZATION'] ||
31
+ env['X-HTTP_AUTHORIZATION'] ||
32
+ env['X_HTTP_AUTHORIZATION'] ||
33
+ env['REDIRECT_X_HTTP_AUTHORIZATION'] || nil
34
+ end
35
+ end
36
+
37
+ base.send(:include, InstanceMethods)
38
+ base.extend(ClassMethods)
39
+ end
40
+
41
+ module InstanceMethods
42
+ def reset_session
43
+ session.clear
44
+ end
45
+
46
+ def redirect_to(*args)
47
+ args.pop if args.last.is_a?(Hash)
48
+ redirect(*args)
49
+ end
50
+
51
+ def root_path
52
+ '/'
53
+ end
54
+
55
+ helpers do
56
+ def request_http_basic_authentication(realm)
57
+ response.header['WWW-Authenticate'] = %(Basic realm="#{realm}")
58
+ response.status = 401
59
+ end
60
+
61
+ def authenticate_with_http_basic(&blk)
62
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
63
+ yield @auth.credentials if ( @auth.provided? && @auth.basic? && @auth.credentials )
64
+ end
65
+ end
66
+
67
+ def cookies
68
+ @cookie_proxy ||= CookieProxy.new(request,response)
69
+ end
70
+ end
71
+
72
+ class CookieProxy
73
+ def initialize(request,response)
74
+ @request = request
75
+ @response = response
76
+ end
77
+
78
+ def [](key)
79
+ @request.cookies[key.to_s]
80
+ end
81
+
82
+ def []=(key, value)
83
+ @response.set_cookie(key, value)
84
+ end
85
+ end
86
+
87
+ module ClassMethods
88
+ def prepend_before_filter(filter)
89
+ prepend_filter(:before) do
90
+ send(filter)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end