sorcery 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. data/README.rdoc +9 -1
  2. data/VERSION +1 -1
  3. data/lib/sorcery/controller/adapters/sinatra.rb +35 -28
  4. data/lib/sorcery/controller/submodules/activity_logging.rb +11 -7
  5. data/lib/sorcery/controller/submodules/brute_force_protection.rb +8 -5
  6. data/lib/sorcery/controller/submodules/external.rb +11 -6
  7. data/lib/sorcery/controller/submodules/http_basic_auth.rb +12 -7
  8. data/lib/sorcery/controller/submodules/remember_me.rb +14 -5
  9. data/lib/sorcery/controller/submodules/session_timeout.rb +3 -1
  10. data/lib/sorcery/controller.rb +12 -9
  11. data/lib/sorcery/crypto_providers/aes256.rb +8 -5
  12. data/lib/sorcery/crypto_providers/bcrypt.rb +12 -6
  13. data/lib/sorcery/crypto_providers/sha256.rb +2 -1
  14. data/lib/sorcery/crypto_providers/sha512.rb +2 -1
  15. data/lib/sorcery/engine.rb +0 -7
  16. data/lib/sorcery/initializers/initializer.rb +127 -36
  17. data/lib/sorcery/model/adapters/active_record.rb +2 -2
  18. data/lib/sorcery/model/adapters/mongoid.rb +4 -4
  19. data/lib/sorcery/model/submodules/activity_logging.rb +7 -6
  20. data/lib/sorcery/model/submodules/brute_force_protection.rb +10 -6
  21. data/lib/sorcery/model/submodules/external.rb +4 -2
  22. data/lib/sorcery/model/submodules/remember_me.rb +4 -3
  23. data/lib/sorcery/model/submodules/reset_password.rb +16 -8
  24. data/lib/sorcery/model/submodules/user_activation.rb +23 -10
  25. data/lib/sorcery/model/temporary_token.rb +3 -2
  26. data/lib/sorcery/model.rb +59 -28
  27. data/lib/sorcery/test_helpers/internal/rails.rb +6 -1
  28. data/lib/sorcery/test_helpers/internal/sinatra.rb +3 -3
  29. data/lib/sorcery/test_helpers/internal/sinatra_modular.rb +74 -0
  30. data/lib/sorcery/test_helpers/internal.rb +2 -1
  31. data/lib/sorcery.rb +3 -5
  32. data/sorcery.gemspec +72 -2
  33. data/spec/Gemfile.lock +1 -1
  34. data/spec/rails3/Gemfile.lock +1 -1
  35. data/spec/rails3/spec/controller_oauth2_spec.rb +3 -24
  36. data/spec/rails3/spec/controller_oauth_spec.rb +3 -24
  37. data/spec/rails3/spec/controller_spec.rb +2 -2
  38. data/spec/rails3/spec/user_activation_spec.rb +2 -168
  39. data/spec/rails3/spec/user_activity_logging_spec.rb +2 -30
  40. data/spec/rails3/spec/user_brute_force_protection_spec.rb +2 -35
  41. data/spec/rails3/spec/user_oauth_spec.rb +2 -26
  42. data/spec/rails3/spec/user_remember_me_spec.rb +2 -45
  43. data/spec/rails3/spec/user_reset_password_spec.rb +3 -168
  44. data/spec/rails3/spec/user_spec.rb +3 -293
  45. data/spec/rails3_mongoid/Gemfile.lock +1 -1
  46. data/spec/rails3_mongoid/app/models/authentication.rb +3 -3
  47. data/spec/rails3_mongoid/spec/user_activation_spec.rb +2 -171
  48. data/spec/rails3_mongoid/spec/user_activity_logging_spec.rb +2 -25
  49. data/spec/rails3_mongoid/spec/user_brute_force_protection_spec.rb +2 -35
  50. data/spec/rails3_mongoid/spec/user_oauth_spec.rb +2 -28
  51. data/spec/rails3_mongoid/spec/user_remember_me_spec.rb +2 -45
  52. data/spec/rails3_mongoid/spec/user_reset_password_spec.rb +2 -176
  53. data/spec/rails3_mongoid/spec/user_spec.rb +3 -295
  54. data/spec/shared_examples/controller_oauth2_shared_examples.rb +37 -0
  55. data/spec/shared_examples/controller_oauth_shared_examples.rb +37 -0
  56. data/spec/shared_examples/user_activation_shared_examples.rb +173 -0
  57. data/spec/shared_examples/user_activity_logging_shared_examples.rb +27 -0
  58. data/spec/shared_examples/user_brute_force_protection_shared_examples.rb +37 -0
  59. data/spec/shared_examples/user_oauth_shared_examples.rb +31 -0
  60. data/spec/shared_examples/user_remember_me_shared_examples.rb +47 -0
  61. data/spec/shared_examples/user_reset_password_shared_examples.rb +177 -0
  62. data/spec/shared_examples/user_shared_examples.rb +292 -0
  63. data/spec/sinatra/Gemfile.lock +1 -1
  64. data/spec/sinatra/Rakefile +1 -1
  65. data/spec/sinatra/filters.rb +20 -14
  66. data/spec/sinatra/modular.rb +157 -0
  67. data/spec/sinatra/spec/controller_oauth2_spec.rb +3 -24
  68. data/spec/sinatra/spec/controller_oauth_spec.rb +3 -24
  69. data/spec/sinatra/spec/controller_spec.rb +2 -2
  70. data/spec/sinatra_modular/Gemfile +15 -0
  71. data/spec/sinatra_modular/Gemfile.lock +117 -0
  72. data/spec/sinatra_modular/Rakefile +11 -0
  73. data/spec/sinatra_modular/authentication.rb +3 -0
  74. data/spec/sinatra_modular/db/migrate/activation/20101224223622_add_activation_to_users.rb +17 -0
  75. data/spec/sinatra_modular/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
  76. data/spec/sinatra_modular/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
  77. data/spec/sinatra_modular/db/migrate/core/20101224223620_create_users.rb +16 -0
  78. data/spec/sinatra_modular/db/migrate/external/20101224223628_create_authentications.rb +14 -0
  79. data/spec/sinatra_modular/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
  80. data/spec/sinatra_modular/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
  81. data/spec/sinatra_modular/filters.rb +27 -0
  82. data/spec/sinatra_modular/modular.rb +157 -0
  83. data/spec/sinatra_modular/myapp.rb +133 -0
  84. data/spec/sinatra_modular/sorcery_mailer.rb +25 -0
  85. data/spec/sinatra_modular/spec_modular/controller_activity_logging_spec.rb +85 -0
  86. data/spec/sinatra_modular/spec_modular/controller_brute_force_protection_spec.rb +70 -0
  87. data/spec/sinatra_modular/spec_modular/controller_http_basic_auth_spec.rb +53 -0
  88. data/spec/sinatra_modular/spec_modular/controller_oauth2_spec.rb +99 -0
  89. data/spec/sinatra_modular/spec_modular/controller_oauth_spec.rb +100 -0
  90. data/spec/sinatra_modular/spec_modular/controller_remember_me_spec.rb +64 -0
  91. data/spec/sinatra_modular/spec_modular/controller_session_timeout_spec.rb +57 -0
  92. data/spec/sinatra_modular/spec_modular/controller_spec.rb +116 -0
  93. data/spec/sinatra_modular/spec_modular/spec.opts +2 -0
  94. data/spec/sinatra_modular/spec_modular/spec_helper.rb +51 -0
  95. data/spec/sinatra_modular/user.rb +6 -0
  96. data/spec/sinatra_modular/views/test_login.erb +4 -0
  97. metadata +72 -2
data/README.rdoc CHANGED
@@ -29,7 +29,7 @@ Example Rails 3 app using sorcery: https://github.com/NoamB/sorcery-example-app
29
29
 
30
30
  Example Sinatra app using sorcery: https://github.com/NoamB/sorcery-example-app-sinatra
31
31
 
32
- Documentation: http://rubydoc.info/gems/sorcery/0.5.1/frames
32
+ Documentation: http://rubydoc.info/gems/sorcery/0.6.0/frames
33
33
 
34
34
  Check out the tutorials in the github wiki!
35
35
 
@@ -224,6 +224,14 @@ The patch version changes are backward compatible.
224
224
  In short, an app that works with x.3.1 should be able to upgrade to x.3.2 with no code changes.
225
225
  The same cannot be said about upgrading to x.4.0 and above, however.
226
226
 
227
+ == Upgrading
228
+
229
+ Important notes while upgrading:
230
+
231
+ * If upgrading from <= v0.5.1 to >= v0.5.2 you need to explicitly set your user_class model in the initializer file.
232
+
233
+ # This line must come after the 'user config' block.
234
+ config.user_class = User
227
235
 
228
236
  == Contributing to sorcery
229
237
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.6.0
@@ -16,7 +16,7 @@ module Sorcery
16
16
  process_route(*arguments) { instance_eval(&block) }
17
17
  end
18
18
  end
19
-
19
+
20
20
  def after_filter(filter)
21
21
  after do
22
22
  send(filter)
@@ -24,66 +24,73 @@ module Sorcery
24
24
  end
25
25
  end
26
26
  end
27
-
27
+
28
28
  ::Sinatra::Request.class_eval do
29
29
  def authorization
30
- env['HTTP_AUTHORIZATION'] ||
31
- env['X-HTTP_AUTHORIZATION'] ||
32
- env['X_HTTP_AUTHORIZATION'] ||
33
- env['REDIRECT_X_HTTP_AUTHORIZATION'] || nil
30
+ env['HTTP_AUTHORIZATION'] ||
31
+ env['X-HTTP_AUTHORIZATION'] ||
32
+ env['X_HTTP_AUTHORIZATION'] ||
33
+ env['REDIRECT_X_HTTP_AUTHORIZATION'] || nil
34
34
  end
35
35
  end
36
-
36
+
37
+ base.send(:include, SorceryHelpers)
37
38
  base.send(:include, InstanceMethods)
38
39
  base.extend(ClassMethods)
39
40
  end
40
-
41
+
42
+ module SorceryHelpers
43
+
44
+ def request_http_basic_authentication(realm)
45
+ response.header['WWW-Authenticate'] = %(Basic realm="#{realm}")
46
+ response.status = 401
47
+ end
48
+
49
+ def authenticate_with_http_basic(&blk)
50
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
51
+ yield @auth.credentials if (@auth.provided? && @auth.basic? && @auth.credentials)
52
+ end
53
+
54
+ end
55
+
41
56
  module InstanceMethods
42
57
  def reset_session
43
58
  session.clear
44
59
  end
45
-
60
+
46
61
  def redirect_to(*args)
47
62
  args.pop if args.last.is_a?(Hash)
48
63
  redirect(*args)
49
64
  end
50
-
65
+
51
66
  def root_path
52
67
  '/'
53
68
  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
69
+
70
+ ::Sinatra.helpers do
71
+ include SorceryHelpers
65
72
  end
66
-
73
+
67
74
  def cookies
68
- @cookie_proxy ||= CookieProxy.new(request,response)
75
+ @cookie_proxy ||= CookieProxy.new(request, response)
69
76
  end
70
77
  end
71
-
78
+
72
79
  class CookieProxy
73
- def initialize(request,response)
80
+ def initialize(request, response)
74
81
  @request = request
75
82
  @response = response
76
83
  end
77
-
84
+
78
85
  def [](key)
79
86
  @request.cookies[key.to_s]
80
87
  end
81
-
88
+
82
89
  def []=(key, value)
83
90
  @response.set_cookie(key, value)
84
91
  end
85
92
  end
86
-
93
+
87
94
  module ClassMethods
88
95
  def prepend_before_filter(filter)
89
96
  prepend_filter(:before) do
@@ -1,13 +1,16 @@
1
1
  module Sorcery
2
2
  module Controller
3
3
  module Submodules
4
- # This submodule keeps track of events such as login, logout, and last activity time, per user.
4
+ # This submodule keeps track of events such as login, logout,
5
+ # and last activity time, per user.
5
6
  # It helps in estimating which users are active now in the site.
6
- # This cannot be determined absolutely because a user might be reading a page without clicking anything for a while.
7
-
8
- # This is the controller part of the submodule, which adds hooks to register user events,
7
+ # This cannot be determined absolutely because a user might be
8
+ # reading a page without clicking anything for a while.
9
+ # This is the controller part of the submodule, which adds hooks
10
+ # to register user events,
9
11
  # and methods to collect active users data for use in the app.
10
- # see Socery::Model::Submodules::ActivityLogging for configuration options.
12
+ # see Socery::Model::Submodules::ActivityLogging for configuration
13
+ # options.
11
14
  module ActivityLogging
12
15
  def self.included(base)
13
16
  base.send(:include, InstanceMethods)
@@ -19,9 +22,10 @@ module Sorcery
19
22
  module InstanceMethods
20
23
  # Returns an array of the active users.
21
24
  def current_users
22
- Config.user_class.current_users
25
+ user_class.current_users
23
26
  # A possible patch here:
24
- # we'll add the current_user to the users list if he's not in it (can happen when he was inactive for more than activity timeout):
27
+ # we'll add the current_user to the users list if he's not in it
28
+ # (can happen when he was inactive for more than activity timeout):
25
29
  #
26
30
  # users.unshift!(current_user) if logged_in? && users.find {|u| u.id == current_user.id}.nil?
27
31
  #
@@ -1,9 +1,12 @@
1
1
  module Sorcery
2
2
  module Controller
3
3
  module Submodules
4
- # This module helps protect user accounts by locking them down after too many failed attemps to login were detected.
5
- # This is the controller part of the submodule which takes care of updating the failed logins and resetting them.
6
- # See Sorcery::Model::Submodules::BruteForceProtection for configuration options.
4
+ # This module helps protect user accounts by locking them down after too
5
+ # many failed attemps to login were detected.
6
+ # This is the controller part of the submodule which takes care of
7
+ # updating the failed logins and resetting them.
8
+ # See Sorcery::Model::Submodules::BruteForceProtection for configuration
9
+ # options.
7
10
  module BruteForceProtection
8
11
  def self.included(base)
9
12
  base.send(:include, InstanceMethods)
@@ -19,14 +22,14 @@ module Sorcery
19
22
  # Increments the failed logins counter on every failed login.
20
23
  # Runs as a hook after a failed login.
21
24
  def update_failed_logins_count!(credentials)
22
- user = Config.user_class.find_by_credentials(credentials)
25
+ user = user_class.find_by_credentials(credentials)
23
26
  user.register_failed_login! if user
24
27
  end
25
28
 
26
29
  # Resets the failed logins counter.
27
30
  # Runs as a hook after a successful login.
28
31
  def reset_failed_logins_count!(user, credentials)
29
- user.update_attributes!(Config.user_class.sorcery_config.failed_logins_count_attribute_name => 0)
32
+ user.update_attributes!(user_class.sorcery_config.failed_logins_count_attribute_name => 0)
30
33
  end
31
34
  end
32
35
  end
@@ -43,7 +43,7 @@ module Sorcery
43
43
  @provider = Config.send(provider)
44
44
  @provider.process_callback(params,session)
45
45
  @user_hash = @provider.get_user_hash
46
- if user = Config.user_class.load_from_provider(provider,@user_hash[:uid])
46
+ if user = user_class.load_from_provider(provider,@user_hash[:uid])
47
47
  reset_session
48
48
  login_user(user)
49
49
  user
@@ -65,14 +65,19 @@ module Sorcery
65
65
  provider = provider.to_sym
66
66
  @provider = Config.send(provider)
67
67
  @user_hash = @provider.get_user_hash
68
- config = Config.user_class.sorcery_config
68
+ config = user_class.sorcery_config
69
69
  attrs = {}
70
70
  @provider.user_info_mapping.each do |k,v|
71
- (varr = v.split("/")).size > 1 ? attrs.merge!(k => varr.inject(@user_hash[:user_info]) {|hsh,v| hsh[v] }) : attrs.merge!(k => @user_hash[:user_info][v])
71
+ if (varr = v.split("/")).size > 1
72
+ attribute_value = varr.inject(@user_hash[:user_info]) {|hsh,v| hsh[v] } rescue nil
73
+ attribute_value.nil? ? attrs : attrs.merge!(k => attribute_value)
74
+ else
75
+ attrs.merge!(k => @user_hash[:user_info][v])
76
+ end
72
77
  end
73
- Config.user_class.transaction do
74
- @user = Config.user_class.create!(attrs)
75
- Config.user_class.sorcery_config.authentications_class.create!({config.authentications_user_id_attribute_name => @user.id, config.provider_attribute_name => provider, config.provider_uid_attribute_name => @user_hash[:uid]})
78
+ user_class.transaction do
79
+ @user = user_class.create!(attrs)
80
+ user_class.sorcery_config.authentications_class.create!({config.authentications_user_id_attribute_name => @user.id, config.provider_attribute_name => provider, config.provider_uid_attribute_name => @user_hash[:uid]})
76
81
  end
77
82
  @user
78
83
  end
@@ -2,8 +2,10 @@ module Sorcery
2
2
  module Controller
3
3
  module Submodules
4
4
  # This submodule integrates HTTP Basic authentication into sorcery.
5
- # You are provided with a before filter, require_login_from_http_basic, which requests the browser for authentication.
6
- # Then the rest of the submodule takes care of logging the user in into the session, so that the next requests will keep him logged in.
5
+ # You are provided with a before filter, require_login_from_http_basic,
6
+ # which requests the browser for authentication.
7
+ # Then the rest of the submodule takes care of logging the user in
8
+ # into the session, so that the next requests will keep him logged in.
7
9
  module HttpBasicAuth
8
10
  def self.included(base)
9
11
  base.send(:include, InstanceMethods)
@@ -21,16 +23,19 @@ module Sorcery
21
23
  end
22
24
 
23
25
  module InstanceMethods
24
-
26
+
25
27
  protected
26
28
 
27
29
  # to be used as a before_filter.
28
30
  # The method sets a session when requesting the user's credentials.
29
31
  # This is a trick to overcome the way HTTP authentication works (explained below):
30
32
  #
31
- # 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.
32
- # 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'.
33
- # 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.
33
+ # Once the user fills the credentials once, the browser will always send it to the
34
+ # server when visiting the website, until the browser is closed.
35
+ # This causes wierd behaviour if the user logs out. The session is reset, yet the
36
+ # user is re-logged in by the before_filter calling 'login_from_basic_auth'.
37
+ # To overcome this, we set a session when requesting the password, which logout will
38
+ # reset, and that's how we know if we need to request for HTTP auth again.
34
39
  def require_login_from_http_basic
35
40
  (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?)
36
41
  require_login
@@ -40,7 +45,7 @@ module Sorcery
40
45
  # given to main controller module as a login source callback
41
46
  def login_from_basic_auth
42
47
  authenticate_with_http_basic do |username, password|
43
- @current_user = (Config.user_class.authenticate(username, password) if session[:http_authentication_used]) || false
48
+ @current_user = (user_class.authenticate(username, password) if session[:http_authentication_used]) || false
44
49
  login_user(@current_user) if @current_user
45
50
  @current_user
46
51
  end
@@ -1,7 +1,8 @@
1
1
  module Sorcery
2
2
  module Controller
3
3
  module Submodules
4
- # The Remember Me submodule takes care of setting the user's cookie so that he will be automatically logged in to the site on every visit,
4
+ # The Remember Me submodule takes care of setting the user's cookie so that he will
5
+ # be automatically logged in to the site on every visit,
5
6
  # until the cookie expires.
6
7
  # See Sorcery::Model::Submodules::RememberMe for configuration options.
7
8
  module RememberMe
@@ -16,7 +17,7 @@ module Sorcery
16
17
  # This method sets the cookie and calls the user to save the token and the expiration to db.
17
18
  def remember_me!
18
19
  current_user.remember_me!
19
- cookies[:remember_me_token] = { :value => current_user.remember_me_token, :expires => current_user.remember_me_token_expires_at }
20
+ set_remember_me_cookie!(current_user)
20
21
  end
21
22
 
22
23
  # Clears the cookie and clears the token from the db.
@@ -33,17 +34,25 @@ module Sorcery
33
34
  remember_me! if credentials.size == 3 && credentials[2]
34
35
  end
35
36
 
36
- # Checks the cookie for a remember me token, tried to find a user with that token and logs the user in if found.
37
+ # Checks the cookie for a remember me token, tried to find a user with that token
38
+ # and logs the user in if found.
37
39
  # Runs as a login source. See 'current_user' method for how it is used.
38
40
  def login_from_cookie
39
- user = cookies[:remember_me_token] && Config.user_class.find_by_remember_me_token(cookies[:remember_me_token])
41
+ user = cookies[:remember_me_token] && user_class.find_by_remember_me_token(cookies[:remember_me_token])
40
42
  if user && user.remember_me_token?
41
- cookies[:remember_me_token] = { :value => user.remember_me_token, :expires => user.remember_me_token_expires_at }
43
+ set_remember_me_cookie!(user)
42
44
  @current_user = user
43
45
  else
44
46
  @current_user = false
45
47
  end
46
48
  end
49
+
50
+ def set_remember_me_cookie!(user)
51
+ cookies[:remember_me_token] = {
52
+ :value => user.send(user.sorcery_config.remember_me_token_attribute_name),
53
+ :expires => user.send(user.sorcery_config.remember_me_token_expires_at_attribute_name)
54
+ }
55
+ end
47
56
  end
48
57
 
49
58
  end
@@ -9,7 +9,9 @@ module Sorcery
9
9
  Config.module_eval do
10
10
  class << self
11
11
  attr_accessor :session_timeout, # how long in seconds to keep the session alive.
12
- :session_timeout_from_last_action # use the last action as the beginning of session timeout.
12
+
13
+ :session_timeout_from_last_action # use the last action as the beginning of session
14
+ # timeout.
13
15
 
14
16
  def merge_session_timeout_defaults!
15
17
  @defaults.merge!(:@session_timeout => 3600, # 1.hour
@@ -29,7 +29,7 @@ module Sorcery
29
29
  # Takes credentials and returns a user on successful authentication.
30
30
  # Runs hooks after login or failed login.
31
31
  def login(*credentials)
32
- user = Config.user_class.authenticate(*credentials)
32
+ user = user_class.authenticate(*credentials)
33
33
  if user
34
34
  return_to_url = session[:return_to_url]
35
35
  reset_session # protect from session fixation attacks
@@ -62,7 +62,8 @@ module Sorcery
62
62
  @current_user ||= login_from_session || login_from_other_sources unless @current_user == false
63
63
  end
64
64
 
65
- # used when a user tries to access a page while logged out, is asked to login, and we want to return him back to the page he originally wanted.
65
+ # used when a user tries to access a page while logged out, is asked to login,
66
+ # and we want to return him back to the page he originally wanted.
66
67
  def redirect_back_or_to(url, flash_hash = {})
67
68
  redirect_to(session[:return_to_url] || url, :flash => flash_hash)
68
69
  end
@@ -90,7 +91,7 @@ module Sorcery
90
91
  end
91
92
 
92
93
  def login_from_session
93
- @current_user = (Config.user_class.find_by_id(session[:user_id]) if session[:user_id]) || false
94
+ @current_user = (user_class.find_by_id(session[:user_id]) if session[:user_id]) || false
94
95
  end
95
96
 
96
97
  def after_login!(user, credentials)
@@ -109,25 +110,27 @@ module Sorcery
109
110
  Config.after_logout.each {|c| self.send(c)}
110
111
  end
111
112
 
113
+ def user_class
114
+ @user_class ||= Config.user_class.to_s.constantize
115
+ end
112
116
  end
113
117
 
114
118
  module Config
115
119
  class << self
116
120
  attr_accessor :submodules,
117
-
118
- :user_class, # what class to use as the user class. Set automatically when you call authenticates_with_sorcery! in the User class.
119
-
121
+ :user_class, # what class to use as the user class.
120
122
  :not_authenticated_action, # what controller action to call for non-authenticated users.
121
123
 
122
- :save_return_to_url, # when a non logged in user tries to enter a page that requires login, save the URL he wanted to reach,
124
+ :save_return_to_url, # when a non logged in user tries to enter a page that requires
125
+ # login, save the URL he wanted to reach,
123
126
  # and send him there after login.
124
127
 
125
128
  :login_sources,
126
129
  :after_login,
127
130
  :after_failed_login,
128
131
  :before_logout,
129
- :after_logout
130
-
132
+ :after_logout
133
+
131
134
  def init!
132
135
  @defaults = {
133
136
  :@user_class => nil,
@@ -2,18 +2,21 @@ require "openssl"
2
2
 
3
3
  module Sorcery
4
4
  module CryptoProviders
5
- # This encryption method is reversible if you have the supplied key. So in order to use this encryption method you must supply it with a key first.
5
+ # This encryption method is reversible if you have the supplied key.
6
+ # So in order to use this encryption method you must supply it with a key first.
6
7
  # In an initializer, or before your application initializes, you should do the following:
7
8
  #
8
9
  # Sorcery::Model::ConfigAES256.key = "my 32 bytes long key"
9
10
  #
10
- # My final comment is that this is a strong encryption method, but its main weakness is that its reversible. If you do not need to reverse the hash
11
+ # My final comment is that this is a strong encryption method,
12
+ # but its main weakness is that its reversible. If you do not need to reverse the hash
11
13
  # then you should consider Sha512 or BCrypt instead.
12
14
  #
13
15
  # Keep your key in a safe place, some even say the key should be stored on a separate server.
14
- # This won't hurt performance because the only time it will try and access the key on the separate server is during initialization, which only
15
- # happens once. The reasoning behind this is if someone does compromise your server they won't have the key also. Basically, you don't want to
16
- # store the key with the lock.
16
+ # This won't hurt performance because the only time it will try and access the key on the
17
+ # separate server is during initialization, which only
18
+ # happens once. The reasoning behind this is if someone does compromise your server they
19
+ # won't have the key also. Basically, you don't want to store the key with the lock.
17
20
  class AES256
18
21
  class << self
19
22
  attr_writer :key
@@ -2,9 +2,12 @@ require 'bcrypt'
2
2
 
3
3
  module Sorcery
4
4
  module CryptoProviders
5
- # For most apps Sha512 is plenty secure, but if you are building an app that stores nuclear launch codes you might want to consier BCrypt. This is an extremely
6
- # secure hashing algorithm, mainly because it is slow. A brute force attack on a BCrypt encrypted password would take much longer than a brute force attack on a
7
- # password encrypted with a Sha algorithm. Keep in mind you are sacrificing performance by using this, generating a password takes exponentially longer than any
5
+ # For most apps Sha512 is plenty secure, but if you are building an app that stores nuclear
6
+ # launch codes you might want to consier BCrypt. This is an extremely
7
+ # secure hashing algorithm, mainly because it is slow.
8
+ # A brute force attack on a BCrypt encrypted password would take much longer than a brute force attack on a
9
+ # password encrypted with a Sha algorithm. Keep in mind you are sacrificing performance by using this,
10
+ # generating a password takes exponentially longer than any
8
11
  # of the Sha algorithms. I did some benchmarking to save you some time with your decision:
9
12
  #
10
13
  # require "bcrypt"
@@ -37,8 +40,10 @@ module Sorcery
37
40
  # You are good to go!
38
41
  class BCrypt
39
42
  class << self
40
- # This is the :cost option for the BCrpyt library. The higher the cost the more secure it is and the longer is take the generate a hash. By default this is 10.
41
- # Set this to whatever you want, play around with it to get that perfect balance between security and performance.
43
+ # This is the :cost option for the BCrpyt library.
44
+ # The higher the cost the more secure it is and the longer is take the generate a hash. By default this is 10.
45
+ # Set this to whatever you want, play around with it to get that perfect balance between
46
+ # security and performance.
42
47
  def cost
43
48
  @cost ||= 10
44
49
  end
@@ -57,7 +62,8 @@ module Sorcery
57
62
  hash == join_tokens(tokens)
58
63
  end
59
64
 
60
- # This method is used as a flag to tell Sorcery to "resave" the password upon a successful login, using the new cost
65
+ # This method is used as a flag to tell Sorcery to "resave" the password
66
+ # upon a successful login, using the new cost
61
67
  def cost_matches?(hash)
62
68
  hash = new_from_hash(hash)
63
69
  if hash.nil? || hash == {}
@@ -1,7 +1,8 @@
1
1
  require "digest/sha2"
2
2
 
3
3
  module Sorcery
4
- # The activate_sorcery method has a custom_crypto_provider configuration option. This allows you to use any type of encryption you like.
4
+ # The activate_sorcery method has a custom_crypto_provider configuration option.
5
+ # This allows you to use any type of encryption you like.
5
6
  # Just create a class with a class level encrypt and matches? method. See example below.
6
7
  #
7
8
  # === Example
@@ -1,7 +1,8 @@
1
1
  require "digest/sha2"
2
2
 
3
3
  module Sorcery
4
- # The activate_sorcery method has a custom_crypto_provider configuration option. This allows you to use any type of encryption you like.
4
+ # The activate_sorcery method has a custom_crypto_provider configuration option.
5
+ # This allows you to use any type of encryption you like.
5
6
  # Just create a class with a class level encrypt and matches? method. See example below.
6
7
  #
7
8
  # === Example
@@ -12,13 +12,6 @@ module Sorcery
12
12
  ActionController::Base.helper_method :current_user
13
13
  end
14
14
 
15
- initializer "attempt to preload user model" do |app|
16
- begin
17
- require Rails.root + "app/models/user.rb"
18
- rescue LoadError
19
- end
20
- end
21
-
22
15
  rake_tasks do
23
16
  load "sorcery/railties/tasks.rake"
24
17
  end