sorcery 0.7.0 → 0.7.1

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 (164) hide show
  1. data/Gemfile +2 -1
  2. data/Gemfile.lock +58 -51
  3. data/README.rdoc +19 -57
  4. data/Rakefile +27 -1
  5. data/VERSION +1 -1
  6. data/lib/generators/sorcery/USAGE +22 -0
  7. data/lib/generators/sorcery/install_generator.rb +71 -0
  8. data/lib/{sorcery/initializers → generators/sorcery/templates}/initializer.rb +4 -4
  9. data/lib/generators/sorcery/templates/migration/activity_logging.rb +17 -0
  10. data/lib/generators/sorcery/templates/migration/brute_force_protection.rb +11 -0
  11. data/lib/generators/{sorcery_migration/templates → sorcery/templates/migration}/core.rb +2 -2
  12. data/lib/generators/{sorcery_migration/templates → sorcery/templates/migration}/external.rb +1 -1
  13. data/lib/generators/sorcery/templates/migration/remember_me.rb +15 -0
  14. data/lib/generators/sorcery/templates/migration/reset_password.rb +17 -0
  15. data/lib/generators/sorcery/templates/migration/user_activation.rb +17 -0
  16. data/lib/sorcery.rb +5 -7
  17. data/lib/sorcery/controller.rb +40 -39
  18. data/lib/sorcery/controller/submodules/activity_logging.rb +7 -10
  19. data/lib/sorcery/controller/submodules/external/protocols/oauth2.rb +23 -8
  20. data/lib/sorcery/controller/submodules/remember_me.rb +3 -2
  21. data/lib/sorcery/model.rb +21 -4
  22. data/lib/sorcery/model/adapters/mongo_mapper.rb +49 -0
  23. data/lib/sorcery/model/adapters/mongoid.rb +1 -1
  24. data/lib/sorcery/model/submodules/brute_force_protection.rb +8 -0
  25. data/lib/sorcery/model/submodules/remember_me.rb +8 -1
  26. data/lib/sorcery/model/submodules/reset_password.rb +10 -0
  27. data/lib/sorcery/model/submodules/user_activation.rb +12 -0
  28. data/sorcery.gemspec +86 -210
  29. data/spec/Gemfile +1 -1
  30. data/spec/Gemfile.lock +28 -15
  31. data/spec/README.md +6 -1
  32. data/spec/rails3/Gemfile +2 -2
  33. data/spec/rails3/Gemfile.lock +45 -50
  34. data/spec/rails3/spec/controller_oauth2_spec.rb +5 -3
  35. data/spec/rails3/spec/spec_helper.rb +1 -3
  36. data/spec/rails3_mongo_mapper/.gitignore +4 -0
  37. data/spec/rails3_mongo_mapper/.rspec +1 -0
  38. data/spec/{sinatra_modular → rails3_mongo_mapper}/Gemfile +5 -5
  39. data/spec/rails3_mongo_mapper/Gemfile.lock +159 -0
  40. data/spec/{sinatra → rails3_mongo_mapper}/Rakefile +3 -3
  41. data/spec/rails3_mongo_mapper/app/controllers/application_controller.rb +108 -0
  42. data/spec/rails3_mongo_mapper/app/helpers/application_helper.rb +2 -0
  43. data/spec/{sinatra → rails3_mongo_mapper/app/mailers}/sorcery_mailer.rb +0 -0
  44. data/spec/rails3_mongo_mapper/app/models/authentication.rb +6 -0
  45. data/spec/rails3_mongo_mapper/app/models/user.rb +5 -0
  46. data/spec/rails3_mongo_mapper/app/views/layouts/application.html.erb +14 -0
  47. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/activation_email.html.erb +17 -0
  48. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/activation_email.text.erb +9 -0
  49. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/activation_success_email.html.erb +17 -0
  50. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/activation_success_email.text.erb +9 -0
  51. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/reset_password_email.html.erb +16 -0
  52. data/spec/rails3_mongo_mapper/app/views/sorcery_mailer/reset_password_email.text.erb +8 -0
  53. data/spec/rails3_mongo_mapper/config.ru +4 -0
  54. data/spec/rails3_mongo_mapper/config/application.rb +51 -0
  55. data/spec/rails3_mongo_mapper/config/boot.rb +13 -0
  56. data/spec/rails3_mongo_mapper/config/environment.rb +5 -0
  57. data/spec/rails3_mongo_mapper/config/environments/development.rb +30 -0
  58. data/spec/rails3_mongo_mapper/config/environments/in_memory.rb +0 -0
  59. data/spec/rails3_mongo_mapper/config/environments/production.rb +49 -0
  60. data/spec/rails3_mongo_mapper/config/environments/test.rb +35 -0
  61. data/spec/rails3_mongo_mapper/config/initializers/backtrace_silencers.rb +7 -0
  62. data/spec/rails3_mongo_mapper/config/initializers/inflections.rb +10 -0
  63. data/spec/rails3_mongo_mapper/config/initializers/mime_types.rb +5 -0
  64. data/spec/rails3_mongo_mapper/config/initializers/mongo.rb +2 -0
  65. data/spec/rails3_mongo_mapper/config/initializers/secret_token.rb +7 -0
  66. data/spec/rails3_mongo_mapper/config/initializers/session_store.rb +8 -0
  67. data/spec/rails3_mongo_mapper/config/locales/en.yml +5 -0
  68. data/spec/rails3_mongo_mapper/config/routes.rb +59 -0
  69. data/spec/rails3_mongo_mapper/db/schema.rb +23 -0
  70. data/spec/rails3_mongo_mapper/db/seeds.rb +7 -0
  71. data/spec/rails3_mongo_mapper/lib/tasks/.gitkeep +0 -0
  72. data/spec/rails3_mongo_mapper/public/404.html +26 -0
  73. data/spec/rails3_mongo_mapper/public/422.html +26 -0
  74. data/spec/rails3_mongo_mapper/public/500.html +26 -0
  75. data/spec/rails3_mongo_mapper/public/favicon.ico +0 -0
  76. data/spec/rails3_mongo_mapper/public/images/rails.png +0 -0
  77. data/spec/rails3_mongo_mapper/public/javascripts/application.js +2 -0
  78. data/spec/rails3_mongo_mapper/public/javascripts/controls.js +965 -0
  79. data/spec/rails3_mongo_mapper/public/javascripts/dragdrop.js +974 -0
  80. data/spec/rails3_mongo_mapper/public/javascripts/effects.js +1123 -0
  81. data/spec/rails3_mongo_mapper/public/javascripts/prototype.js +6001 -0
  82. data/spec/rails3_mongo_mapper/public/javascripts/rails.js +175 -0
  83. data/spec/rails3_mongo_mapper/public/robots.txt +5 -0
  84. data/spec/rails3_mongo_mapper/public/stylesheets/.gitkeep +0 -0
  85. data/spec/rails3_mongo_mapper/script/rails +6 -0
  86. data/spec/{sinatra → rails3_mongo_mapper}/spec/controller_spec.rb +45 -42
  87. data/spec/{sinatra → rails3_mongo_mapper}/spec/spec.opts +0 -0
  88. data/spec/rails3_mongo_mapper/spec/spec_helper.orig.rb +27 -0
  89. data/spec/rails3_mongo_mapper/spec/spec_helper.rb +55 -0
  90. data/spec/rails3_mongo_mapper/spec/user_activation_spec.rb +9 -0
  91. data/spec/rails3_mongo_mapper/spec/user_activity_logging_spec.rb +8 -0
  92. data/spec/rails3_mongo_mapper/spec/user_brute_force_protection_spec.rb +8 -0
  93. data/spec/rails3_mongo_mapper/spec/user_oauth_spec.rb +8 -0
  94. data/spec/rails3_mongo_mapper/spec/user_remember_me_spec.rb +8 -0
  95. data/spec/rails3_mongo_mapper/spec/user_reset_password_spec.rb +8 -0
  96. data/spec/rails3_mongo_mapper/spec/user_spec.rb +37 -0
  97. data/spec/rails3_mongo_mapper/vendor/plugins/.gitkeep +0 -0
  98. data/spec/rails3_mongoid/Gemfile +1 -1
  99. data/spec/rails3_mongoid/Gemfile.lock +44 -35
  100. data/spec/rails3_mongoid/spec/controller_activity_logging_spec.rb +98 -0
  101. data/spec/rails3_mongoid/spec/user_spec.rb +1 -0
  102. data/spec/shared_examples/user_shared_examples.rb +29 -1
  103. metadata +124 -237
  104. data/lib/generators/sorcery_migration/sorcery_migration_generator.rb +0 -24
  105. data/lib/generators/sorcery_migration/templates/activity_logging.rb +0 -17
  106. data/lib/generators/sorcery_migration/templates/brute_force_protection.rb +0 -11
  107. data/lib/generators/sorcery_migration/templates/remember_me.rb +0 -15
  108. data/lib/generators/sorcery_migration/templates/reset_password.rb +0 -17
  109. data/lib/generators/sorcery_migration/templates/user_activation.rb +0 -17
  110. data/lib/sorcery/controller/adapters/sinatra.rb +0 -115
  111. data/lib/sorcery/sinatra.rb +0 -4
  112. data/lib/sorcery/test_helpers/internal/sinatra.rb +0 -74
  113. data/lib/sorcery/test_helpers/internal/sinatra_modular.rb +0 -74
  114. data/lib/sorcery/test_helpers/sinatra.rb +0 -88
  115. data/spec/rails3/Rakefile.unused +0 -7
  116. data/spec/sinatra/Gemfile +0 -15
  117. data/spec/sinatra/Gemfile.lock +0 -115
  118. data/spec/sinatra/authentication.rb +0 -3
  119. data/spec/sinatra/db/migrate/activation/20101224223622_add_activation_to_users.rb +0 -17
  120. data/spec/sinatra/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +0 -17
  121. data/spec/sinatra/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +0 -11
  122. data/spec/sinatra/db/migrate/core/20101224223620_create_users.rb +0 -16
  123. data/spec/sinatra/db/migrate/external/20101224223628_create_authentications.rb +0 -14
  124. data/spec/sinatra/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +0 -15
  125. data/spec/sinatra/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +0 -13
  126. data/spec/sinatra/filters.rb +0 -27
  127. data/spec/sinatra/modular.rb +0 -157
  128. data/spec/sinatra/myapp.rb +0 -133
  129. data/spec/sinatra/spec/controller_activity_logging_spec.rb +0 -85
  130. data/spec/sinatra/spec/controller_brute_force_protection_spec.rb +0 -70
  131. data/spec/sinatra/spec/controller_http_basic_auth_spec.rb +0 -53
  132. data/spec/sinatra/spec/controller_oauth2_spec.rb +0 -96
  133. data/spec/sinatra/spec/controller_oauth_spec.rb +0 -100
  134. data/spec/sinatra/spec/controller_remember_me_spec.rb +0 -64
  135. data/spec/sinatra/spec/controller_session_timeout_spec.rb +0 -57
  136. data/spec/sinatra/spec/spec_helper.rb +0 -45
  137. data/spec/sinatra/user.rb +0 -6
  138. data/spec/sinatra/views/test_login.erb +0 -4
  139. data/spec/sinatra_modular/Gemfile.lock +0 -115
  140. data/spec/sinatra_modular/Rakefile +0 -11
  141. data/spec/sinatra_modular/authentication.rb +0 -3
  142. data/spec/sinatra_modular/db/migrate/activation/20101224223622_add_activation_to_users.rb +0 -17
  143. data/spec/sinatra_modular/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +0 -17
  144. data/spec/sinatra_modular/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +0 -11
  145. data/spec/sinatra_modular/db/migrate/core/20101224223620_create_users.rb +0 -16
  146. data/spec/sinatra_modular/db/migrate/external/20101224223628_create_authentications.rb +0 -14
  147. data/spec/sinatra_modular/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +0 -15
  148. data/spec/sinatra_modular/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +0 -13
  149. data/spec/sinatra_modular/filters.rb +0 -27
  150. data/spec/sinatra_modular/modular.rb +0 -157
  151. data/spec/sinatra_modular/myapp.rb +0 -133
  152. data/spec/sinatra_modular/sorcery_mailer.rb +0 -25
  153. data/spec/sinatra_modular/spec_modular/controller_activity_logging_spec.rb +0 -85
  154. data/spec/sinatra_modular/spec_modular/controller_brute_force_protection_spec.rb +0 -70
  155. data/spec/sinatra_modular/spec_modular/controller_http_basic_auth_spec.rb +0 -53
  156. data/spec/sinatra_modular/spec_modular/controller_oauth2_spec.rb +0 -96
  157. data/spec/sinatra_modular/spec_modular/controller_oauth_spec.rb +0 -100
  158. data/spec/sinatra_modular/spec_modular/controller_remember_me_spec.rb +0 -64
  159. data/spec/sinatra_modular/spec_modular/controller_session_timeout_spec.rb +0 -57
  160. data/spec/sinatra_modular/spec_modular/controller_spec.rb +0 -116
  161. data/spec/sinatra_modular/spec_modular/spec.opts +0 -2
  162. data/spec/sinatra_modular/spec_modular/spec_helper.rb +0 -51
  163. data/spec/sinatra_modular/user.rb +0 -6
  164. data/spec/sinatra_modular/views/test_login.erb +0 -4
@@ -18,6 +18,9 @@ Rails.application.config.sorcery.configure do |config|
18
18
  # and send him there after login, using
19
19
  # 'redirect_back_or_to'.
20
20
 
21
+ # config.cookie_domain = nil # set domain option for cookies
22
+ # Useful for remember_me submodule
23
+
21
24
  # -- session timeout --
22
25
  # config.session_timeout = 3600 # how long in seconds to keep the session alive.
23
26
  # config.session_timeout_from_last_action = false # use the last action as the beginning of
@@ -54,9 +57,6 @@ Rails.application.config.sorcery.configure do |config|
54
57
  # config.github.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=github"
55
58
  # config.github.user_info_mapping = {:email => "name"}
56
59
 
57
- # config.sinatra_cookie_secret = 'ch4ng3M3plz' # key used to sign cookies in Sinatra
58
- # changing it will invalidate all signed cookies!
59
-
60
60
  # --- user config ---
61
61
  config.user_config do |user|
62
62
  # -- core --
@@ -194,6 +194,6 @@ Rails.application.config.sorcery.configure do |config|
194
194
  end
195
195
 
196
196
  # This line must come after the 'user config' block.
197
- config.user_class = "User" # define which model authenticates
197
+ config.user_class = "<%= model_class_name %>" # define which model authenticates
198
198
  # with sorcery.
199
199
  end
@@ -0,0 +1,17 @@
1
+ class SorceryActivityLogging < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :<%= model_class_name.tableize %>, :last_login_at, :datetime, :default => nil
4
+ add_column :<%= model_class_name.tableize %>, :last_logout_at, :datetime, :default => nil
5
+ add_column :<%= model_class_name.tableize %>, :last_activity_at, :datetime, :default => nil
6
+
7
+ add_index :<%= model_class_name.tableize %>, [:last_logout_at, :last_activity_at]
8
+ end
9
+
10
+ def self.down
11
+ remove_index :<%= model_class_name.tableize %>, [:last_logout_at, :last_activity_at]
12
+
13
+ remove_column :<%= model_class_name.tableize %>, :last_activity_at
14
+ remove_column :<%= model_class_name.tableize %>, :last_logout_at
15
+ remove_column :<%= model_class_name.tableize %>, :last_login_at
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ class SorceryBruteForceProtection < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :<%= model_class_name.tableize %>, :failed_logins_count, :integer, :default => 0
4
+ add_column :<%= model_class_name.tableize %>, :lock_expires_at, :datetime, :default => nil
5
+ end
6
+
7
+ def self.down
8
+ remove_column :<%= model_class_name.tableize %>, :lock_expires_at
9
+ remove_column :<%= model_class_name.tableize %>, :failed_logins_count
10
+ end
11
+ end
@@ -1,6 +1,6 @@
1
1
  class SorceryCore < ActiveRecord::Migration
2
2
  def self.up
3
- create_table :users do |t|
3
+ create_table :<%= model_class_name.tableize %> do |t|
4
4
  t.string :username, :null => false # if you use another field as a username, for example email, you can safely remove this field.
5
5
  t.string :email, :default => nil # if you use this field as a username, you might want to make it :null => false.
6
6
  t.string :crypted_password, :default => nil
@@ -11,6 +11,6 @@ class SorceryCore < ActiveRecord::Migration
11
11
  end
12
12
 
13
13
  def self.down
14
- drop_table :users
14
+ drop_table :<%= model_class_name.tableize %>
15
15
  end
16
16
  end
@@ -1,7 +1,7 @@
1
1
  class SorceryExternal < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :authentications do |t|
4
- t.integer :user_id, :null => false
4
+ t.integer :<%= model_class_name.tableize.singularize %>_id, :null => false
5
5
  t.string :provider, :uid, :null => false
6
6
 
7
7
  t.timestamps
@@ -0,0 +1,15 @@
1
+ class SorceryRememberMe < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :<%= model_class_name.tableize %>, :remember_me_token, :string, :default => nil
4
+ add_column :<%= model_class_name.tableize %>, :remember_me_token_expires_at, :datetime, :default => nil
5
+
6
+ add_index :<%= model_class_name.tableize %>, :remember_me_token
7
+ end
8
+
9
+ def self.down
10
+ remove_index :<%= model_class_name.tableize %>, :remember_me_token
11
+
12
+ remove_column :<%= model_class_name.tableize %>, :remember_me_token_expires_at
13
+ remove_column :<%= model_class_name.tableize %>, :remember_me_token
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ class SorceryResetPassword < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :<%= model_class_name.tableize %>, :reset_password_token, :string, :default => nil
4
+ add_column :<%= model_class_name.tableize %>, :reset_password_token_expires_at, :datetime, :default => nil
5
+ add_column :<%= model_class_name.tableize %>, :reset_password_email_sent_at, :datetime, :default => nil
6
+
7
+ add_index :<%= model_class_name.tableize %>, :reset_password_token
8
+ end
9
+
10
+ def self.down
11
+ remove_index :users, :reset_password_token
12
+
13
+ remove_column :<%= model_class_name.tableize %>, :reset_password_email_sent_at
14
+ remove_column :<%= model_class_name.tableize %>, :reset_password_token_expires_at
15
+ remove_column :<%= model_class_name.tableize %>, :reset_password_token
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ class SorceryUserActivation < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :<%= model_class_name.tableize %>, :activation_state, :string, :default => nil
4
+ add_column :<%= model_class_name.tableize %>, :activation_token, :string, :default => nil
5
+ add_column :<%= model_class_name.tableize %>, :activation_token_expires_at, :datetime, :default => nil
6
+
7
+ add_index :<%= model_class_name.tableize %>, :activation_token
8
+ end
9
+
10
+ def self.down
11
+ remove_index :<%= model_class_name.tableize %>, :activation_token
12
+
13
+ remove_column :<%= model_class_name.tableize %>, :activation_token_expires_at
14
+ remove_column :<%= model_class_name.tableize %>, :activation_token
15
+ remove_column :<%= model_class_name.tableize %>, :activation_state
16
+ end
17
+ end
data/lib/sorcery.rb CHANGED
@@ -5,6 +5,7 @@ module Sorcery
5
5
  module Adapters
6
6
  autoload :ActiveRecord, 'sorcery/model/adapters/active_record'
7
7
  autoload :Mongoid, 'sorcery/model/adapters/mongoid'
8
+ autoload :MongoMapper, 'sorcery/model/adapters/mongo_mapper'
8
9
  end
9
10
  module Submodules
10
11
  autoload :UserActivation, 'sorcery/model/submodules/user_activation'
@@ -36,9 +37,6 @@ module Sorcery
36
37
  end
37
38
  end
38
39
  end
39
- module Adapters
40
- autoload :Sinatra, 'sorcery/controller/adapters/sinatra'
41
- end
42
40
  end
43
41
  module CryptoProviders
44
42
  autoload :Common, 'sorcery/crypto_providers/common'
@@ -52,12 +50,9 @@ module Sorcery
52
50
  autoload :TestHelpers, 'sorcery/test_helpers'
53
51
  module TestHelpers
54
52
  autoload :Rails, 'sorcery/test_helpers/rails'
55
- autoload :Sinatra, 'sorcery/test_helpers/sinatra'
56
53
  autoload :Internal, 'sorcery/test_helpers/internal'
57
54
  module Internal
58
55
  autoload :Rails, 'sorcery/test_helpers/internal/rails'
59
- autoload :Sinatra, 'sorcery/test_helpers/internal/sinatra'
60
- autoload :SinatraModular, 'sorcery/test_helpers/internal/sinatra_modular'
61
56
  end
62
57
 
63
58
  end
@@ -77,6 +72,9 @@ module Sorcery
77
72
  end
78
73
  end
79
74
 
75
+ if defined?(MongoMapper)
76
+ MongoMapper::Document.send(:plugin, Sorcery::Model::Adapters::MongoMapper)
77
+ end
78
+
80
79
  require 'sorcery/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
81
- require 'sorcery/sinatra' if defined?(Sinatra)
82
80
  end
@@ -5,7 +5,7 @@ module Sorcery
5
5
  include InstanceMethods
6
6
  Config.submodules.each do |mod|
7
7
  begin
8
- include Submodules.const_get(mod.to_s.split("_").map {|p| p.capitalize}.join(""))
8
+ include Submodules.const_get(mod.to_s.split("_").map {|p| p.capitalize}.join(""))
9
9
  rescue NameError
10
10
  # don't stop on a missing submodule.
11
11
  end
@@ -14,7 +14,7 @@ module Sorcery
14
14
  Config.update!
15
15
  Config.configure!
16
16
  end
17
-
17
+
18
18
  module InstanceMethods
19
19
  # To be used as before_filter.
20
20
  # Will trigger auto-login attempts via the call to logged_in?
@@ -22,10 +22,10 @@ module Sorcery
22
22
  def require_login
23
23
  if !logged_in?
24
24
  session[:return_to_url] = request.url if Config.save_return_to_url
25
- self.send(Config.not_authenticated_action)
25
+ self.send(Config.not_authenticated_action)
26
26
  end
27
27
  end
28
-
28
+
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)
@@ -42,43 +42,44 @@ module Sorcery
42
42
  nil
43
43
  end
44
44
  end
45
-
45
+
46
46
  # Resets the session and runs hooks before and after.
47
47
  def logout
48
48
  if logged_in?
49
49
  before_logout!(current_user)
50
50
  reset_session
51
51
  after_logout!
52
+ @current_user = nil
52
53
  end
53
54
  end
54
-
55
+
55
56
  def logged_in?
56
57
  !!current_user
57
58
  end
58
-
59
+
59
60
  # attempts to auto-login from the sources defined (session, basic_auth, cookie, etc.)
60
61
  # returns the logged in user if found, false if not (using old restful-authentication trick, nil != false).
61
62
  def current_user
62
63
  @current_user ||= login_from_session || login_from_other_sources unless @current_user == false
63
64
  end
64
-
65
+
65
66
  def current_user=(user)
66
67
  @current_user = user
67
68
  end
68
-
69
- # used when a user tries to access a page while logged out, is asked to login,
69
+
70
+ # used when a user tries to access a page while logged out, is asked to login,
70
71
  # and we want to return him back to the page he originally wanted.
71
72
  def redirect_back_or_to(url, flash_hash = {})
72
73
  redirect_to(session[:return_to_url] || url, :flash => flash_hash)
73
74
  end
74
-
75
+
75
76
  # The default action for denying non-authenticated users.
76
77
  # You can override this method in your controllers,
77
78
  # or provide a different method in the configuration.
78
79
  def not_authenticated
79
80
  redirect_to root_path
80
81
  end
81
-
82
+
82
83
  # login a user instance
83
84
  #
84
85
  # @param [<User-Model>] user the user instance.
@@ -87,16 +88,16 @@ module Sorcery
87
88
  session[:user_id] = user.id
88
89
  @current_user = user
89
90
  end
90
-
91
+
91
92
  # Overwrite Rails' handle unverified request
92
93
  def handle_unverified_request
93
94
  cookies[:remember_me_token] = nil
94
95
  @current_user = nil
95
96
  super # call the default behaviour which resets the session
96
97
  end
97
-
98
+
98
99
  protected
99
-
100
+
100
101
  # Tries all available sources (methods) until one doesn't return false.
101
102
  def login_from_other_sources
102
103
  result = nil
@@ -105,51 +106,51 @@ module Sorcery
105
106
  end
106
107
  result || false
107
108
  end
108
-
109
+
109
110
  def login_from_session
110
111
  @current_user = (user_class.find_by_id(session[:user_id]) if session[:user_id]) || false
111
112
  end
112
-
113
+
113
114
  def after_login!(user, credentials)
114
115
  Config.after_login.each {|c| self.send(c, user, credentials)}
115
116
  end
116
-
117
+
117
118
  def after_failed_login!(credentials)
118
119
  Config.after_failed_login.each {|c| self.send(c, credentials)}
119
120
  end
120
-
121
+
121
122
  def before_logout!(user)
122
123
  Config.before_logout.each {|c| self.send(c, user)}
123
124
  end
124
-
125
+
125
126
  def after_logout!
126
127
  Config.after_logout.each {|c| self.send(c)}
127
128
  end
128
-
129
+
129
130
  def user_class
130
131
  @user_class ||= Config.user_class.to_s.constantize
131
132
  end
132
-
133
+
133
134
  end
134
-
135
+
135
136
  module Config
136
137
  class << self
137
138
  attr_accessor :submodules,
138
- :user_class, # what class to use as the user class.
139
+ :user_class, # what class to use as the user class.
139
140
  :not_authenticated_action, # what controller action to call for non-authenticated users.
140
-
141
+
141
142
  :save_return_to_url, # when a non logged in user tries to enter a page that requires
142
- # login, save the URL he wanted to reach,
143
+ # login, save the URL he wanted to reach,
143
144
  # and send him there after login.
144
-
145
- :sinatra_cookie_secret, # used to sign cookies in Sinatra.
146
-
145
+
146
+ :cookie_domain, # set domain option for cookies
147
+
147
148
  :login_sources,
148
149
  :after_login,
149
150
  :after_failed_login,
150
151
  :before_logout,
151
- :after_logout
152
-
152
+ :after_logout
153
+
153
154
  def init!
154
155
  @defaults = {
155
156
  :@user_class => nil,
@@ -161,31 +162,31 @@ module Sorcery
161
162
  :@before_logout => [],
162
163
  :@after_logout => [],
163
164
  :@save_return_to_url => true,
164
- :@sinatra_cookie_secret => 'ch4ng3M3plz'
165
+ :@cookie_domain => nil
165
166
  }
166
167
  end
167
-
168
+
168
169
  # Resets all configuration options to their default values.
169
170
  def reset!
170
171
  @defaults.each do |k,v|
171
172
  instance_variable_set(k,v)
172
- end
173
+ end
173
174
  end
174
-
175
+
175
176
  def update!
176
177
  @defaults.each do |k,v|
177
178
  instance_variable_set(k,v) if !instance_variable_defined?(k)
178
179
  end
179
180
  end
180
-
181
+
181
182
  def user_config(&blk)
182
183
  block_given? ? @user_config = blk : @user_config
183
184
  end
184
-
185
+
185
186
  def configure(&blk)
186
187
  @configure_blk = blk
187
188
  end
188
-
189
+
189
190
  def configure!
190
191
  @configure_blk.call(self) if @configure_blk
191
192
  end
@@ -194,4 +195,4 @@ module Sorcery
194
195
  reset!
195
196
  end
196
197
  end
197
- end
198
+ end
@@ -21,15 +21,15 @@ module Sorcery
21
21
  attr_accessor :register_last_activity_time
22
22
 
23
23
  def merge_activity_logging_defaults!
24
- @defaults.merge!(:@register_login_time => true,
25
- :@register_logout_time => true,
24
+ @defaults.merge!(:@register_login_time => true,
25
+ :@register_logout_time => true,
26
26
  :@register_last_activity_time => true)
27
27
  end
28
28
  end
29
29
  merge_activity_logging_defaults!
30
30
  end
31
- Config.after_login << :register_login_time_to_db
32
- Config.before_logout << :register_logout_time_to_db
31
+ Config.after_login << :register_login_time_to_db
32
+ Config.before_logout << :register_logout_time_to_db
33
33
  base.after_filter :register_last_activity_time_to_db
34
34
  end
35
35
 
@@ -52,16 +52,14 @@ module Sorcery
52
52
  # This runs as a hook just after a successful login.
53
53
  def register_login_time_to_db(user, credentials)
54
54
  return unless Config.register_login_time
55
- user.send(:"#{user.sorcery_config.last_login_at_attribute_name}=", Time.now.utc.to_s(:db))
56
- user.save!(:validate => false)
55
+ user.update_attributes!(user.sorcery_config.last_login_at_attribute_name => Time.now.utc)
57
56
  end
58
57
 
59
58
  # registers last logout time on every logout.
60
59
  # This runs as a hook just before a logout.
61
60
  def register_logout_time_to_db(user)
62
61
  return unless Config.register_logout_time
63
- user.send(:"#{user.sorcery_config.last_logout_at_attribute_name}=", Time.now.utc.to_s(:db))
64
- user.save!(:validate => false)
62
+ user.update_attributes!(user.sorcery_config.last_logout_at_attribute_name => Time.now.utc)
65
63
  end
66
64
 
67
65
  # Updates last activity time on every request.
@@ -69,8 +67,7 @@ module Sorcery
69
67
  def register_last_activity_time_to_db
70
68
  return unless Config.register_last_activity_time
71
69
  return unless logged_in?
72
- current_user.send(:"#{current_user.sorcery_config.last_activity_at_attribute_name}=", Time.now.utc.to_s(:db))
73
- current_user.save!(:validate => false)
70
+ current_user.update_attributes!(current_user.sorcery_config.last_activity_at_attribute_name => Time.now.utc)
74
71
  end
75
72
  end
76
73
  end
@@ -8,19 +8,34 @@ module Sorcery
8
8
  def oauth_version
9
9
  "2.0"
10
10
  end
11
-
11
+
12
12
  def authorize_url(options = {})
13
13
  defaults = {
14
- :site => @site,
15
- :ssl => { :ca_file => Config.ca_file }
14
+ :site => @site,
15
+ :ssl => { :ca_file => Config.ca_file }
16
16
  }
17
- client = ::OAuth2::Client.new(@key, @secret, defaults.merge!(options))
18
- client.authorize_url(:redirect_uri => @callback_url, :scope => @scope)
17
+ client = ::OAuth2::Client.new(
18
+ @key,
19
+ @secret,
20
+ defaults.merge!(options)
21
+ )
22
+ client.web_server.authorize_url(
23
+ :redirect_uri => @callback_url,
24
+ :scope => @scope
25
+ )
19
26
  end
20
-
27
+
21
28
  def get_access_token(args)
22
- client = ::OAuth2::Client.new(@key, @secret, :site => @site, :ssl => { :ca_file => Config.ca_file })
23
- client.get_token(args[:code], :redirect_uri => @callback_url)
29
+ client = ::OAuth2::Client.new(
30
+ @key,
31
+ @secret,
32
+ :site => @site,
33
+ :ssl => { :ca_file => Config.ca_file }
34
+ )
35
+ client.web_server.get_access_token(
36
+ args[:code],
37
+ :redirect_uri => @callback_url
38
+ )
24
39
  end
25
40
  end
26
41
  end