sorcery 0.1.3 → 0.1.4

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 (36) hide show
  1. data/Gemfile +1 -2
  2. data/LICENSE.txt +1 -1
  3. data/README.rdoc +27 -11
  4. data/Rakefile +2 -13
  5. data/VERSION +1 -1
  6. data/lib/sorcery.rb +4 -1
  7. data/lib/sorcery/controller.rb +13 -7
  8. data/lib/sorcery/controller/submodules/activity_logging.rb +45 -0
  9. data/lib/sorcery/controller/submodules/brute_force_protection.rb +8 -69
  10. data/lib/sorcery/controller/submodules/http_basic_auth.rb +7 -4
  11. data/lib/sorcery/controller/submodules/session_timeout.rb +4 -1
  12. data/lib/sorcery/crypto_providers/bcrypt.rb +1 -5
  13. data/lib/sorcery/model/submodules/activity_logging.rb +35 -0
  14. data/lib/sorcery/model/submodules/brute_force_protection.rb +72 -0
  15. data/lib/sorcery/model/submodules/remember_me.rb +3 -1
  16. data/lib/sorcery/model/submodules/reset_password.rb +93 -0
  17. data/lib/sorcery/model/submodules/user_activation.rb +2 -0
  18. data/sorcery.gemspec +26 -14
  19. data/spec/rails3/app_root/app/controllers/application_controller.rb +2 -2
  20. data/spec/rails3/app_root/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
  21. data/spec/rails3/app_root/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
  22. data/spec/rails3/app_root/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
  23. data/spec/rails3/controller_activity_logging_spec.rb +84 -0
  24. data/spec/rails3/controller_brute_force_protection_spec.rb +24 -41
  25. data/spec/rails3/controller_http_basic_auth_spec.rb +10 -0
  26. data/spec/rails3/controller_session_timeout_spec.rb +1 -0
  27. data/spec/rails3/controller_spec.rb +3 -3
  28. data/spec/rails3/spec_helper.rb +39 -19
  29. data/spec/rails3/user_activity_logging_spec.rb +36 -0
  30. data/spec/rails3/user_brute_force_protection_spec.rb +76 -0
  31. data/spec/rails3/user_reset_password_spec.rb +198 -0
  32. metadata +34 -22
  33. data/features/support/env.rb +0 -13
  34. data/lib/sorcery/model/submodules/password_reset.rb +0 -64
  35. data/spec/rails3/app_root/db/migrate/password_reset/20101224223622_add_password_reset_to_users.rb +0 -9
  36. data/spec/rails3/user_password_reset_spec.rb +0 -76
data/Gemfile CHANGED
@@ -6,13 +6,12 @@ source "http://rubygems.org"
6
6
  # Add dependencies to develop your gem here.
7
7
  # Include everything needed to run rake, tests, features, etc.
8
8
  group :development do
9
- gem "rails", "3.0.3"
9
+ gem "rails", ">= 3.0.0"
10
10
  gem "rspec", "~> 2.3.0"
11
11
  gem 'rspec-rails'
12
12
  gem 'ruby-debug19'
13
13
  gem 'sqlite3-ruby', :require => 'sqlite3'
14
14
  gem "yard", "~> 0.6.0"
15
- gem "cucumber", ">= 0"
16
15
  gem "bundler", "~> 1.0.0"
17
16
  gem "jeweler", "~> 1.5.2"
18
17
  gem 'simplecov', '>= 0.3.8', :require => false # Will install simplecov-html as a dependency
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Noam Ben-Ari
1
+ Copyright (c) 2010 Noam Ben-Ari <mailto:nbenari@gmail.com>
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -19,11 +19,13 @@ User Activation (see lib/sorcery/model/submodules/user_activation.rb):
19
19
  * User activation by email with optional success email.
20
20
  * configurable attribute names.
21
21
  * configurable mailer.
22
- * Optionally prevent active users to login.
22
+ * Optionally prevent non-active users to login.
23
23
 
24
- Password Reset (see lib/sorcery/model/submodules/password_reset.rb):
24
+ Reset Password (see lib/sorcery/model/submodules/reset_password.rb):
25
25
  * Reset password with email verification.
26
26
  * configurable mailer, method name, and attribute name.
27
+ * configurable expiration.
28
+ * configurable time between emails (hammering protection).
27
29
 
28
30
  Remember Me (see lib/sorcery/model/submodules/remember_me.rb):
29
31
  * Remember me with configurable expiration.
@@ -33,15 +35,20 @@ Session Timeout (see lib/sorcery/controller/submodules/session_timeout.rb):
33
35
  * Configurable session timeout.
34
36
  * Optionally session timeout will be calculated from last user action.
35
37
 
36
- Brute Force Protection (see lib/sorcery/controller/submodules/brute_force_protection.rb):
38
+ Brute Force Protection (see lib/sorcery/model/submodules/brute_force_protection.rb):
37
39
  * Brute force login hammering protection.
38
- * configurable logins before ban, logins within time period before ban, ban time and ban action.
40
+ * configurable logins before lock and lock duration.
39
41
 
40
42
  Basic HTTP Authentication (see lib/sorcery/controller/submodules/http_basic_auth.rb):
41
43
  * A before filter for requesting authentication with HTTP Basic.
42
44
  * automatic login from HTTP Basic.
43
45
  * automatic login is disabled if session key changed.
44
46
 
47
+ Activity Logging (see lib/sorcery/model/submodules/activity_logging.rb):
48
+ * automatic logging of last login, last logout and last activity time.
49
+ * an easy method of collecting the list of currently logged in users.
50
+ * configurable timeout by which to decide whether to include a user in the list of logged in users.
51
+
45
52
  Other:
46
53
  * Modular design, load only the modules you need.
47
54
  * 100% TDD'd code, 100% test coverage.
@@ -49,13 +56,12 @@ Other:
49
56
  == Next Planned Features:
50
57
 
51
58
  I've got many plans which include:
52
- * Hammering reset password protection
53
59
  * Configurable Auto login on registration/activation
54
60
  * Other reset password strategies (security questions?)
61
+ * Other brute force protection strategies (captcha)
55
62
  * Sinatra support
56
63
  * Mongoid support
57
- * OmniAuth integration
58
- * Activity logging
64
+ * OAuth1 and OAuth2 support
59
65
  * Have an idea? Let me know, and it might get into the gem!
60
66
 
61
67
  == Project Goals:
@@ -75,9 +81,9 @@ Hopefully, I've achieved this. If not, let me know.
75
81
 
76
82
  == Installation:
77
83
 
78
- You can either git clone and then 'rake install',
84
+ You can either git clone and then 'rake install' to live on the edge (unstable),
79
85
 
80
- In the future will be available:
86
+ Or simply (stable):
81
87
 
82
88
  gem install sorcery
83
89
 
@@ -122,11 +128,21 @@ The configuration options vary with the modules you've chosen to use.
122
128
 
123
129
  == Contributing to sorcery
124
130
 
125
- I can use help of any kind, be it comments on code, suggestions, features, bug reports, bug fixes, documentation and if you like, a donation.
131
+ Your feedback is very welcome and will make this gem much much better for you, me and everyone else.
132
+ Besides feedback on code, features, suggestions and bug reports, you may want to actually make an impact on the code.
133
+ For this:
134
+
135
+ * Fork the project.
136
+ * Make your feature addition or bug fix.
137
+ * Add tests for it. This is important so I don’t break it in a future version unintentionally.
138
+ * 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)
139
+ * Send me a pull request. Bonus points for topic branches.
140
+
141
+ 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.
126
142
 
127
143
  == Contact
128
144
 
129
- email: nbenari@gmail.com
145
+ email: nbenari@gmail.com ( also for paypal )
130
146
  twitter: @nbenari
131
147
 
132
148
  == Copyright
data/Rakefile CHANGED
@@ -22,31 +22,20 @@ Jeweler::Tasks.new do |gem|
22
22
  # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
23
23
  # gem.add_runtime_dependency 'jabber4r', '> 0.1'
24
24
  # gem.add_development_dependency 'rspec', '> 1.2.3'
25
- # gem.add_runtime_dependency 'bcrypt-ruby', '~> 2.1.4'
25
+ gem.add_runtime_dependency 'bcrypt-ruby', '~> 2.1.4'
26
26
  end
27
27
  Jeweler::RubygemsDotOrgTasks.new
28
28
 
29
29
  require 'rspec/core'
30
30
  require 'rspec/core/rake_task'
31
+
31
32
  RSpec::Core::RakeTask.new(:spec) do |spec|
32
33
  spec.pattern = FileList['spec/**/*_spec.rb']
33
34
  end
34
35
 
35
- RSpec::Core::RakeTask.new(:rcov) do |spec|
36
- spec.pattern = 'spec/**/*_spec.rb'
37
- spec.rcov = true
38
- end
39
-
40
- require 'cucumber/rake/task'
41
- Cucumber::Rake::Task.new(:features)
42
-
43
-
44
-
45
36
  require 'yard'
46
37
  YARD::Rake::YardocTask.new
47
38
 
48
-
49
- #task :default => :spec
50
39
  desc 'Default: Run all specs.'
51
40
  task :default => :all_specs
52
41
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/lib/sorcery.rb CHANGED
@@ -3,8 +3,10 @@ module Sorcery
3
3
  module Model
4
4
  module Submodules
5
5
  autoload :UserActivation, 'sorcery/model/submodules/user_activation'
6
- autoload :PasswordReset, 'sorcery/model/submodules/password_reset'
6
+ autoload :ResetPassword, 'sorcery/model/submodules/reset_password'
7
7
  autoload :RememberMe, 'sorcery/model/submodules/remember_me'
8
+ autoload :ActivityLogging, 'sorcery/model/submodules/activity_logging'
9
+ autoload :BruteForceProtection, 'sorcery/model/submodules/brute_force_protection'
8
10
  end
9
11
  end
10
12
  autoload :Controller, 'sorcery/controller'
@@ -14,6 +16,7 @@ module Sorcery
14
16
  autoload :SessionTimeout, 'sorcery/controller/submodules/session_timeout'
15
17
  autoload :BruteForceProtection, 'sorcery/controller/submodules/brute_force_protection'
16
18
  autoload :HttpBasicAuth, 'sorcery/controller/submodules/http_basic_auth'
19
+ autoload :ActivityLogging, 'sorcery/controller/submodules/activity_logging'
17
20
  end
18
21
  end
19
22
  module CryptoProviders
@@ -12,7 +12,6 @@ module Sorcery
12
12
  end
13
13
  end
14
14
  Config.update!
15
- Config.user_class = User
16
15
  end
17
16
  end
18
17
 
@@ -31,7 +30,7 @@ module Sorcery
31
30
  # To be used as before_filter.
32
31
  # Will trigger auto-login attempts via the call to logged_in?
33
32
  # If all attempts to auto-login fail, the failure callback will be called.
34
- def require_user_login
33
+ def require_login
35
34
  if !logged_in?
36
35
  session[:user_wanted_url] = request.url if Config.save_user_wanted_url
37
36
  self.send(Config.not_authenticated_action)
@@ -46,13 +45,14 @@ module Sorcery
46
45
  after_login!(user, credentials)
47
46
  logged_in_user
48
47
  else
49
- after_failed_login!(user, credentials)
48
+ after_failed_login!(credentials)
50
49
  nil
51
50
  end
52
51
  end
53
52
 
54
53
  def logout
55
54
  if logged_in?
55
+ before_logout!(logged_in_user)
56
56
  reset_session
57
57
  after_logout!
58
58
  end
@@ -63,9 +63,9 @@ module Sorcery
63
63
  end
64
64
 
65
65
  # attempts to auto-login from the sources defined (session, basic_auth, cookie, etc.)
66
- # returns the logged in user if found, false if not (using old restful-authentication trick).
66
+ # returns the logged in user if found, false if not (using old restful-authentication trick, nil != false).
67
67
  def logged_in_user
68
- @logged_in_user ||= login_from_session || login_from_other_sources unless @logged_in_user == false # || login_from_basic_auth || )
68
+ @logged_in_user ||= login_from_session || login_from_other_sources unless @logged_in_user == false
69
69
  end
70
70
 
71
71
  def login_from_other_sources
@@ -94,8 +94,12 @@ module Sorcery
94
94
  Config.after_login.each {|c| self.send(c, user, credentials)}
95
95
  end
96
96
 
97
- def after_failed_login!(user, credentials)
98
- Config.after_failed_login.each {|c| self.send(c, user, credentials)}
97
+ def after_failed_login!(credentials)
98
+ Config.after_failed_login.each {|c| self.send(c, credentials)}
99
+ end
100
+
101
+ def before_logout!(user)
102
+ Config.before_logout.each {|c| self.send(c, user)}
99
103
  end
100
104
 
101
105
  def after_logout!
@@ -118,6 +122,7 @@ module Sorcery
118
122
  :login_sources,
119
123
  :after_login,
120
124
  :after_failed_login,
125
+ :before_logout,
121
126
  :after_logout,
122
127
  :after_config
123
128
 
@@ -130,6 +135,7 @@ module Sorcery
130
135
  :@login_sources => [],
131
136
  :@after_login => [],
132
137
  :@after_failed_login => [],
138
+ :@before_logout => [],
133
139
  :@after_logout => [],
134
140
  :@after_config => [],
135
141
  :@save_user_wanted_url => true
@@ -0,0 +1,45 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ module ActivityLogging
5
+ def self.included(base)
6
+ base.send(:include, InstanceMethods)
7
+ Config.after_login << :register_login_time_to_db
8
+ Config.before_logout << :register_logout_time_to_db
9
+ base.after_filter :register_last_activity_time_to_db
10
+ end
11
+
12
+ module InstanceMethods
13
+ def logged_in_users
14
+ Config.user_class.logged_in_users
15
+ # A possible patch here:
16
+ # we'll add the logged_in_user to the users list if he's not in it (can happen when he was inactive for more than activity timeout):
17
+ #
18
+ # users.unshift!(logged_in_user) if logged_in? && users.find {|u| u.id == logged_in_user.id}.nil?
19
+ #
20
+ # disadvantages: can hurt performance.
21
+ end
22
+
23
+ protected
24
+
25
+ def register_login_time_to_db(user, credentials)
26
+ user.send(:"#{user.sorcery_config.last_login_at_attribute_name}=", Time.now.utc.to_s(:db))
27
+ user.save!(:validate => false)
28
+ end
29
+
30
+ def register_logout_time_to_db(user)
31
+ user.send(:"#{user.sorcery_config.last_logout_at_attribute_name}=", Time.now.utc.to_s(:db))
32
+ user.save!(:validate => false)
33
+ end
34
+
35
+ # we do not update activity on logout
36
+ def register_last_activity_time_to_db
37
+ return if !logged_in?
38
+ logged_in_user.send(:"#{logged_in_user.sorcery_config.last_activity_at_attribute_name}=", Time.now.utc.to_s(:db))
39
+ logged_in_user.save!(:validate => false)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -4,83 +4,22 @@ module Sorcery
4
4
  module BruteForceProtection
5
5
  def self.included(base)
6
6
  base.send(:include, InstanceMethods)
7
- Config.module_eval do
8
- class << self
9
- attr_accessor :login_retries_amount_allowed, # how many failed logins allowed.
10
- :login_retries_time_period, # the time after which the failed logins counter is reset.
11
- :login_ban_time_period, # how long the user should be banned. in seconds.
12
- :banned_action # what controller action should be called when a banned user tries.
13
-
14
- def merge_brute_force_protection_defaults!
15
- @defaults.merge!(:@login_retries_amount_allowed => 50,
16
- :@login_retries_time_period => 30,
17
- :@login_ban_time_period => 3600,
18
- :@banned_action => :default_banned_action)
19
- end
20
- end
21
- merge_brute_force_protection_defaults!
22
- end
23
- Config.after_failed_login << :check_failed_logins_limit_reached
24
- base.prepend_before_filter :deny_banned_user
7
+
8
+ Config.after_login << :reset_failed_logins_count!
9
+ Config.after_failed_login << :update_failed_logins_count!
25
10
  end
26
11
 
27
12
  module InstanceMethods
28
- def check_failed_logins_limit_reached(user, credentials)
29
- now = Time.now.utc
30
-
31
- # not banned
32
- if session[:first_failed_login_time]
33
- reset_failed_logins_if_time_passed(now)
34
- else
35
- session[:first_failed_login_time] = now
36
- end
37
- increment_failed_logins
38
- # ban
39
- ban_if_above_limit(now)
40
- end
41
13
 
42
14
  protected
43
15
 
44
- def release_ban_if_time_passed(now)
45
- if now - session[:ban_start_time] > Config.login_ban_time_period
46
- session[:banned] = nil
47
- session[:failed_logins] = 0
48
- return true
49
- end
50
- false
51
- end
52
-
53
- def increment_failed_logins
54
- session[:failed_logins] ||= 0
55
- session[:failed_logins] += 1
56
- end
57
-
58
- def reset_failed_logins_if_time_passed(now)
59
- if now - session[:first_failed_login_time] > Config.login_retries_time_period
60
- session[:failed_logins] = 0
61
- session[:first_failed_login_time] = now
62
- end
63
- end
64
-
65
- def ban_if_above_limit(now)
66
- if session[:failed_logins] > Config.login_retries_amount_allowed
67
- session[:banned] = true
68
- session[:ban_start_time] = now
69
- end
70
- end
71
-
72
- def deny_banned_user
73
- if session[:banned]
74
- now = Time.now.utc
75
- release_ban_if_time_passed(now)
76
- end
77
-
78
- # if still banned
79
- send(Config.banned_action) if session[:banned]
16
+ def update_failed_logins_count!(credentials)
17
+ user = User.where("#{User.sorcery_config.username_attribute_name} = ?", credentials[0]).first
18
+ user.register_failed_login! if user
80
19
  end
81
20
 
82
- def default_banned_action
83
- render :nothing => true
21
+ def reset_failed_logins_count!(user, credentials)
22
+ user.update_attributes!(User.sorcery_config.failed_logins_count_attribute_name => 0)
84
23
  end
85
24
  end
86
25
  end
@@ -23,19 +23,22 @@ module Sorcery
23
23
 
24
24
  # to be used as a before_filter.
25
25
  # The method sets a session when requesting the user's credentials.
26
- # This is a trick to overcome the way HTTP authentication work (explained below):
26
+ # This is a trick to overcome the way HTTP authentication works (explained below):
27
27
  #
28
28
  # Once the user fills the credentials once, the browser will always send it to the server when visiting the website, until the browser is closed.
29
29
  # This causes wierd behaviour if the user logs out. The session is reset, yet the user is re-logged in by the before_filter calling 'login_from_basic_auth'.
30
30
  # To overcome this, we set a session when requesting the password, which logout will reset, and that's how we know if we need to request for HTTP auth again.
31
- def require_user_login_from_http
32
- request_http_basic_authentication(realm_name_by_controller) and (session[:http_authentication_used] = true) and return if request.authorization.nil? || session[:http_authentication_used].nil?
33
- require_user_login
31
+ def require_login_from_http_basic
32
+ (request_http_basic_authentication(realm_name_by_controller) and (session[:http_authentication_used] = true) and return) if (request.authorization.nil? || session[:http_authentication_used].nil?)
33
+ require_login
34
34
  end
35
35
 
36
+ # given to main controller module as a login source callback
36
37
  def login_from_basic_auth
37
38
  authenticate_with_http_basic do |username, password|
38
39
  @logged_in_user = (Config.user_class.authenticate(username, password) if session[:http_authentication_used]) || false
40
+ login_user(@logged_in_user) if @logged_in_user
41
+ @logged_in_user
39
42
  end
40
43
  end
41
44
 
@@ -21,11 +21,13 @@ module Sorcery
21
21
  end
22
22
 
23
23
  module InstanceMethods
24
+ protected
25
+
24
26
  def register_login_time(user, credentials)
25
27
  session[:login_time] = session[:last_action_time] = Time.now.utc
26
28
  end
27
29
 
28
- # To be used as a before_filter, before require_user_login
30
+ # To be used as a before_filter, before require_login
29
31
  def validate_session
30
32
  session_to_use = Config.session_timeout_from_last_action ? session[:last_action_time] : session[:login_time]
31
33
  if session_to_use && (Time.now.utc - session_to_use > Config.session_timeout)
@@ -35,6 +37,7 @@ module Sorcery
35
37
  session[:last_action_time] = Time.now.utc
36
38
  end
37
39
  end
40
+
38
41
  end
39
42
  end
40
43
  end
@@ -1,8 +1,4 @@
1
- begin
2
- require "bcrypt"
3
- rescue LoadError
4
- "sudo gem install bcrypt-ruby"
5
- end
1
+ require 'bcrypt'
6
2
 
7
3
  module Sorcery
8
4
  module CryptoProviders