devise_token_auth 0.1.43 → 0.2.0

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

Potentially problematic release.


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

Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +40 -894
  3. data/Rakefile +2 -0
  4. data/app/controllers/devise_token_auth/application_controller.rb +6 -0
  5. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +2 -0
  6. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +9 -14
  7. data/app/controllers/devise_token_auth/confirmations_controller.rb +3 -1
  8. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +2 -0
  9. data/app/controllers/devise_token_auth/passwords_controller.rb +8 -14
  10. data/app/controllers/devise_token_auth/registrations_controller.rb +26 -21
  11. data/app/controllers/devise_token_auth/sessions_controller.rb +2 -0
  12. data/app/controllers/devise_token_auth/token_validations_controller.rb +2 -0
  13. data/app/controllers/devise_token_auth/unlocks_controller.rb +2 -0
  14. data/app/models/devise_token_auth/concerns/user.rb +41 -16
  15. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +2 -0
  16. data/app/validators/email_validator.rb +3 -1
  17. data/config/initializers/devise.rb +2 -0
  18. data/config/locales/da-DK.yml +1 -1
  19. data/config/locales/sv.yml +50 -0
  20. data/lib/devise_token_auth.rb +3 -0
  21. data/lib/devise_token_auth/controllers/helpers.rb +2 -0
  22. data/lib/devise_token_auth/controllers/url_helpers.rb +2 -0
  23. data/lib/devise_token_auth/engine.rb +2 -0
  24. data/lib/devise_token_auth/errors.rb +7 -0
  25. data/lib/devise_token_auth/rails/routes.rb +2 -0
  26. data/lib/devise_token_auth/url.rb +2 -0
  27. data/lib/devise_token_auth/version.rb +3 -1
  28. data/lib/generators/devise_token_auth/USAGE +1 -1
  29. data/lib/generators/devise_token_auth/install_generator.rb +7 -5
  30. data/lib/generators/devise_token_auth/install_views_generator.rb +2 -0
  31. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +2 -0
  32. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +8 -7
  33. data/lib/generators/devise_token_auth/templates/user.rb +3 -1
  34. data/lib/tasks/devise_token_auth_tasks.rake +2 -0
  35. data/test/controllers/custom/custom_confirmations_controller_test.rb +2 -0
  36. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +2 -0
  37. data/test/controllers/custom/custom_passwords_controller_test.rb +2 -0
  38. data/test/controllers/custom/custom_registrations_controller_test.rb +11 -0
  39. data/test/controllers/custom/custom_sessions_controller_test.rb +2 -0
  40. data/test/controllers/custom/custom_token_validations_controller_test.rb +2 -0
  41. data/test/controllers/demo_group_controller_test.rb +2 -0
  42. data/test/controllers/demo_mang_controller_test.rb +4 -1
  43. data/test/controllers/demo_user_controller_test.rb +47 -10
  44. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +4 -6
  45. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +7 -4
  46. data/test/controllers/devise_token_auth/passwords_controller_test.rb +2 -0
  47. data/test/controllers/devise_token_auth/registrations_controller_test.rb +3 -1
  48. data/test/controllers/devise_token_auth/sessions_controller_test.rb +52 -0
  49. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +2 -0
  50. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +2 -0
  51. data/test/controllers/overrides/confirmations_controller_test.rb +2 -0
  52. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +2 -0
  53. data/test/controllers/overrides/passwords_controller_test.rb +2 -0
  54. data/test/controllers/overrides/registrations_controller_test.rb +31 -27
  55. data/test/controllers/overrides/sessions_controller_test.rb +2 -0
  56. data/test/controllers/overrides/token_validations_controller_test.rb +2 -0
  57. data/test/dummy/app/controllers/application_controller.rb +2 -0
  58. data/test/dummy/app/controllers/auth_origin_controller.rb +2 -0
  59. data/test/dummy/app/controllers/custom/confirmations_controller.rb +2 -0
  60. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +2 -0
  61. data/test/dummy/app/controllers/custom/passwords_controller.rb +2 -0
  62. data/test/dummy/app/controllers/custom/registrations_controller.rb +2 -1
  63. data/test/dummy/app/controllers/custom/sessions_controller.rb +2 -0
  64. data/test/dummy/app/controllers/custom/token_validations_controller.rb +2 -0
  65. data/test/dummy/app/controllers/demo_group_controller.rb +2 -0
  66. data/test/dummy/app/controllers/demo_mang_controller.rb +2 -0
  67. data/test/dummy/app/controllers/demo_user_controller.rb +2 -0
  68. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +2 -0
  69. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +2 -0
  70. data/test/dummy/app/controllers/overrides/passwords_controller.rb +2 -0
  71. data/test/dummy/app/controllers/overrides/registrations_controller.rb +2 -0
  72. data/test/dummy/app/controllers/overrides/sessions_controller.rb +2 -0
  73. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +2 -0
  74. data/test/dummy/app/helpers/application_helper.rb +2 -0
  75. data/test/dummy/app/models/evil_user.rb +2 -0
  76. data/test/dummy/app/models/lockable_user.rb +2 -0
  77. data/test/dummy/app/models/mang.rb +2 -0
  78. data/test/dummy/app/models/nice_user.rb +2 -0
  79. data/test/dummy/app/models/only_email_user.rb +2 -0
  80. data/test/dummy/app/models/scoped_user.rb +2 -0
  81. data/test/dummy/app/models/unconfirmable_user.rb +2 -0
  82. data/test/dummy/app/models/unregisterable_user.rb +2 -0
  83. data/test/dummy/app/models/user.rb +2 -0
  84. data/test/dummy/config/application.rb +2 -0
  85. data/test/dummy/config/boot.rb +2 -0
  86. data/test/dummy/config/environment.rb +2 -0
  87. data/test/dummy/config/environments/development.rb +2 -0
  88. data/test/dummy/config/environments/production.rb +2 -0
  89. data/test/dummy/config/environments/test.rb +2 -0
  90. data/test/dummy/config/initializers/assets.rb +2 -0
  91. data/test/dummy/config/initializers/backtrace_silencers.rb +2 -0
  92. data/test/dummy/config/initializers/cookies_serializer.rb +3 -1
  93. data/test/dummy/config/initializers/devise.rb +2 -0
  94. data/test/dummy/config/initializers/devise_token_auth.rb +2 -0
  95. data/test/dummy/config/initializers/figaro.rb +2 -0
  96. data/test/dummy/config/initializers/filter_parameter_logging.rb +2 -0
  97. data/test/dummy/config/initializers/inflections.rb +2 -0
  98. data/test/dummy/config/initializers/mime_types.rb +2 -0
  99. data/test/dummy/config/initializers/omniauth.rb +2 -0
  100. data/test/dummy/config/initializers/session_store.rb +2 -0
  101. data/test/dummy/config/initializers/wrap_parameters.rb +2 -0
  102. data/test/dummy/config/routes.rb +2 -0
  103. data/test/dummy/config/spring.rb +2 -0
  104. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +2 -0
  105. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +2 -0
  106. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +2 -0
  107. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +2 -0
  108. data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +2 -0
  109. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +2 -0
  110. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +2 -0
  111. data/test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb +2 -0
  112. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +2 -0
  113. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +2 -0
  114. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +2 -0
  115. data/test/dummy/lib/migration_database_helper.rb +15 -1
  116. data/test/dummy/tmp/generators/app/models/user.rb +5 -5
  117. data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +2 -0
  118. data/test/dummy/tmp/generators/db/migrate/{20171014052631_devise_token_auth_create_users.rb → 20180805205504_devise_token_auth_create_users.rb} +1 -0
  119. data/test/integration/navigation_test.rb +2 -0
  120. data/test/lib/devise_token_auth/url_test.rb +2 -0
  121. data/test/lib/generators/devise_token_auth/install_generator_test.rb +2 -0
  122. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +194 -0
  123. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +2 -0
  124. data/test/models/only_email_user_test.rb +2 -0
  125. data/test/models/user_test.rb +6 -4
  126. data/test/test_helper.rb +30 -14
  127. metadata +23 -5
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'bundler/setup'
3
5
  rescue LoadError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth
2
4
  class ApplicationController < DeviseController
3
5
  include DeviseTokenAuth::Concerns::SetUserByToken
@@ -17,6 +19,10 @@ module DeviseTokenAuth
17
19
 
18
20
  protected
19
21
 
22
+ def blacklisted_redirect_url?
23
+ DeviseTokenAuth.redirect_whitelist && !DeviseTokenAuth::Url.whitelisted?(@redirect_url)
24
+ end
25
+
20
26
  def build_redirect_headers(access_token, client, redirect_header_options = {})
21
27
  {
22
28
  DeviseTokenAuth.headers_names[:"access-token"] => access_token,
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth::Concerns::ResourceFinder
2
4
  extend ActiveSupport::Concern
3
5
  include DeviseTokenAuth::Controllers::Helpers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth::Concerns::SetUserByToken
2
4
  extend ActiveSupport::Concern
3
5
  include DeviseTokenAuth::Concerns::ResourceFinder
@@ -11,7 +13,7 @@ module DeviseTokenAuth::Concerns::SetUserByToken
11
13
 
12
14
  # keep track of request duration
13
15
  def set_request_start
14
- @request_started_at = Time.now
16
+ @request_started_at = Time.zone.now
15
17
  @used_auth_by_token = true
16
18
 
17
19
  # initialize instance variables
@@ -60,7 +62,9 @@ module DeviseTokenAuth::Concerns::SetUserByToken
60
62
  if devise_warden_user && devise_warden_user.tokens[@client_id].nil?
61
63
  @used_auth_by_token = false
62
64
  @resource = devise_warden_user
63
- @resource.create_new_auth_token
65
+ # REVIEW: The following line _should_ be safe to remove;
66
+ # the generated token does not get used anywhere.
67
+ # @resource.create_new_auth_token
64
68
  end
65
69
  end
66
70
 
@@ -131,22 +135,13 @@ module DeviseTokenAuth::Concerns::SetUserByToken
131
135
  if @is_batch_request
132
136
  auth_header = @resource.extend_batch_buffer(@token, @client_id)
133
137
 
134
- # Do not return token for batch requests to avoid invalidated
135
- # tokens returned to the client in case of race conditions.
136
- # Use a blank string for the header to still be present and
137
- # being passed in a XHR response in case of
138
- # 304 Not Modified responses.
139
- auth_header[DeviseTokenAuth.headers_names[:"access-token"]] = ' '
140
- auth_header[DeviseTokenAuth.headers_names[:"expiry"]] = ' '
141
-
142
138
  # update Authorization response header with new token
143
139
  else
144
140
  auth_header = @resource.create_new_auth_token(@client_id)
145
- end
146
-
147
- # update the response header
148
- response.headers.merge!(auth_header)
149
141
 
142
+ # update the response header
143
+ response.headers.merge!(auth_header)
144
+ end
150
145
  end # end lock
151
146
  end # end ensure_pristine_resource
152
147
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth
2
4
  class ConfirmationsController < DeviseTokenAuth::ApplicationController
3
5
  def show
@@ -6,7 +8,7 @@ module DeviseTokenAuth
6
8
  if @resource && @resource.id
7
9
  expiry = nil
8
10
  if defined?(@resource.sign_in_count) && @resource.sign_in_count > 0
9
- expiry = (Time.now + 1.second).to_i
11
+ expiry = (Time.zone.now + 1.second).to_i
10
12
  end
11
13
 
12
14
  client_id, token = @resource.create_token expiry: expiry
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth
2
4
  class OmniauthCallbacksController < DeviseTokenAuth::ApplicationController
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth
2
4
  class PasswordsController < DeviseTokenAuth::ApplicationController
3
5
  before_action :set_user_by_token, :only => [:update]
@@ -11,21 +13,13 @@ module DeviseTokenAuth
11
13
  end
12
14
 
13
15
  # give redirect value from params priority
14
- @redirect_url = params[:redirect_url]
15
-
16
- # fall back to default value if provided
17
- @redirect_url ||= DeviseTokenAuth.default_password_reset_url
18
-
19
- unless @redirect_url
20
- return render_create_error_missing_redirect_url
21
- end
16
+ @redirect_url = params.fetch(
17
+ :redirect_url,
18
+ DeviseTokenAuth.default_password_reset_url
19
+ )
22
20
 
23
- # if whitelist is set, validate redirect_url against whitelist
24
- if DeviseTokenAuth.redirect_whitelist
25
- unless DeviseTokenAuth::Url.whitelisted?(@redirect_url)
26
- return render_create_error_not_allowed_redirect_url
27
- end
28
- end
21
+ return render_create_error_missing_redirect_url unless @redirect_url
22
+ return render_create_error_not_allowed_redirect_url if blacklisted_redirect_url?
29
23
 
30
24
  @email = get_case_insensitive_field_from_resource_params(:email)
31
25
  @resource = find_resource(:uid, @email)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth
2
4
  class RegistrationsController < DeviseTokenAuth::ApplicationController
3
5
  before_action :set_user_by_token, only: [:destroy, :update]
@@ -6,21 +8,18 @@ module DeviseTokenAuth
6
8
  skip_after_action :update_auth_header, only: [:create, :destroy]
7
9
 
8
10
  def create
9
- @resource = resource_class.new(sign_up_params.except(:confirm_success_url))
10
- @resource.provider = provider
11
+ build_resource
11
12
 
12
- # honor devise configuration for case_insensitive_keys
13
- if resource_class.case_insensitive_keys.include?(:email)
14
- @resource.email = sign_up_params[:email].try :downcase
15
- else
16
- @resource.email = sign_up_params[:email]
13
+ unless @resource.present?
14
+ raise DeviseTokenAuth::Errors::NoResourceDefinedError,
15
+ "#{self.class.name} #build_resource does not define @resource, execution stopped"
17
16
  end
18
17
 
19
18
  # give redirect value from params priority
20
- @redirect_url = sign_up_params[:confirm_success_url]
21
-
22
- # fall back to default value if provided
23
- @redirect_url ||= DeviseTokenAuth.default_confirm_success_url
19
+ @redirect_url = params.fetch(
20
+ :confirm_success_url,
21
+ DeviseTokenAuth.default_confirm_success_url
22
+ )
24
23
 
25
24
  # success redirect url is required
26
25
  if confirmable_enabled? && !@redirect_url
@@ -28,20 +27,18 @@ module DeviseTokenAuth
28
27
  end
29
28
 
30
29
  # if whitelist is set, validate redirect_url against whitelist
31
- if DeviseTokenAuth.redirect_whitelist
32
- unless DeviseTokenAuth::Url.whitelisted?(@redirect_url)
33
- return render_create_error_redirect_url_not_allowed
34
- end
35
- end
30
+ return render_create_error_redirect_url_not_allowed if blacklisted_redirect_url?
36
31
 
37
32
  begin
38
33
  # override email confirmation, must be sent manually from ctrl
39
34
  resource_class.set_callback("create", :after, :send_on_create_confirmation_instructions)
40
35
  resource_class.skip_callback("create", :after, :send_on_create_confirmation_instructions)
36
+
41
37
  if @resource.respond_to? :skip_confirmation_notification!
42
38
  # Fix duplicate e-mails by disabling Devise confirmation e-mail
43
39
  @resource.skip_confirmation_notification!
44
40
  end
41
+
45
42
  if @resource.save
46
43
  yield @resource if block_given?
47
44
 
@@ -51,13 +48,10 @@ module DeviseTokenAuth
51
48
  client_config: params[:config_name],
52
49
  redirect_url: @redirect_url
53
50
  })
54
-
55
51
  else
56
52
  # email auth has been bypassed, authenticate user
57
53
  @client_id, @token = @resource.create_token
58
-
59
54
  @resource.save!
60
-
61
55
  update_auth_header
62
56
  end
63
57
  render_create_success
@@ -88,7 +82,6 @@ module DeviseTokenAuth
88
82
  if @resource
89
83
  @resource.destroy
90
84
  yield @resource if block_given?
91
-
92
85
  render_destroy_success
93
86
  else
94
87
  render_destroy_error
@@ -96,7 +89,7 @@ module DeviseTokenAuth
96
89
  end
97
90
 
98
91
  def sign_up_params
99
- params.permit([*params_for_resource(:sign_up), :confirm_success_url])
92
+ params.permit(*params_for_resource(:sign_up))
100
93
  end
101
94
 
102
95
  def account_update_params
@@ -105,6 +98,18 @@ module DeviseTokenAuth
105
98
 
106
99
  protected
107
100
 
101
+ def build_resource
102
+ @resource = resource_class.new(sign_up_params)
103
+ @resource.provider = provider
104
+
105
+ # honor devise configuration for case_insensitive_keys
106
+ if resource_class.case_insensitive_keys.include?(:email)
107
+ @resource.email = sign_up_params[:email].try(:downcase)
108
+ else
109
+ @resource.email = sign_up_params[:email]
110
+ end
111
+ end
112
+
108
113
  def render_create_error_missing_confirm_success_url
109
114
  response = {
110
115
  status: 'error',
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # see http://www.emilsoman.com/blog/2013/05/18/building-a-tested/
2
4
  module DeviseTokenAuth
3
5
  class SessionsController < DeviseTokenAuth::ApplicationController
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth
2
4
  class TokenValidationsController < DeviseTokenAuth::ApplicationController
3
5
  skip_before_action :assert_is_devise_resource!, :only => [:validate_token]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth
2
4
  class UnlocksController < DeviseTokenAuth::ApplicationController
3
5
  skip_after_action :update_auth_header, :only => [:create, :show]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bcrypt'
2
4
 
3
5
  module DeviseTokenAuth::Concerns::User
@@ -91,13 +93,15 @@ module DeviseTokenAuth::Concerns::User
91
93
  def create_token(client_id: nil, token: nil, expiry: nil, **token_extras)
92
94
  client_id ||= SecureRandom.urlsafe_base64(nil, false)
93
95
  token ||= SecureRandom.urlsafe_base64(nil, false)
94
- expiry ||= (Time.now + token_lifespan).to_i
96
+ expiry ||= (Time.zone.now + token_lifespan).to_i
95
97
 
96
98
  self.tokens[client_id] = {
97
99
  token: BCrypt::Password.create(token),
98
100
  expiry: expiry
99
101
  }.merge!(token_extras)
100
102
 
103
+ clean_old_tokens
104
+
101
105
  [client_id, token, expiry]
102
106
  end
103
107
 
@@ -139,7 +143,7 @@ module DeviseTokenAuth::Concerns::User
139
143
  expiry && token &&
140
144
 
141
145
  # ensure that the token has not yet expired
142
- DateTime.strptime(expiry.to_s, '%s') > Time.now &&
146
+ DateTime.strptime(expiry.to_s, '%s') > Time.zone.now &&
143
147
 
144
148
  # ensure that the token is valid
145
149
  DeviseTokenAuth::Concerns::User.tokens_match?(token_hash, token)
@@ -158,7 +162,7 @@ module DeviseTokenAuth::Concerns::User
158
162
  updated_at && last_token &&
159
163
 
160
164
  # ensure that previous token falls within the batch buffer throttle time of the last request
161
- Time.parse(updated_at) > Time.now - DeviseTokenAuth.batch_request_buffer_throttle &&
165
+ Time.parse(updated_at) > Time.zone.now - DeviseTokenAuth.batch_request_buffer_throttle &&
162
166
 
163
167
  # ensure that the token is valid
164
168
  ::BCrypt::Password.new(last_token) == token
@@ -168,7 +172,7 @@ module DeviseTokenAuth::Concerns::User
168
172
 
169
173
  # update user's auth token (should happen on each request)
170
174
  def create_new_auth_token(client_id=nil)
171
- now = Time.now
175
+ now = Time.zone.now
172
176
 
173
177
  client_id, token = create_token(
174
178
  client_id: client_id,
@@ -196,17 +200,12 @@ module DeviseTokenAuth::Concerns::User
196
200
 
197
201
  def update_auth_header(token, client_id='default')
198
202
  headers = build_auth_header(token, client_id)
199
- while tokens.length > 0 && DeviseTokenAuth.max_number_of_devices < tokens.length
200
- oldest_client_id, _tk = tokens.min_by { |_cid, v| v[:expiry] || v["expiry"] }
201
- tokens.delete(oldest_client_id)
202
- end
203
-
203
+ clean_old_tokens
204
204
  save!
205
205
 
206
206
  headers
207
207
  end
208
208
 
209
-
210
209
  def build_auth_url(base_url, args)
211
210
  args[:uid] = uid
212
211
  args[:expiry] = tokens[args[:client_id]]['expiry']
@@ -214,9 +213,8 @@ module DeviseTokenAuth::Concerns::User
214
213
  DeviseTokenAuth::Url.generate(base_url, args)
215
214
  end
216
215
 
217
-
218
216
  def extend_batch_buffer(token, client_id)
219
- self.tokens[client_id]['updated_at'] = Time.now
217
+ self.tokens[client_id]['updated_at'] = Time.zone.now
220
218
  update_auth_header(token, client_id)
221
219
  end
222
220
 
@@ -242,19 +240,46 @@ module DeviseTokenAuth::Concerns::User
242
240
  if tokens
243
241
  tokens.delete_if do |cid, v|
244
242
  expiry = v[:expiry] || v["expiry"]
245
- DateTime.strptime(expiry.to_s, '%s') < Time.now
243
+ DateTime.strptime(expiry.to_s, '%s') < Time.zone.now
246
244
  end
247
245
  end
248
246
  end
249
247
 
248
+ def should_remove_tokens_after_password_reset?
249
+ if Rails::VERSION::MAJOR <= 5
250
+ encrypted_password_changed? &&
251
+ DeviseTokenAuth.remove_tokens_after_password_reset
252
+ else
253
+ saved_change_to_encrypted_password? &&
254
+ DeviseTokenAuth.remove_tokens_after_password_reset
255
+ end
256
+ end
257
+
250
258
  def remove_tokens_after_password_reset
251
- should_remove_old_tokens = DeviseTokenAuth.remove_tokens_after_password_reset &&
252
- encrypted_password_changed? && tokens && tokens.many?
259
+ return unless should_remove_tokens_after_password_reset?
253
260
 
254
- if should_remove_old_tokens
261
+ if tokens.present? && tokens.many?
255
262
  client_id, token_data = tokens.max_by { |cid, v| v[:expiry] || v["expiry"] }
256
263
  self.tokens = {client_id => token_data}
257
264
  end
258
265
  end
259
266
 
267
+ def max_client_tokens_exceeded?
268
+ tokens.length > DeviseTokenAuth.max_number_of_devices
269
+ end
270
+
271
+ def clean_old_tokens
272
+ if tokens.present? && max_client_tokens_exceeded?
273
+ # Using Enumerable#sort_by on a Hash will typecast it into an associative
274
+ # Array (i.e. an Array of key-value Array pairs). However, since Hashes
275
+ # have an internal order in Ruby 1.9+, the resulting sorted associative
276
+ # Array can be converted back into a Hash, while maintaining the sorted
277
+ # order.
278
+ self.tokens = tokens.sort_by { |_cid, v| v[:expiry] || v['expiry'] }.to_h
279
+
280
+ # Since the tokens are sorted by expiry, shift the oldest client token
281
+ # off the Hash until it no longer exceeds the maximum number of clients
282
+ tokens.shift while max_client_tokens_exceeded?
283
+ end
284
+ end
260
285
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeviseTokenAuth::Concerns::UserOmniauthCallbacks
2
4
  extend ActiveSupport::Concern
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class EmailValidator < ActiveModel::EachValidator
2
4
  def validate_each(record, attribute, value)
3
5
  unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
@@ -18,4 +20,4 @@ class EmailValidator < ActiveModel::EachValidator
18
20
 
19
21
  message
20
22
  end
21
- end
23
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Use this hook to configure devise mailer, warden hooks and so forth.
2
4
  # Many of these configuration options can be set straight in your model.
3
5
  Devise.setup do |config|
@@ -26,7 +26,7 @@ da-DK:
26
26
  unlocks:
27
27
  missing_email: "Du skal udfylde en email."
28
28
  sended: "En email er blevet sendt til '%{email}', som indeholder instruktioner for at låse kontoen op."
29
- user_not_found: "Kan ikke finde en burger med email '%{email}'."
29
+ user_not_found: "Kan ikke finde en bruger med email '%{email}'."
30
30
  errors:
31
31
  messages:
32
32
  validate_sign_up_params: "Angiv venligst passende registeringsdata i request body."
@@ -0,0 +1,50 @@
1
+ sv:
2
+ devise_token_auth:
3
+ sessions:
4
+ not_confirmed: "Ett bekräftelse-email har skickats till '%{email}'. Följ instruktionerna i emailet så kan ditt konto aktiveras"
5
+ bad_credentials: "Ogiltig login-information. Vänligen försök igen."
6
+ not_supported: "Använd POST /sign_in för att logga in. GET stöds inte."
7
+ user_not_found: "Användaren hittades inte eller var inte inloggad."
8
+ token_validations:
9
+ invalid: "Ogiltig token-information"
10
+ registrations:
11
+ missing_confirm_success_url: "Saknar 'confirm_success_url'-parameter."
12
+ redirect_url_not_allowed: "Omdirigering till '%{redirect_url}' ej tillåten."
13
+ email_already_exists: "Det finns redan ett konto för '%{email}'"
14
+ account_with_uid_destroyed: "Kontot med UID '%{uid}' har tagits bort."
15
+ account_to_destroy_not_found: "Kunde inte hitta kontot för borttagning."
16
+ user_not_found: "Användaren hittades ej."
17
+ passwords:
18
+ missing_email: "Du måste ange en emailadress."
19
+ missing_redirect_url: "Saknar en omdirigerings-URL."
20
+ not_allowed_redirect_url: "Omdirigering till '%{redirect_url}' ej tillåten."
21
+ sended: "Ett email har skickats till '%{email}' med instruktioner för hur du skapar ett nytt lösenord."
22
+ user_not_found: "Kunde inte hitta användaren med email '%{email}'."
23
+ password_not_required: "Det har kontot kräver inget lösenord. Logga in via ditt '%{provider}'-konto istället."
24
+ missing_passwords: "Du måste fylla i fälten 'Lösenord' och 'Upprepa lösenord'."
25
+ successfully_updated: "Ditt lösenord har ändrats."
26
+ unlocks:
27
+ missing_email: "Du måste ange en emailadress."
28
+ sended: "Ett email har skickats till '%{email}' med instruktioner för hur du låser upp ditt konto."
29
+ user_not_found: "Kunde inte hitta användaren med emailadressen '%{email}'."
30
+ errors:
31
+ messages:
32
+ validate_sign_up_params: "Vänligen skicka giltig data för att skapa konto i request-bodyn."
33
+ validate_account_update_params: "Vänligen skicka giltig data för att uppdatera konto i request-bodyn."
34
+ not_email: "är inte en emailadress"
35
+ devise:
36
+ mailer:
37
+ confirmation_instructions:
38
+ confirm_link_msg: "Du kan bekräfta ditt kontos emailadress genom att besöka länken nedan:"
39
+ confirm_account_link: "Bekräfta mitt konto"
40
+ reset_password_instructions:
41
+ request_reset_link_msg: "Någon har begärt en länk för att ändra ditt lösenord. Du kan göra detta via länken nedan."
42
+ password_change_link: "Byt mitt lösenord"
43
+ ignore_mail_msg: "Om du inte begärt detta, vänligen bortse från detta mail."
44
+ no_changes_msg: "Ditt lösenord kommer inte att ändras förrän du använder länken ovan och skapar ett nytt."
45
+ unlock_instructions:
46
+ account_lock_msg: "Ditt konto har låsts efter för många misslyckade loginförsök."
47
+ unlock_link_msg: "Klicka på länken nedan för att låsa upp ditt konto:"
48
+ unlock_link: "Lås upp mitt konto"
49
+ hello: "hej"
50
+ welcome: "välkommen"