devise 3.5.2 → 4.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +259 -1086
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +254 -67
  5. data/app/controllers/devise/confirmations_controller.rb +3 -1
  6. data/app/controllers/devise/omniauth_callbacks_controller.rb +8 -6
  7. data/app/controllers/devise/passwords_controller.rb +8 -4
  8. data/app/controllers/devise/registrations_controller.rb +39 -18
  9. data/app/controllers/devise/sessions_controller.rb +9 -7
  10. data/app/controllers/devise/unlocks_controller.rb +4 -2
  11. data/app/controllers/devise_controller.rb +23 -10
  12. data/app/helpers/devise_helper.rb +12 -19
  13. data/app/mailers/devise/mailer.rb +10 -0
  14. data/app/views/devise/confirmations/new.html.erb +2 -2
  15. data/app/views/devise/mailer/email_changed.html.erb +7 -0
  16. data/app/views/devise/mailer/password_change.html.erb +3 -0
  17. data/app/views/devise/passwords/edit.html.erb +3 -3
  18. data/app/views/devise/passwords/new.html.erb +2 -2
  19. data/app/views/devise/registrations/edit.html.erb +9 -5
  20. data/app/views/devise/registrations/new.html.erb +4 -4
  21. data/app/views/devise/sessions/new.html.erb +4 -4
  22. data/app/views/devise/shared/_error_messages.html.erb +15 -0
  23. data/app/views/devise/shared/_links.html.erb +8 -8
  24. data/app/views/devise/unlocks/new.html.erb +2 -2
  25. data/config/locales/en.yml +6 -1
  26. data/lib/devise/controllers/helpers.rb +35 -26
  27. data/lib/devise/controllers/rememberable.rb +11 -2
  28. data/lib/devise/controllers/scoped_views.rb +2 -0
  29. data/lib/devise/controllers/sign_in_out.rb +34 -11
  30. data/lib/devise/controllers/store_location.rb +25 -7
  31. data/lib/devise/controllers/url_helpers.rb +2 -0
  32. data/lib/devise/delegator.rb +2 -0
  33. data/lib/devise/encryptor.rb +6 -4
  34. data/lib/devise/failure_app.rb +84 -32
  35. data/lib/devise/hooks/activatable.rb +2 -0
  36. data/lib/devise/hooks/csrf_cleaner.rb +2 -0
  37. data/lib/devise/hooks/forgetable.rb +2 -0
  38. data/lib/devise/hooks/lockable.rb +6 -1
  39. data/lib/devise/hooks/proxy.rb +3 -1
  40. data/lib/devise/hooks/rememberable.rb +2 -0
  41. data/lib/devise/hooks/timeoutable.rb +7 -3
  42. data/lib/devise/hooks/trackable.rb +2 -0
  43. data/lib/devise/mailers/helpers.rb +7 -4
  44. data/lib/devise/mapping.rb +2 -0
  45. data/lib/devise/models/authenticatable.rb +51 -26
  46. data/lib/devise/models/confirmable.rb +89 -27
  47. data/lib/devise/models/database_authenticatable.rb +97 -20
  48. data/lib/devise/models/lockable.rb +15 -5
  49. data/lib/devise/models/omniauthable.rb +2 -0
  50. data/lib/devise/models/recoverable.rb +32 -24
  51. data/lib/devise/models/registerable.rb +4 -0
  52. data/lib/devise/models/rememberable.rb +42 -26
  53. data/lib/devise/models/timeoutable.rb +2 -6
  54. data/lib/devise/models/trackable.rb +15 -1
  55. data/lib/devise/models/validatable.rb +10 -3
  56. data/lib/devise/models.rb +3 -1
  57. data/lib/devise/modules.rb +2 -0
  58. data/lib/devise/omniauth/config.rb +2 -0
  59. data/lib/devise/omniauth/url_helpers.rb +14 -5
  60. data/lib/devise/omniauth.rb +2 -0
  61. data/lib/devise/orm/active_record.rb +5 -1
  62. data/lib/devise/orm/mongoid.rb +6 -2
  63. data/lib/devise/parameter_filter.rb +4 -0
  64. data/lib/devise/parameter_sanitizer.rb +139 -65
  65. data/lib/devise/rails/routes.rb +59 -34
  66. data/lib/devise/rails/warden_compat.rb +3 -10
  67. data/lib/devise/rails.rb +7 -16
  68. data/lib/devise/secret_key_finder.rb +27 -0
  69. data/lib/devise/strategies/authenticatable.rb +4 -2
  70. data/lib/devise/strategies/base.rb +2 -0
  71. data/lib/devise/strategies/database_authenticatable.rb +11 -4
  72. data/lib/devise/strategies/rememberable.rb +5 -6
  73. data/lib/devise/test/controller_helpers.rb +165 -0
  74. data/lib/devise/test/integration_helpers.rb +63 -0
  75. data/lib/devise/test_helpers.rb +7 -124
  76. data/lib/devise/time_inflector.rb +2 -0
  77. data/lib/devise/token_generator.rb +3 -41
  78. data/lib/devise/version.rb +3 -1
  79. data/lib/devise.rb +72 -42
  80. data/lib/generators/active_record/devise_generator.rb +29 -10
  81. data/lib/generators/active_record/templates/migration.rb +4 -2
  82. data/lib/generators/active_record/templates/migration_existing.rb +4 -2
  83. data/lib/generators/devise/controllers_generator.rb +3 -1
  84. data/lib/generators/devise/devise_generator.rb +4 -2
  85. data/lib/generators/devise/install_generator.rb +17 -0
  86. data/lib/generators/devise/orm_helpers.rb +10 -21
  87. data/lib/generators/devise/views_generator.rb +21 -11
  88. data/lib/generators/mongoid/devise_generator.rb +7 -5
  89. data/lib/generators/templates/README +1 -8
  90. data/lib/generators/templates/controllers/confirmations_controller.rb +2 -0
  91. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +2 -0
  92. data/lib/generators/templates/controllers/passwords_controller.rb +2 -0
  93. data/lib/generators/templates/controllers/registrations_controller.rb +6 -4
  94. data/lib/generators/templates/controllers/sessions_controller.rb +4 -2
  95. data/lib/generators/templates/controllers/unlocks_controller.rb +2 -0
  96. data/lib/generators/templates/devise.rb +52 -19
  97. data/lib/generators/templates/markerb/confirmation_instructions.markerb +1 -1
  98. data/lib/generators/templates/markerb/email_changed.markerb +7 -0
  99. data/lib/generators/templates/markerb/password_change.markerb +3 -0
  100. data/lib/generators/templates/markerb/reset_password_instructions.markerb +1 -1
  101. data/lib/generators/templates/markerb/unlock_instructions.markerb +1 -1
  102. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +5 -1
  103. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +10 -2
  104. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +4 -1
  105. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +11 -3
  106. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +11 -3
  107. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +7 -2
  108. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +4 -1
  109. metadata +27 -313
  110. data/.gitignore +0 -10
  111. data/.travis.yml +0 -45
  112. data/.yardopts +0 -9
  113. data/CONTRIBUTING.md +0 -14
  114. data/Gemfile +0 -29
  115. data/Gemfile.lock +0 -183
  116. data/Rakefile +0 -36
  117. data/devise.gemspec +0 -27
  118. data/devise.png +0 -0
  119. data/gemfiles/Gemfile.rails-3.2-stable +0 -29
  120. data/gemfiles/Gemfile.rails-3.2-stable.lock +0 -169
  121. data/gemfiles/Gemfile.rails-4.0-stable +0 -29
  122. data/gemfiles/Gemfile.rails-4.0-stable.lock +0 -163
  123. data/gemfiles/Gemfile.rails-4.1-stable +0 -29
  124. data/gemfiles/Gemfile.rails-4.1-stable.lock +0 -169
  125. data/gemfiles/Gemfile.rails-4.2-stable +0 -29
  126. data/gemfiles/Gemfile.rails-4.2-stable.lock +0 -191
  127. data/script/cached-bundle +0 -49
  128. data/script/s3-put +0 -71
  129. data/test/controllers/custom_registrations_controller_test.rb +0 -40
  130. data/test/controllers/custom_strategy_test.rb +0 -62
  131. data/test/controllers/helpers_test.rb +0 -316
  132. data/test/controllers/inherited_controller_i18n_messages_test.rb +0 -51
  133. data/test/controllers/internal_helpers_test.rb +0 -129
  134. data/test/controllers/load_hooks_controller_test.rb +0 -19
  135. data/test/controllers/passwords_controller_test.rb +0 -31
  136. data/test/controllers/sessions_controller_test.rb +0 -103
  137. data/test/controllers/url_helpers_test.rb +0 -65
  138. data/test/delegator_test.rb +0 -19
  139. data/test/devise_test.rb +0 -107
  140. data/test/failure_app_test.rb +0 -298
  141. data/test/generators/active_record_generator_test.rb +0 -109
  142. data/test/generators/controllers_generator_test.rb +0 -48
  143. data/test/generators/devise_generator_test.rb +0 -39
  144. data/test/generators/install_generator_test.rb +0 -13
  145. data/test/generators/mongoid_generator_test.rb +0 -23
  146. data/test/generators/views_generator_test.rb +0 -96
  147. data/test/helpers/devise_helper_test.rb +0 -49
  148. data/test/integration/authenticatable_test.rb +0 -729
  149. data/test/integration/confirmable_test.rb +0 -324
  150. data/test/integration/database_authenticatable_test.rb +0 -95
  151. data/test/integration/http_authenticatable_test.rb +0 -105
  152. data/test/integration/lockable_test.rb +0 -239
  153. data/test/integration/omniauthable_test.rb +0 -133
  154. data/test/integration/recoverable_test.rb +0 -347
  155. data/test/integration/registerable_test.rb +0 -359
  156. data/test/integration/rememberable_test.rb +0 -176
  157. data/test/integration/timeoutable_test.rb +0 -172
  158. data/test/integration/trackable_test.rb +0 -92
  159. data/test/mailers/confirmation_instructions_test.rb +0 -115
  160. data/test/mailers/reset_password_instructions_test.rb +0 -96
  161. data/test/mailers/unlock_instructions_test.rb +0 -91
  162. data/test/mapping_test.rb +0 -134
  163. data/test/models/authenticatable_test.rb +0 -23
  164. data/test/models/confirmable_test.rb +0 -479
  165. data/test/models/database_authenticatable_test.rb +0 -249
  166. data/test/models/lockable_test.rb +0 -328
  167. data/test/models/omniauthable_test.rb +0 -7
  168. data/test/models/recoverable_test.rb +0 -228
  169. data/test/models/registerable_test.rb +0 -7
  170. data/test/models/rememberable_test.rb +0 -204
  171. data/test/models/serializable_test.rb +0 -49
  172. data/test/models/timeoutable_test.rb +0 -51
  173. data/test/models/trackable_test.rb +0 -41
  174. data/test/models/validatable_test.rb +0 -127
  175. data/test/models_test.rb +0 -144
  176. data/test/omniauth/config_test.rb +0 -57
  177. data/test/omniauth/url_helpers_test.rb +0 -54
  178. data/test/orm/active_record.rb +0 -10
  179. data/test/orm/mongoid.rb +0 -13
  180. data/test/parameter_sanitizer_test.rb +0 -81
  181. data/test/rails_app/Rakefile +0 -6
  182. data/test/rails_app/app/active_record/admin.rb +0 -6
  183. data/test/rails_app/app/active_record/shim.rb +0 -2
  184. data/test/rails_app/app/active_record/user.rb +0 -6
  185. data/test/rails_app/app/active_record/user_on_engine.rb +0 -7
  186. data/test/rails_app/app/active_record/user_on_main_app.rb +0 -7
  187. data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -6
  188. data/test/rails_app/app/controllers/admins_controller.rb +0 -6
  189. data/test/rails_app/app/controllers/application_controller.rb +0 -12
  190. data/test/rails_app/app/controllers/application_with_fake_engine.rb +0 -30
  191. data/test/rails_app/app/controllers/custom/registrations_controller.rb +0 -31
  192. data/test/rails_app/app/controllers/home_controller.rb +0 -25
  193. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -2
  194. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -2
  195. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -14
  196. data/test/rails_app/app/controllers/users_controller.rb +0 -31
  197. data/test/rails_app/app/helpers/application_helper.rb +0 -3
  198. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +0 -3
  199. data/test/rails_app/app/mailers/users/mailer.rb +0 -3
  200. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +0 -4
  201. data/test/rails_app/app/mongoid/admin.rb +0 -29
  202. data/test/rails_app/app/mongoid/shim.rb +0 -23
  203. data/test/rails_app/app/mongoid/user.rb +0 -39
  204. data/test/rails_app/app/mongoid/user_on_engine.rb +0 -39
  205. data/test/rails_app/app/mongoid/user_on_main_app.rb +0 -39
  206. data/test/rails_app/app/views/admins/index.html.erb +0 -1
  207. data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
  208. data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
  209. data/test/rails_app/app/views/home/index.html.erb +0 -1
  210. data/test/rails_app/app/views/home/join.html.erb +0 -1
  211. data/test/rails_app/app/views/home/private.html.erb +0 -1
  212. data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
  213. data/test/rails_app/app/views/layouts/application.html.erb +0 -24
  214. data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
  215. data/test/rails_app/app/views/users/index.html.erb +0 -1
  216. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
  217. data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
  218. data/test/rails_app/bin/bundle +0 -3
  219. data/test/rails_app/bin/rails +0 -4
  220. data/test/rails_app/bin/rake +0 -4
  221. data/test/rails_app/config/application.rb +0 -40
  222. data/test/rails_app/config/boot.rb +0 -14
  223. data/test/rails_app/config/database.yml +0 -18
  224. data/test/rails_app/config/environment.rb +0 -5
  225. data/test/rails_app/config/environments/development.rb +0 -30
  226. data/test/rails_app/config/environments/production.rb +0 -84
  227. data/test/rails_app/config/environments/test.rb +0 -41
  228. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  229. data/test/rails_app/config/initializers/devise.rb +0 -180
  230. data/test/rails_app/config/initializers/inflections.rb +0 -2
  231. data/test/rails_app/config/initializers/secret_token.rb +0 -8
  232. data/test/rails_app/config/initializers/session_store.rb +0 -1
  233. data/test/rails_app/config/routes.rb +0 -120
  234. data/test/rails_app/config.ru +0 -4
  235. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -71
  236. data/test/rails_app/db/schema.rb +0 -55
  237. data/test/rails_app/lib/shared_admin.rb +0 -17
  238. data/test/rails_app/lib/shared_user.rb +0 -29
  239. data/test/rails_app/lib/shared_user_without_omniauth.rb +0 -13
  240. data/test/rails_app/public/404.html +0 -26
  241. data/test/rails_app/public/422.html +0 -26
  242. data/test/rails_app/public/500.html +0 -26
  243. data/test/rails_app/public/favicon.ico +0 -0
  244. data/test/rails_test.rb +0 -9
  245. data/test/routes_test.rb +0 -264
  246. data/test/support/action_controller/record_identifier.rb +0 -10
  247. data/test/support/assertions.rb +0 -39
  248. data/test/support/helpers.rb +0 -73
  249. data/test/support/integration.rb +0 -92
  250. data/test/support/locale/en.yml +0 -8
  251. data/test/support/mongoid.yml +0 -6
  252. data/test/support/webrat/integrations/rails.rb +0 -24
  253. data/test/test_helper.rb +0 -34
  254. data/test/test_helpers_test.rb +0 -178
  255. data/test/test_models.rb +0 -33
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Devise
2
4
  module Models
3
5
 
@@ -16,10 +18,6 @@ module Devise
16
18
  # # resets the user password and save the record, true if valid passwords are given, otherwise false
17
19
  # User.find(1).reset_password('password123', 'password123')
18
20
  #
19
- # # only resets the user password, without saving the record
20
- # user = User.find(1)
21
- # user.reset_password('password123', 'password123')
22
- #
23
21
  # # creates a new token and send it with instructions about how to reset the password
24
22
  # User.find(1).send_reset_password_instructions
25
23
  #
@@ -31,30 +29,20 @@ module Devise
31
29
  end
32
30
 
33
31
  included do
34
- before_save do
35
- if email_changed? || encrypted_password_changed?
36
- clear_reset_password_token
37
- end
38
- end
32
+ before_update :clear_reset_password_token, if: :clear_reset_password_token?
39
33
  end
40
34
 
41
35
  # Update password saving the record and clearing token. Returns true if
42
36
  # the passwords are valid and the record was saved, false otherwise.
43
37
  def reset_password(new_password, new_password_confirmation)
44
- self.password = new_password
45
- self.password_confirmation = new_password_confirmation
46
-
47
- if respond_to?(:after_password_reset) && valid?
48
- ActiveSupport::Deprecation.warn "after_password_reset is deprecated"
49
- after_password_reset
38
+ if new_password.present?
39
+ self.password = new_password
40
+ self.password_confirmation = new_password_confirmation
41
+ save
42
+ else
43
+ errors.add(:password, :blank)
44
+ false
50
45
  end
51
-
52
- save
53
- end
54
-
55
- def reset_password!(new_password, new_password_confirmation)
56
- ActiveSupport::Deprecation.warn "reset_password! is deprecated in favor of reset_password"
57
- reset_password(new_password, new_password_confirmation)
58
46
  end
59
47
 
60
48
  # Resets reset password token and send reset password instructions by email.
@@ -87,7 +75,7 @@ module Devise
87
75
  # reset_password_period_valid? # will always return false
88
76
  #
89
77
  def reset_password_period_valid?
90
- reset_password_sent_at && reset_password_sent_at.utc >= self.class.reset_password_within.ago
78
+ reset_password_sent_at && reset_password_sent_at.utc >= self.class.reset_password_within.ago.utc
91
79
  end
92
80
 
93
81
  protected
@@ -103,7 +91,7 @@ module Devise
103
91
 
104
92
  self.reset_password_token = enc
105
93
  self.reset_password_sent_at = Time.now.utc
106
- self.save(validate: false)
94
+ save(validate: false)
107
95
  raw
108
96
  end
109
97
 
@@ -111,6 +99,26 @@ module Devise
111
99
  send_devise_notification(:reset_password_instructions, token, {})
112
100
  end
113
101
 
102
+ if Devise.activerecord51?
103
+ def clear_reset_password_token?
104
+ encrypted_password_changed = respond_to?(:will_save_change_to_encrypted_password?) && will_save_change_to_encrypted_password?
105
+ authentication_keys_changed = self.class.authentication_keys.any? do |attribute|
106
+ respond_to?("will_save_change_to_#{attribute}?") && send("will_save_change_to_#{attribute}?")
107
+ end
108
+
109
+ authentication_keys_changed || encrypted_password_changed
110
+ end
111
+ else
112
+ def clear_reset_password_token?
113
+ encrypted_password_changed = respond_to?(:encrypted_password_changed?) && encrypted_password_changed?
114
+ authentication_keys_changed = self.class.authentication_keys.any? do |attribute|
115
+ respond_to?("#{attribute}_changed?") && send("#{attribute}_changed?")
116
+ end
117
+
118
+ authentication_keys_changed || encrypted_password_changed
119
+ end
120
+ end
121
+
114
122
  module ClassMethods
115
123
  # Attempt to find a user by password reset token. If a user is found, return it
116
124
  # If a user is not found, return nil
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Devise
2
4
  module Models
3
5
  # Registerable is responsible for everything related to registering a new
@@ -19,6 +21,8 @@ module Devise
19
21
  def new_with_session(params, session)
20
22
  new(params)
21
23
  end
24
+
25
+ Devise::Models.config(self, :sign_in_after_change_password)
22
26
  end
23
27
  end
24
28
  end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'devise/strategies/rememberable'
2
4
  require 'devise/hooks/rememberable'
3
5
  require 'devise/hooks/forgetable'
4
6
 
5
7
  module Devise
6
8
  module Models
7
- # Rememberable manages generating and clearing token for remember the user
9
+ # Rememberable manages generating and clearing token for remembering the user
8
10
  # from a saved cookie. Rememberable also has utility methods for dealing
9
11
  # with serializing the user into the cookie and back from the cookie, trying
10
12
  # to lookup the record based on the saved information.
@@ -39,17 +41,15 @@ module Devise
39
41
  module Rememberable
40
42
  extend ActiveSupport::Concern
41
43
 
42
- attr_accessor :remember_me, :extend_remember_period
44
+ attr_accessor :remember_me
43
45
 
44
46
  def self.required_fields(klass)
45
47
  [:remember_created_at]
46
48
  end
47
49
 
48
- # Generate a new remember token and save the record without validations
49
- # if remember expired (token is no longer valid) or extend_remember_period is true
50
- def remember_me!(extend_period=false)
51
- self.remember_token = self.class.remember_token if generate_remember_token?
52
- self.remember_created_at = Time.now.utc if generate_remember_timestamp?(extend_period)
50
+ def remember_me!
51
+ self.remember_token ||= self.class.remember_token if respond_to?(:remember_token)
52
+ self.remember_created_at ||= Time.now.utc
53
53
  save(validate: false) if self.changed?
54
54
  end
55
55
 
@@ -57,19 +57,17 @@ module Devise
57
57
  # it exists), and save the record without validations.
58
58
  def forget_me!
59
59
  return unless persisted?
60
- self.remember_token = nil if respond_to?(:remember_token=)
60
+ self.remember_token = nil if respond_to?(:remember_token)
61
61
  self.remember_created_at = nil if self.class.expire_all_remember_me_on_sign_out
62
62
  save(validate: false)
63
63
  end
64
64
 
65
- # Remember token should be expired if expiration time not overpass now.
66
- def remember_expired?
67
- remember_created_at.nil? || (remember_expires_at <= Time.now.utc)
65
+ def remember_expires_at
66
+ self.class.remember_for.from_now
68
67
  end
69
68
 
70
- # Remember token expires at created time + remember_for configuration
71
- def remember_expires_at
72
- remember_created_at + self.class.remember_for
69
+ def extend_remember_period
70
+ self.class.extend_remember_period
73
71
  end
74
72
 
75
73
  def rememberable_value
@@ -78,7 +76,7 @@ module Devise
78
76
  elsif respond_to?(:authenticatable_salt) && (salt = authenticatable_salt.presence)
79
77
  salt
80
78
  else
81
- raise "authenticable_salt returned nil for the #{self.class.name} model. " \
79
+ raise "authenticatable_salt returned nil for the #{self.class.name} model. " \
82
80
  "In order to use rememberable, you must ensure a password is always set " \
83
81
  "or have a remember_token column in your model or implement your own " \
84
82
  "rememberable_value in the model with custom logic."
@@ -102,29 +100,47 @@ module Devise
102
100
  def after_remembered
103
101
  end
104
102
 
105
- protected
103
+ def remember_me?(token, generated_at)
104
+ # TODO: Normalize the JSON type coercion along with the Timeoutable hook
105
+ # in a single place https://github.com/plataformatec/devise/blob/ffe9d6d406e79108cf32a2c6a1d0b3828849c40b/lib/devise/hooks/timeoutable.rb#L14-L18
106
+ if generated_at.is_a?(String)
107
+ generated_at = time_from_json(generated_at)
108
+ end
106
109
 
107
- def generate_remember_token? #:nodoc:
108
- respond_to?(:remember_token) && remember_expired?
110
+ # The token is only valid if:
111
+ # 1. we have a date
112
+ # 2. the current time does not pass the expiry period
113
+ # 3. the record has a remember_created_at date
114
+ # 4. the token date is bigger than the remember_created_at
115
+ # 5. the token matches
116
+ generated_at.is_a?(Time) &&
117
+ (self.class.remember_for.ago < generated_at) &&
118
+ (generated_at > (remember_created_at || Time.now).utc) &&
119
+ Devise.secure_compare(rememberable_value, token)
109
120
  end
110
121
 
111
- # Generate a timestamp if extend_remember_period is true, if no remember_token
112
- # exists, or if an existing remember token has expired.
113
- def generate_remember_timestamp?(extend_period) #:nodoc:
114
- extend_period || remember_expired?
122
+ private
123
+
124
+ def time_from_json(value)
125
+ if value =~ /\A\d+\.\d+\Z/
126
+ Time.at(value.to_f)
127
+ else
128
+ Time.parse(value) rescue nil
129
+ end
115
130
  end
116
131
 
117
132
  module ClassMethods
118
133
  # Create the cookie key using the record id and remember_token
119
134
  def serialize_into_cookie(record)
120
- [record.to_key, record.rememberable_value]
135
+ [record.to_key, record.rememberable_value, Time.now.utc.to_f.to_s]
121
136
  end
122
137
 
123
138
  # Recreate the user based on the stored cookie
124
- def serialize_from_cookie(id, remember_token)
139
+ def serialize_from_cookie(*args)
140
+ id, token, generated_at = *args
141
+
125
142
  record = to_adapter.get(id)
126
- record if record && !record.remember_expired? &&
127
- Devise.secure_compare(record.rememberable_value, remember_token)
143
+ record if record && record.remember_me?(token, generated_at)
128
144
  end
129
145
 
130
146
  # Generate a token checking if one does not already exist in the database.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'devise/hooks/timeoutable'
2
4
 
3
5
  module Devise
@@ -26,7 +28,6 @@ module Devise
26
28
 
27
29
  # Checks whether the user session has expired based on configured time.
28
30
  def timedout?(last_access)
29
- return false if remember_exists_and_not_expired?
30
31
  !timeout_in.nil? && last_access && last_access <= timeout_in.ago
31
32
  end
32
33
 
@@ -36,11 +37,6 @@ module Devise
36
37
 
37
38
  private
38
39
 
39
- def remember_exists_and_not_expired?
40
- return false unless respond_to?(:remember_created_at) && respond_to?(:remember_expired?)
41
- remember_created_at && !remember_expired?
42
- end
43
-
44
40
  module ClassMethods
45
41
  Devise::Models.config(self, :timeout_in)
46
42
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'devise/hooks/trackable'
2
4
 
3
5
  module Devise
@@ -20,7 +22,7 @@ module Devise
20
22
  self.last_sign_in_at = old_current || new_current
21
23
  self.current_sign_in_at = new_current
22
24
 
23
- old_current, new_current = self.current_sign_in_ip, request.remote_ip
25
+ old_current, new_current = self.current_sign_in_ip, extract_ip_from(request)
24
26
  self.last_sign_in_ip = old_current || new_current
25
27
  self.current_sign_in_ip = new_current
26
28
 
@@ -29,9 +31,21 @@ module Devise
29
31
  end
30
32
 
31
33
  def update_tracked_fields!(request)
34
+ # We have to check if the user is already persisted before running
35
+ # `save` here because invalid users can be saved if we don't.
36
+ # See https://github.com/plataformatec/devise/issues/4673 for more details.
37
+ return if new_record?
38
+
32
39
  update_tracked_fields(request)
33
40
  save(validate: false)
34
41
  end
42
+
43
+ protected
44
+
45
+ def extract_ip_from(request)
46
+ request.remote_ip
47
+ end
48
+
35
49
  end
36
50
  end
37
51
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Devise
2
4
  module Models
3
5
  # Validatable creates all needed validations for a user email and password.
@@ -10,7 +12,7 @@ module Devise
10
12
  # Validatable adds the following options to devise_for:
11
13
  #
12
14
  # * +email_regexp+: the regular expression used to validate e-mails;
13
- # * +password_length+: a range expressing password length. Defaults to 8..72.
15
+ # * +password_length+: a range expressing password length. Defaults to 6..128.
14
16
  #
15
17
  module Validatable
16
18
  # All validations used by this module.
@@ -27,8 +29,13 @@ module Devise
27
29
 
28
30
  base.class_eval do
29
31
  validates_presence_of :email, if: :email_required?
30
- validates_uniqueness_of :email, allow_blank: true, if: :email_changed?
31
- validates_format_of :email, with: email_regexp, allow_blank: true, if: :email_changed?
32
+ if Devise.activerecord51?
33
+ validates_uniqueness_of :email, allow_blank: true, case_sensitive: true, if: :will_save_change_to_email?
34
+ validates_format_of :email, with: email_regexp, allow_blank: true, if: :will_save_change_to_email?
35
+ else
36
+ validates_uniqueness_of :email, allow_blank: true, if: :email_changed?
37
+ validates_format_of :email, with: email_regexp, allow_blank: true, if: :email_changed?
38
+ end
32
39
 
33
40
  validates_presence_of :password, if: :password_required?
34
41
  validates_confirmation_of :password, if: :password_required?
data/lib/devise/models.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Devise
2
4
  module Models
3
5
  class MissingAttribute < StandardError
@@ -12,7 +14,7 @@ module Devise
12
14
 
13
15
  # Creates configuration values for Devise and for the given module.
14
16
  #
15
- # Devise::Models.config(Devise::Authenticatable, :stretches, 10)
17
+ # Devise::Models.config(Devise::Models::DatabaseAuthenticatable, :stretches)
16
18
  #
17
19
  # The line above creates:
18
20
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/object/with_options'
2
4
 
3
5
  Devise.with_options model: true do |d|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Devise
2
4
  module OmniAuth
3
5
  class StrategyNotFound < NameError
@@ -1,17 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Devise
2
4
  module OmniAuth
3
5
  module UrlHelpers
4
- def self.define_helpers(mapping)
6
+ def omniauth_authorize_path(resource_or_scope, provider, *args)
7
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
8
+ _devise_route_context.send("#{scope}_#{provider}_omniauth_authorize_path", *args)
9
+ end
10
+
11
+ def omniauth_authorize_url(resource_or_scope, provider, *args)
12
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
13
+ _devise_route_context.send("#{scope}_#{provider}_omniauth_authorize_url", *args)
5
14
  end
6
15
 
7
- def omniauth_authorize_path(resource_or_scope, *args)
16
+ def omniauth_callback_path(resource_or_scope, provider, *args)
8
17
  scope = Devise::Mapping.find_scope!(resource_or_scope)
9
- _devise_route_context.send("#{scope}_omniauth_authorize_path", *args)
18
+ _devise_route_context.send("#{scope}_#{provider}_omniauth_callback_path", *args)
10
19
  end
11
20
 
12
- def omniauth_callback_path(resource_or_scope, *args)
21
+ def omniauth_callback_url(resource_or_scope, provider, *args)
13
22
  scope = Devise::Mapping.find_scope!(resource_or_scope)
14
- _devise_route_context.send("#{scope}_omniauth_callback_path", *args)
23
+ _devise_route_context.send("#{scope}_#{provider}_omniauth_callback_url", *args)
15
24
  end
16
25
  end
17
26
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require "omniauth"
3
5
  require "omniauth/version"
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'orm_adapter/adapters/active_record'
2
4
 
3
- ActiveRecord::Base.extend Devise::Models
5
+ ActiveSupport.on_load(:active_record) do
6
+ extend Devise::Models
7
+ end
@@ -1,3 +1,7 @@
1
- require 'orm_adapter/adapters/mongoid'
1
+ # frozen_string_literal: true
2
2
 
3
- Mongoid::Document::ClassMethods.send :include, Devise::Models
3
+ ActiveSupport.on_load(:mongoid) do
4
+ require 'orm_adapter/adapters/mongoid'
5
+
6
+ Mongoid::Document::ClassMethods.send :include, Devise::Models
7
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Devise
2
4
  class ParameterFilter
3
5
  def initialize(case_insensitive_keys, strip_whitespace_keys)
@@ -16,6 +18,8 @@ module Devise
16
18
 
17
19
  def filtered_hash_by_method_for_given_keys(conditions, method, condition_keys)
18
20
  condition_keys.each do |k|
21
+ next unless conditions.key?(k)
22
+
19
23
  value = conditions[k]
20
24
  conditions[k] = value.send(method) if value.respond_to?(method)
21
25
  end