sidecar_token_auth 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +13 -0
  3. data/README.md +12 -0
  4. data/Rakefile +42 -0
  5. data/app/controllers/devise_token_auth/application_controller.rb +79 -0
  6. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +44 -0
  7. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +162 -0
  8. data/app/controllers/devise_token_auth/confirmations_controller.rb +90 -0
  9. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +287 -0
  10. data/app/controllers/devise_token_auth/passwords_controller.rb +206 -0
  11. data/app/controllers/devise_token_auth/registrations_controller.rb +283 -0
  12. data/app/controllers/devise_token_auth/sessions_controller.rb +245 -0
  13. data/app/controllers/devise_token_auth/token_validations_controller.rb +31 -0
  14. data/app/controllers/devise_token_auth/unlocks_controller.rb +89 -0
  15. data/app/models/devise_token_auth/concerns/active_record_support.rb +16 -0
  16. data/app/models/devise_token_auth/concerns/confirmable_support.rb +28 -0
  17. data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
  18. data/app/models/devise_token_auth/concerns/tokens_serialization.rb +19 -0
  19. data/app/models/devise_token_auth/concerns/user.rb +257 -0
  20. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +31 -0
  21. data/app/validators/devise_token_auth_email_validator.rb +23 -0
  22. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  23. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  24. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  25. data/app/views/devise_token_auth/omniauth_external_window.html.erb +38 -0
  26. data/config/locales/da-DK.yml +52 -0
  27. data/config/locales/de.yml +51 -0
  28. data/config/locales/en.yml +60 -0
  29. data/config/locales/es.yml +51 -0
  30. data/config/locales/fr.yml +51 -0
  31. data/config/locales/he.yml +52 -0
  32. data/config/locales/it.yml +48 -0
  33. data/config/locales/ja.yml +48 -0
  34. data/config/locales/ko.yml +51 -0
  35. data/config/locales/nl.yml +32 -0
  36. data/config/locales/pl.yml +51 -0
  37. data/config/locales/pt-BR.yml +48 -0
  38. data/config/locales/pt.yml +51 -0
  39. data/config/locales/ro.yml +48 -0
  40. data/config/locales/ru.yml +52 -0
  41. data/config/locales/sq.yml +48 -0
  42. data/config/locales/sv.yml +52 -0
  43. data/config/locales/uk.yml +61 -0
  44. data/config/locales/vi.yml +52 -0
  45. data/config/locales/zh-CN.yml +48 -0
  46. data/config/locales/zh-HK.yml +50 -0
  47. data/config/locales/zh-TW.yml +50 -0
  48. data/lib/devise_token_auth.rb +14 -0
  49. data/lib/devise_token_auth/blacklist.rb +2 -0
  50. data/lib/devise_token_auth/controllers/helpers.rb +157 -0
  51. data/lib/devise_token_auth/controllers/url_helpers.rb +10 -0
  52. data/lib/devise_token_auth/engine.rb +96 -0
  53. data/lib/devise_token_auth/errors.rb +8 -0
  54. data/lib/devise_token_auth/rails/routes.rb +116 -0
  55. data/lib/devise_token_auth/token_factory.rb +126 -0
  56. data/lib/devise_token_auth/url.rb +44 -0
  57. data/lib/devise_token_auth/version.rb +5 -0
  58. data/lib/generators/devise_token_auth/USAGE +31 -0
  59. data/lib/generators/devise_token_auth/install_generator.rb +91 -0
  60. data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
  61. data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
  62. data/lib/generators/devise_token_auth/install_views_generator.rb +18 -0
  63. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +60 -0
  64. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +49 -0
  65. data/lib/generators/devise_token_auth/templates/user.rb.erb +9 -0
  66. data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +56 -0
  67. data/lib/tasks/devise_token_auth_tasks.rake +6 -0
  68. data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
  69. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
  70. data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
  71. data/test/controllers/custom/custom_registrations_controller_test.rb +63 -0
  72. data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
  73. data/test/controllers/custom/custom_token_validations_controller_test.rb +42 -0
  74. data/test/controllers/demo_group_controller_test.rb +151 -0
  75. data/test/controllers/demo_mang_controller_test.rb +284 -0
  76. data/test/controllers/demo_user_controller_test.rb +629 -0
  77. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +191 -0
  78. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +441 -0
  79. data/test/controllers/devise_token_auth/passwords_controller_test.rb +780 -0
  80. data/test/controllers/devise_token_auth/registrations_controller_test.rb +907 -0
  81. data/test/controllers/devise_token_auth/sessions_controller_test.rb +503 -0
  82. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +102 -0
  83. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +196 -0
  84. data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
  85. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
  86. data/test/controllers/overrides/passwords_controller_test.rb +64 -0
  87. data/test/controllers/overrides/registrations_controller_test.rb +46 -0
  88. data/test/controllers/overrides/sessions_controller_test.rb +35 -0
  89. data/test/controllers/overrides/token_validations_controller_test.rb +43 -0
  90. data/test/dummy/README.rdoc +28 -0
  91. data/test/dummy/app/active_record/confirmable_user.rb +11 -0
  92. data/test/dummy/app/active_record/lockable_user.rb +7 -0
  93. data/test/dummy/app/active_record/mang.rb +5 -0
  94. data/test/dummy/app/active_record/only_email_user.rb +7 -0
  95. data/test/dummy/app/active_record/scoped_user.rb +9 -0
  96. data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
  97. data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
  98. data/test/dummy/app/active_record/user.rb +6 -0
  99. data/test/dummy/app/controllers/application_controller.rb +18 -0
  100. data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
  101. data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
  102. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
  103. data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -0
  104. data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
  105. data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
  106. data/test/dummy/app/controllers/custom/token_validations_controller.rb +19 -0
  107. data/test/dummy/app/controllers/demo_group_controller.rb +15 -0
  108. data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
  109. data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
  110. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +28 -0
  111. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
  112. data/test/dummy/app/controllers/overrides/passwords_controller.rb +35 -0
  113. data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -0
  114. data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
  115. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
  116. data/test/dummy/app/helpers/application_helper.rb +1058 -0
  117. data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
  118. data/test/dummy/app/mongoid/confirmable_user.rb +52 -0
  119. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  120. data/test/dummy/app/mongoid/mang.rb +46 -0
  121. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  122. data/test/dummy/app/mongoid/scoped_user.rb +50 -0
  123. data/test/dummy/app/mongoid/unconfirmable_user.rb +44 -0
  124. data/test/dummy/app/mongoid/unregisterable_user.rb +47 -0
  125. data/test/dummy/app/mongoid/user.rb +49 -0
  126. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  127. data/test/dummy/config.ru +18 -0
  128. data/test/dummy/config/application.rb +48 -0
  129. data/test/dummy/config/application.yml.bk +0 -0
  130. data/test/dummy/config/boot.rb +11 -0
  131. data/test/dummy/config/environment.rb +7 -0
  132. data/test/dummy/config/environments/development.rb +46 -0
  133. data/test/dummy/config/environments/production.rb +84 -0
  134. data/test/dummy/config/environments/test.rb +50 -0
  135. data/test/dummy/config/initializers/assets.rb +10 -0
  136. data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
  137. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  138. data/test/dummy/config/initializers/devise.rb +290 -0
  139. data/test/dummy/config/initializers/devise_token_auth.rb +55 -0
  140. data/test/dummy/config/initializers/figaro.rb +3 -0
  141. data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  142. data/test/dummy/config/initializers/inflections.rb +18 -0
  143. data/test/dummy/config/initializers/mime_types.rb +6 -0
  144. data/test/dummy/config/initializers/omniauth.rb +11 -0
  145. data/test/dummy/config/initializers/session_store.rb +5 -0
  146. data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
  147. data/test/dummy/config/routes.rb +57 -0
  148. data/test/dummy/config/spring.rb +3 -0
  149. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +58 -0
  150. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +57 -0
  151. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
  152. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
  153. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +55 -0
  154. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +56 -0
  155. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +56 -0
  156. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +56 -0
  157. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +56 -0
  158. data/test/dummy/db/migrate/20190924101113_devise_token_auth_create_confirmable_users.rb +49 -0
  159. data/test/dummy/db/schema.rb +198 -0
  160. data/test/dummy/lib/migration_database_helper.rb +43 -0
  161. data/test/factories/users.rb +41 -0
  162. data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
  163. data/test/lib/devise_token_auth/token_factory_test.rb +191 -0
  164. data/test/lib/devise_token_auth/url_test.rb +26 -0
  165. data/test/lib/generators/devise_token_auth/install_generator_test.rb +217 -0
  166. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +222 -0
  167. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +25 -0
  168. data/test/models/concerns/mongoid_support_test.rb +31 -0
  169. data/test/models/concerns/tokens_serialization_test.rb +70 -0
  170. data/test/models/confirmable_user_test.rb +35 -0
  171. data/test/models/only_email_user_test.rb +29 -0
  172. data/test/models/user_test.rb +108 -0
  173. data/test/support/controllers/routes.rb +43 -0
  174. data/test/test_helper.rb +103 -0
  175. metadata +481 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 593d12ca2ac3e8397b389fa09656f6ae61a5a49852331a5cb7c82f8a3d083695
4
+ data.tar.gz: '08fe7c735782c2260e73e4abf9ad87188256cf4110c481dddc705db5a87753cb'
5
+ SHA512:
6
+ metadata.gz: e1b4cfa5dfcff0c83868c1a78386febd8322a22dfe9d535d3d021f609df333ba2dcd4d5089bb1974cf8e4a5e6dad840338a281f996b82957c204c9a4cc8dfa4a
7
+ data.tar.gz: 0c8ed59ffdc73a5eb205be7222c085d02b1fee94fb90c7976cf5b8022c708ebdb335fd673831fe1d26c29a884534fd5d21dc403d612994fad1fc4b6b670fecb9
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
@@ -0,0 +1,12 @@
1
+ This gem is forked from https://github.com/lynndylanhurley/devise_token_auth
2
+
3
+ The `sidecar` branch, which is set as a default branch for the repo, has all the customizations done to the gem speicific to sidecar.
4
+
5
+ The version of lynndylanhurley/devise_token_auth at the time of this README:
6
+ v1.1.4
7
+
8
+ How to upgrade(hypothetical - have not done earlier):
9
+ - Check out README
10
+ - `git pull` from upstream `master`
11
+ - `git checkout sidecar`
12
+ - `git rebase -i master`
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'DeviseTokenAuth'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.rdoc')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
20
+ load 'rails/tasks/engine.rake'
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ t.warning = false
32
+ end
33
+
34
+ task default: :test
35
+
36
+ require 'rubocop/rake_task'
37
+
38
+ desc 'Run RuboCop'
39
+ RuboCop::RakeTask.new(:rubocop) do |task|
40
+ task.formatters = %w[fuubar offenses worst]
41
+ task.fail_on_error = false # don't abort rake on failure
42
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeviseTokenAuth
4
+ class ApplicationController < DeviseController
5
+ include DeviseTokenAuth::Concerns::SetUserByToken
6
+
7
+ def resource_data(opts = {})
8
+ response_data = opts[:resource_json] || @resource.as_json
9
+ response_data['type'] = @resource.class.name.parameterize if json_api?
10
+ response_data
11
+ end
12
+
13
+ def resource_errors
14
+ @resource.errors.to_hash.merge(full_messages: @resource.errors.full_messages)
15
+ end
16
+
17
+ protected
18
+
19
+ def blacklisted_redirect_url?(redirect_url)
20
+ DeviseTokenAuth.redirect_whitelist && !DeviseTokenAuth::Url.whitelisted?(redirect_url)
21
+ end
22
+
23
+ def build_redirect_headers(access_token, client, redirect_header_options = {})
24
+ {
25
+ DeviseTokenAuth.headers_names[:"access-token"] => access_token,
26
+ DeviseTokenAuth.headers_names[:"client"] => client,
27
+ :config => params[:config],
28
+
29
+ # Legacy parameters which may be removed in a future release.
30
+ # Consider using "client" and "access-token" in client code.
31
+ # See: github.com/lynndylanhurley/devise_token_auth/issues/993
32
+ :client_id => client,
33
+ :token => access_token
34
+ }.merge(redirect_header_options)
35
+ end
36
+
37
+ def params_for_resource(resource)
38
+ devise_parameter_sanitizer.instance_values['permitted'][resource].each do |type|
39
+ params[type.to_s] ||= request.headers[type.to_s] unless request.headers[type.to_s].nil?
40
+ end
41
+ devise_parameter_sanitizer.instance_values['permitted'][resource]
42
+ end
43
+
44
+ def resource_class(m = nil)
45
+ if m
46
+ mapping = Devise.mappings[m]
47
+ else
48
+ mapping = Devise.mappings[resource_name] || Devise.mappings.values.first
49
+ end
50
+
51
+ mapping.to
52
+ end
53
+
54
+ def json_api?
55
+ return false unless defined?(ActiveModel::Serializer)
56
+ return ActiveModel::Serializer.setup do |config|
57
+ config.adapter == :json_api
58
+ end if ActiveModel::Serializer.respond_to?(:setup)
59
+ ActiveModelSerializers.config.adapter == :json_api
60
+ end
61
+
62
+ def recoverable_enabled?
63
+ resource_class.devise_modules.include?(:recoverable)
64
+ end
65
+
66
+ def confirmable_enabled?
67
+ resource_class.devise_modules.include?(:confirmable)
68
+ end
69
+
70
+ def render_error(status, message, data = nil)
71
+ response = {
72
+ success: false,
73
+ errors: [message]
74
+ }
75
+ response = response.merge(data) if data
76
+ render json: response, status: status
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeviseTokenAuth::Concerns::ResourceFinder
4
+ extend ActiveSupport::Concern
5
+ include DeviseTokenAuth::Controllers::Helpers
6
+
7
+ def get_case_insensitive_field_from_resource_params(field)
8
+ # honor Devise configuration for case_insensitive keys
9
+ q_value = resource_params[field.to_sym]
10
+
11
+ if resource_class.case_insensitive_keys.include?(field.to_sym)
12
+ q_value.downcase!
13
+ end
14
+
15
+ if resource_class.strip_whitespace_keys.include?(field.to_sym)
16
+ q_value.strip!
17
+ end
18
+
19
+ q_value
20
+ end
21
+
22
+ def find_resource(field, value)
23
+ @resource = if resource_class.try(:connection_config).try(:[], :adapter).try(:include?, 'mysql')
24
+ # fix for mysql default case insensitivity
25
+ resource_class.where("BINARY #{field} = ? AND provider= ?", value, provider).first
26
+ else
27
+ resource_class.dta_find_by(field => value, 'provider' => provider)
28
+ end
29
+ end
30
+
31
+ def resource_class(m = nil)
32
+ mapping = if m
33
+ Devise.mappings[m]
34
+ else
35
+ Devise.mappings[resource_name] || Devise.mappings.values.first
36
+ end
37
+
38
+ mapping.to
39
+ end
40
+
41
+ def provider
42
+ 'email'
43
+ end
44
+ end
@@ -0,0 +1,162 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeviseTokenAuth::Concerns::SetUserByToken
4
+ extend ActiveSupport::Concern
5
+ include DeviseTokenAuth::Concerns::ResourceFinder
6
+
7
+ included do
8
+ before_action :set_request_start
9
+ after_action :update_auth_header
10
+ end
11
+
12
+ protected
13
+
14
+ # keep track of request duration
15
+ def set_request_start
16
+ @request_started_at = Time.zone.now
17
+ @used_auth_by_token = true
18
+
19
+ # initialize instance variables
20
+ @token ||= DeviseTokenAuth::TokenFactory.new
21
+ @resource ||= nil
22
+ @is_batch_request ||= nil
23
+ end
24
+
25
+ # user auth
26
+ def set_user_by_token(mapping = nil)
27
+ # determine target authentication class
28
+ rc = resource_class(mapping)
29
+
30
+ # no default user defined
31
+ return unless rc
32
+
33
+ # gets the headers names, which was set in the initialize file
34
+ uid_name = DeviseTokenAuth.headers_names[:'uid']
35
+ access_token_name = DeviseTokenAuth.headers_names[:'access-token']
36
+ client_name = DeviseTokenAuth.headers_names[:'client']
37
+
38
+ # parse header for values necessary for authentication
39
+ uid = request.headers[uid_name] || params[uid_name]
40
+ @token = DeviseTokenAuth::TokenFactory.new unless @token
41
+ @token.token ||= request.headers[access_token_name] || params[access_token_name]
42
+ @token.client ||= request.headers[client_name] || params[client_name]
43
+
44
+ # client isn't required, set to 'default' if absent
45
+ @token.client ||= 'default'
46
+
47
+ # check for an existing user, authenticated via warden/devise, if enabled
48
+ if DeviseTokenAuth.enable_standard_devise_support
49
+ devise_warden_user = warden.user(rc.to_s.underscore.to_sym)
50
+ if devise_warden_user && devise_warden_user.tokens[@token.client].nil?
51
+ @used_auth_by_token = false
52
+ @resource = devise_warden_user
53
+ # REVIEW: The following line _should_ be safe to remove;
54
+ # the generated token does not get used anywhere.
55
+ # @resource.create_new_auth_token
56
+ end
57
+ end
58
+
59
+ # user has already been found and authenticated
60
+ return @resource if @resource && @resource.is_a?(rc)
61
+
62
+ # ensure we clear the client
63
+ unless @token.present?
64
+ @token.client = nil
65
+ return
66
+ end
67
+
68
+ # mitigate timing attacks by finding by uid instead of auth token
69
+ user = uid && rc.dta_find_by(uid: uid)
70
+ scope = rc.to_s.underscore.to_sym
71
+
72
+ if user && user.valid_token?(@token.token, @token.client)
73
+ # sign_in with bypass: true will be deprecated in the next version of Devise
74
+ if respond_to?(:bypass_sign_in) && DeviseTokenAuth.bypass_sign_in
75
+ bypass_sign_in(user, scope: scope)
76
+ else
77
+ sign_in(scope, user, store: false, event: :fetch, bypass: DeviseTokenAuth.bypass_sign_in)
78
+ end
79
+ return @resource = user
80
+ else
81
+ # zero all values previously set values
82
+ @token.client = nil
83
+ return @resource = nil
84
+ end
85
+ end
86
+
87
+ def update_auth_header
88
+ # cannot save object if model has invalid params
89
+ return unless @resource && @token.client
90
+
91
+ # Generate new client with existing authentication
92
+ @token.client = nil unless @used_auth_by_token
93
+
94
+ if @used_auth_by_token && !DeviseTokenAuth.change_headers_on_each_request
95
+ # should not append auth header if @resource related token was
96
+ # cleared by sign out in the meantime
97
+ return if @resource.reload.tokens[@token.client].nil?
98
+
99
+ auth_header = @resource.build_auth_header(@token.token, @token.client)
100
+
101
+ # update the response header
102
+ response.headers.merge!(auth_header)
103
+
104
+ else
105
+ unless @resource.reload.valid?
106
+ @resource = @resource.class.find(@resource.to_param) # errors remain after reload
107
+ # if we left the model in a bad state, something is wrong in our app
108
+ unless @resource.valid?
109
+ raise DeviseTokenAuth::Errors::InvalidModel, "Cannot set auth token in invalid model. Errors: #{@resource.errors.full_messages}"
110
+ end
111
+ end
112
+ refresh_headers
113
+ end
114
+ end
115
+
116
+ private
117
+
118
+ def refresh_headers
119
+ # Lock the user record during any auth_header updates to ensure
120
+ # we don't have write contention from multiple threads
121
+ @resource.with_lock do
122
+ # should not append auth header if @resource related token was
123
+ # cleared by sign out in the meantime
124
+ return if @used_auth_by_token && @resource.tokens[@token.client].nil?
125
+
126
+ # update the response header
127
+ response.headers.merge!(auth_header_from_batch_request)
128
+ end # end lock
129
+ end
130
+
131
+ def is_batch_request?(user, client)
132
+ !params[:unbatch] &&
133
+ user.tokens[client] &&
134
+ user.tokens[client]['updated_at'] &&
135
+ user.tokens[client]['updated_at'].to_time > @request_started_at - DeviseTokenAuth.batch_request_buffer_throttle
136
+ end
137
+
138
+ def auth_header_from_batch_request
139
+ # determine batch request status after request processing, in case
140
+ # another processes has updated it during that processing
141
+ @is_batch_request = is_batch_request?(@resource, @token.client)
142
+
143
+ auth_header = {}
144
+ # extend expiration of batch buffer to account for the duration of
145
+ # this request
146
+ if @is_batch_request
147
+ auth_header = @resource.extend_batch_buffer(@token.token, @token.client)
148
+
149
+ # Do not return token for batch requests to avoid invalidated
150
+ # tokens returned to the client in case of race conditions.
151
+ # Use a blank string for the header to still be present and
152
+ # being passed in a XHR response in case of
153
+ # 304 Not Modified responses.
154
+ auth_header[DeviseTokenAuth.headers_names[:"access-token"]] = ' '
155
+ auth_header[DeviseTokenAuth.headers_names[:"expiry"]] = ' '
156
+ else
157
+ # update Authorization response header with new token
158
+ auth_header = @resource.create_new_auth_token(@token.client)
159
+ end
160
+ auth_header
161
+ end
162
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeviseTokenAuth
4
+ class ConfirmationsController < DeviseTokenAuth::ApplicationController
5
+
6
+ def show
7
+ @resource = resource_class.confirm_by_token(resource_params[:confirmation_token])
8
+
9
+ if @resource.errors.empty?
10
+ yield @resource if block_given?
11
+
12
+ redirect_header_options = { account_confirmation_success: true }
13
+
14
+ if signed_in?(resource_name)
15
+ token = signed_in_resource.create_token
16
+ signed_in_resource.save!
17
+
18
+ redirect_headers = build_redirect_headers(token.token,
19
+ token.client,
20
+ redirect_header_options)
21
+
22
+ redirect_to_link = signed_in_resource.build_auth_url(redirect_url, redirect_headers)
23
+ else
24
+ redirect_to_link = DeviseTokenAuth::Url.generate(redirect_url, redirect_header_options)
25
+ end
26
+
27
+ redirect_to(redirect_to_link)
28
+ else
29
+ ##################################################
30
+ # Custom Logic Start
31
+ ##################################################
32
+
33
+ redirect_to redirect_url + '?' + { error: 'You may have already confirmed. Try to login!', status: 'error' }.to_query
34
+
35
+ ##################################################
36
+ # Custom Logic End
37
+ ##################################################
38
+ end
39
+ end
40
+
41
+ def create
42
+ return render_create_error_missing_email if resource_params[:email].blank?
43
+
44
+ @email = get_case_insensitive_field_from_resource_params(:email)
45
+
46
+ @resource = resource_class.dta_find_by(uid: @email, provider: provider)
47
+
48
+ return render_not_found_error unless @resource
49
+
50
+ @resource.send_confirmation_instructions({
51
+ redirect_url: redirect_url,
52
+ client_config: resource_params[:config_name]
53
+ })
54
+
55
+ return render_create_success
56
+ end
57
+
58
+ protected
59
+
60
+ def render_create_error_missing_email
61
+ render_error(401, I18n.t('devise_token_auth.confirmations.missing_email'))
62
+ end
63
+
64
+ def render_create_success
65
+ render json: {
66
+ success: true,
67
+ message: I18n.t('devise_token_auth.confirmations.sended', email: @email)
68
+ }
69
+ end
70
+
71
+ def render_not_found_error
72
+ render_error(404, I18n.t('devise_token_auth.confirmations.user_not_found', email: @email))
73
+ end
74
+
75
+ private
76
+
77
+ def resource_params
78
+ params.permit(:email, :confirmation_token, :config_name)
79
+ end
80
+
81
+ # give redirect value from params priority or fall back to default value if provided
82
+ def redirect_url
83
+ params.fetch(
84
+ :redirect_url,
85
+ DeviseTokenAuth.default_confirm_success_url
86
+ )
87
+ end
88
+
89
+ end
90
+ end