devise_token_auth_skycocker_fork 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +13 -0
  3. data/README.md +97 -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 +178 -0
  8. data/app/controllers/devise_token_auth/confirmations_controller.rb +39 -0
  9. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +239 -0
  10. data/app/controllers/devise_token_auth/passwords_controller.rb +185 -0
  11. data/app/controllers/devise_token_auth/registrations_controller.rb +198 -0
  12. data/app/controllers/devise_token_auth/sessions_controller.rb +131 -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 +34 -0
  16. data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
  17. data/app/models/devise_token_auth/concerns/user.rb +262 -0
  18. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +28 -0
  19. data/app/validators/devise_token_auth/email_validator.rb +23 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise_token_auth/omniauth_external_window.html.erb +38 -0
  24. data/config/locales/da-DK.yml +50 -0
  25. data/config/locales/de.yml +49 -0
  26. data/config/locales/en.yml +50 -0
  27. data/config/locales/es.yml +49 -0
  28. data/config/locales/fr.yml +49 -0
  29. data/config/locales/it.yml +46 -0
  30. data/config/locales/ja.yml +46 -0
  31. data/config/locales/nl.yml +30 -0
  32. data/config/locales/pl.yml +48 -0
  33. data/config/locales/pt-BR.yml +46 -0
  34. data/config/locales/pt.yml +48 -0
  35. data/config/locales/ro.yml +46 -0
  36. data/config/locales/ru.yml +50 -0
  37. data/config/locales/sq.yml +46 -0
  38. data/config/locales/sv.yml +50 -0
  39. data/config/locales/uk.yml +59 -0
  40. data/config/locales/vi.yml +50 -0
  41. data/config/locales/zh-CN.yml +46 -0
  42. data/config/locales/zh-HK.yml +48 -0
  43. data/config/locales/zh-TW.yml +48 -0
  44. data/lib/devise_token_auth.rb +13 -0
  45. data/lib/devise_token_auth/blacklist.rb +2 -0
  46. data/lib/devise_token_auth/controllers/helpers.rb +161 -0
  47. data/lib/devise_token_auth/controllers/url_helpers.rb +10 -0
  48. data/lib/devise_token_auth/engine.rb +90 -0
  49. data/lib/devise_token_auth/errors.rb +8 -0
  50. data/lib/devise_token_auth/rails/routes.rb +116 -0
  51. data/lib/devise_token_auth/url.rb +41 -0
  52. data/lib/devise_token_auth/version.rb +5 -0
  53. data/lib/generators/devise_token_auth/USAGE +31 -0
  54. data/lib/generators/devise_token_auth/install_generator.rb +91 -0
  55. data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
  56. data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
  57. data/lib/generators/devise_token_auth/install_views_generator.rb +18 -0
  58. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +50 -0
  59. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +56 -0
  60. data/lib/generators/devise_token_auth/templates/user.rb.erb +9 -0
  61. data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +63 -0
  62. data/lib/tasks/devise_token_auth_tasks.rake +6 -0
  63. data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
  64. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
  65. data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
  66. data/test/controllers/custom/custom_registrations_controller_test.rb +63 -0
  67. data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
  68. data/test/controllers/custom/custom_token_validations_controller_test.rb +42 -0
  69. data/test/controllers/demo_group_controller_test.rb +151 -0
  70. data/test/controllers/demo_mang_controller_test.rb +284 -0
  71. data/test/controllers/demo_user_controller_test.rb +629 -0
  72. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +127 -0
  73. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +376 -0
  74. data/test/controllers/devise_token_auth/passwords_controller_test.rb +639 -0
  75. data/test/controllers/devise_token_auth/registrations_controller_test.rb +880 -0
  76. data/test/controllers/devise_token_auth/sessions_controller_test.rb +541 -0
  77. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +102 -0
  78. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +196 -0
  79. data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
  80. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
  81. data/test/controllers/overrides/passwords_controller_test.rb +64 -0
  82. data/test/controllers/overrides/registrations_controller_test.rb +46 -0
  83. data/test/controllers/overrides/sessions_controller_test.rb +35 -0
  84. data/test/controllers/overrides/token_validations_controller_test.rb +43 -0
  85. data/test/dummy/README.rdoc +28 -0
  86. data/test/dummy/app/active_record/lockable_user.rb +7 -0
  87. data/test/dummy/app/active_record/mang.rb +5 -0
  88. data/test/dummy/app/active_record/only_email_user.rb +7 -0
  89. data/test/dummy/app/active_record/scoped_user.rb +9 -0
  90. data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
  91. data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
  92. data/test/dummy/app/active_record/user.rb +6 -0
  93. data/test/dummy/app/controllers/application_controller.rb +18 -0
  94. data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
  95. data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
  96. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
  97. data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -0
  98. data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
  99. data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
  100. data/test/dummy/app/controllers/custom/token_validations_controller.rb +19 -0
  101. data/test/dummy/app/controllers/demo_group_controller.rb +15 -0
  102. data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
  103. data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
  104. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +28 -0
  105. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
  106. data/test/dummy/app/controllers/overrides/passwords_controller.rb +35 -0
  107. data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -0
  108. data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
  109. data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
  110. data/test/dummy/app/helpers/application_helper.rb +1058 -0
  111. data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
  112. data/test/dummy/app/mongoid/lockable_user.rb +38 -0
  113. data/test/dummy/app/mongoid/mang.rb +53 -0
  114. data/test/dummy/app/mongoid/only_email_user.rb +33 -0
  115. data/test/dummy/app/mongoid/scoped_user.rb +57 -0
  116. data/test/dummy/app/mongoid/unconfirmable_user.rb +51 -0
  117. data/test/dummy/app/mongoid/unregisterable_user.rb +54 -0
  118. data/test/dummy/app/mongoid/user.rb +56 -0
  119. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  120. data/test/dummy/config.ru +18 -0
  121. data/test/dummy/config/application.rb +48 -0
  122. data/test/dummy/config/application.yml.bk +0 -0
  123. data/test/dummy/config/boot.rb +11 -0
  124. data/test/dummy/config/environment.rb +7 -0
  125. data/test/dummy/config/environments/development.rb +46 -0
  126. data/test/dummy/config/environments/production.rb +84 -0
  127. data/test/dummy/config/environments/test.rb +50 -0
  128. data/test/dummy/config/initializers/assets.rb +10 -0
  129. data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
  130. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  131. data/test/dummy/config/initializers/devise.rb +17 -0
  132. data/test/dummy/config/initializers/devise_token_auth.rb +24 -0
  133. data/test/dummy/config/initializers/figaro.rb +3 -0
  134. data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
  135. data/test/dummy/config/initializers/inflections.rb +18 -0
  136. data/test/dummy/config/initializers/mime_types.rb +6 -0
  137. data/test/dummy/config/initializers/omniauth.rb +10 -0
  138. data/test/dummy/config/initializers/session_store.rb +5 -0
  139. data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
  140. data/test/dummy/config/routes.rb +55 -0
  141. data/test/dummy/config/spring.rb +3 -0
  142. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +65 -0
  143. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +64 -0
  144. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
  145. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
  146. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +62 -0
  147. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +63 -0
  148. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +63 -0
  149. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +63 -0
  150. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +63 -0
  151. data/test/dummy/db/schema.rb +200 -0
  152. data/test/dummy/lib/migration_database_helper.rb +43 -0
  153. data/test/dummy/tmp/generators/app/models/user.rb +9 -0
  154. data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +50 -0
  155. data/test/dummy/tmp/generators/db/migrate/20181030122248_devise_token_auth_create_users.rb +56 -0
  156. data/test/factories/users.rb +40 -0
  157. data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
  158. data/test/lib/devise_token_auth/url_test.rb +26 -0
  159. data/test/lib/generators/devise_token_auth/install_generator_test.rb +217 -0
  160. data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +222 -0
  161. data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +25 -0
  162. data/test/models/concerns/mongoid_support_test.rb +31 -0
  163. data/test/models/only_email_user_test.rb +37 -0
  164. data/test/models/user_test.rb +140 -0
  165. data/test/support/controllers/routes.rb +43 -0
  166. data/test/test_helper.rb +103 -0
  167. metadata +443 -0
@@ -0,0 +1,198 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeviseTokenAuth
4
+ class RegistrationsController < DeviseTokenAuth::ApplicationController
5
+ before_action :set_user_by_token, only: [:destroy, :update]
6
+ before_action :validate_sign_up_params, only: :create
7
+ before_action :validate_account_update_params, only: :update
8
+ skip_after_action :update_auth_header, only: [:create, :destroy]
9
+
10
+ def create
11
+ build_resource
12
+
13
+ unless @resource.present?
14
+ raise DeviseTokenAuth::Errors::NoResourceDefinedError,
15
+ "#{self.class.name} #build_resource does not define @resource,"\
16
+ ' execution stopped.'
17
+ end
18
+
19
+ # give redirect value from params priority
20
+ @redirect_url = params.fetch(
21
+ :confirm_success_url,
22
+ DeviseTokenAuth.default_confirm_success_url
23
+ )
24
+
25
+ # success redirect url is required
26
+ if confirmable_enabled? && !@redirect_url
27
+ return render_create_error_missing_confirm_success_url
28
+ end
29
+
30
+ # if whitelist is set, validate redirect_url against whitelist
31
+ return render_create_error_redirect_url_not_allowed if blacklisted_redirect_url?
32
+
33
+ # override email confirmation, must be sent manually from ctrl
34
+ resource_class.set_callback('create', :after, :send_on_create_confirmation_instructions)
35
+ resource_class.skip_callback('create', :after, :send_on_create_confirmation_instructions)
36
+
37
+ if @resource.respond_to? :skip_confirmation_notification!
38
+ # Fix duplicate e-mails by disabling Devise confirmation e-mail
39
+ @resource.skip_confirmation_notification!
40
+ end
41
+
42
+ if @resource.save
43
+ yield @resource if block_given?
44
+
45
+ if @resource.confirmed?
46
+ # email auth has been bypassed, authenticate user
47
+ @client_id, @token = @resource.create_token
48
+ @resource.save!
49
+ update_auth_header
50
+ else
51
+ # user will require email authentication
52
+ @resource.send_confirmation_instructions(
53
+ client_config: params[:config_name],
54
+ redirect_url: @redirect_url
55
+ )
56
+ end
57
+
58
+ render_create_success
59
+ else
60
+ clean_up_passwords @resource
61
+ render_create_error
62
+ end
63
+ end
64
+
65
+ def update
66
+ if @resource
67
+ if @resource.send(resource_update_method, account_update_params)
68
+ yield @resource if block_given?
69
+ render_update_success
70
+ else
71
+ render_update_error
72
+ end
73
+ else
74
+ render_update_error_user_not_found
75
+ end
76
+ end
77
+
78
+ def destroy
79
+ if @resource
80
+ @resource.destroy
81
+ yield @resource if block_given?
82
+ render_destroy_success
83
+ else
84
+ render_destroy_error
85
+ end
86
+ end
87
+
88
+ def sign_up_params
89
+ params.permit(*params_for_resource(:sign_up))
90
+ end
91
+
92
+ def account_update_params
93
+ params.permit(*params_for_resource(:account_update))
94
+ end
95
+
96
+ protected
97
+
98
+ def build_resource
99
+ @resource = resource_class.new(sign_up_params)
100
+ @resource.provider = provider
101
+
102
+ # honor devise configuration for case_insensitive_keys
103
+ if resource_class.case_insensitive_keys.include?(:email)
104
+ @resource.email = sign_up_params[:email].try(:downcase)
105
+ else
106
+ @resource.email = sign_up_params[:email]
107
+ end
108
+ end
109
+
110
+ def render_create_error_missing_confirm_success_url
111
+ response = {
112
+ status: 'error',
113
+ data: resource_data
114
+ }
115
+ message = I18n.t('devise_token_auth.registrations.missing_confirm_success_url')
116
+ render_error(422, message, response)
117
+ end
118
+
119
+ def render_create_error_redirect_url_not_allowed
120
+ response = {
121
+ status: 'error',
122
+ data: resource_data
123
+ }
124
+ message = I18n.t('devise_token_auth.registrations.redirect_url_not_allowed', redirect_url: @redirect_url)
125
+ render_error(422, message, response)
126
+ end
127
+
128
+ def render_create_success
129
+ render json: {
130
+ status: 'success',
131
+ data: resource_data
132
+ }
133
+ end
134
+
135
+ def render_create_error
136
+ render json: {
137
+ status: 'error',
138
+ data: resource_data,
139
+ errors: resource_errors
140
+ }, status: 422
141
+ end
142
+
143
+ def render_update_success
144
+ render json: {
145
+ status: 'success',
146
+ data: resource_data
147
+ }
148
+ end
149
+
150
+ def render_update_error
151
+ render json: {
152
+ status: 'error',
153
+ errors: resource_errors
154
+ }, status: 422
155
+ end
156
+
157
+ def render_update_error_user_not_found
158
+ render_error(404, I18n.t('devise_token_auth.registrations.user_not_found'), status: 'error')
159
+ end
160
+
161
+ def render_destroy_success
162
+ render json: {
163
+ status: 'success',
164
+ message: I18n.t('devise_token_auth.registrations.account_with_uid_destroyed', uid: @resource.uid)
165
+ }
166
+ end
167
+
168
+ def render_destroy_error
169
+ render_error(404, I18n.t('devise_token_auth.registrations.account_to_destroy_not_found'), status: 'error')
170
+ end
171
+
172
+ private
173
+
174
+ def resource_update_method
175
+ if DeviseTokenAuth.check_current_password_before_update == :attributes
176
+ 'update_with_password'
177
+ elsif DeviseTokenAuth.check_current_password_before_update == :password && account_update_params.key?(:password)
178
+ 'update_with_password'
179
+ elsif account_update_params.key?(:current_password)
180
+ 'update_with_password'
181
+ else
182
+ 'update_attributes'
183
+ end
184
+ end
185
+
186
+ def validate_sign_up_params
187
+ validate_post_data sign_up_params, I18n.t('errors.messages.validate_sign_up_params')
188
+ end
189
+
190
+ def validate_account_update_params
191
+ validate_post_data account_update_params, I18n.t('errors.messages.validate_account_update_params')
192
+ end
193
+
194
+ def validate_post_data which, message
195
+ render_error(:unprocessable_entity, message, status: 'error') if which.empty?
196
+ end
197
+ end
198
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ # see http://www.emilsoman.com/blog/2013/05/18/building-a-tested/
4
+ module DeviseTokenAuth
5
+ class SessionsController < DeviseTokenAuth::ApplicationController
6
+ before_action :set_user_by_token, only: [:destroy]
7
+ after_action :reset_session, only: [:destroy]
8
+
9
+ def new
10
+ render_new_error
11
+ end
12
+
13
+ def create
14
+ # Check
15
+ field = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys).first
16
+
17
+ @resource = nil
18
+ if field
19
+ q_value = get_case_insensitive_field_from_resource_params(field)
20
+
21
+ @resource = find_resource(field, q_value)
22
+ end
23
+
24
+ if @resource && valid_params?(field, q_value) && (!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
25
+ valid_password = @resource.valid_password?(resource_params[:password])
26
+ if (@resource.respond_to?(:valid_for_authentication?) && !@resource.valid_for_authentication? { valid_password }) || !valid_password
27
+ return render_create_error_bad_credentials
28
+ end
29
+ @client_id, @token = @resource.create_token
30
+ @resource.save
31
+
32
+ sign_in(:user, @resource, store: false, bypass: false)
33
+
34
+ yield @resource if block_given?
35
+
36
+ render_create_success
37
+ elsif @resource && !(!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
38
+ if @resource.respond_to?(:locked_at) && @resource.locked_at
39
+ render_create_error_account_locked
40
+ else
41
+ render_create_error_not_confirmed
42
+ end
43
+ else
44
+ render_create_error_bad_credentials
45
+ end
46
+ end
47
+
48
+ def destroy
49
+ # remove auth instance variables so that after_action does not run
50
+ user = remove_instance_variable(:@resource) if @resource
51
+ client_id = remove_instance_variable(:@client_id) if @client_id
52
+ remove_instance_variable(:@token) if @token
53
+
54
+ if user && client_id && user.tokens[client_id]
55
+ user.tokens.delete(client_id)
56
+ user.save!
57
+
58
+ yield user if block_given?
59
+
60
+ render_destroy_success
61
+ else
62
+ render_destroy_error
63
+ end
64
+ end
65
+
66
+ protected
67
+
68
+ def valid_params?(key, val)
69
+ resource_params[:password] && key && val
70
+ end
71
+
72
+ def get_auth_params
73
+ auth_key = nil
74
+ auth_val = nil
75
+
76
+ # iterate thru allowed auth keys, use first found
77
+ resource_class.authentication_keys.each do |k|
78
+ if resource_params[k]
79
+ auth_val = resource_params[k]
80
+ auth_key = k
81
+ break
82
+ end
83
+ end
84
+
85
+ # honor devise configuration for case_insensitive_keys
86
+ if resource_class.case_insensitive_keys.include?(auth_key)
87
+ auth_val.downcase!
88
+ end
89
+
90
+ { key: auth_key, val: auth_val }
91
+ end
92
+
93
+ def render_new_error
94
+ render_error(405, I18n.t('devise_token_auth.sessions.not_supported'))
95
+ end
96
+
97
+ def render_create_success
98
+ render json: {
99
+ data: resource_data(resource_json: @resource.token_validation_response)
100
+ }
101
+ end
102
+
103
+ def render_create_error_not_confirmed
104
+ render_error(401, I18n.t('devise_token_auth.sessions.not_confirmed', email: @resource.email))
105
+ end
106
+
107
+ def render_create_error_account_locked
108
+ render_error(401, I18n.t('devise.mailer.unlock_instructions.account_lock_msg'))
109
+ end
110
+
111
+ def render_create_error_bad_credentials
112
+ render_error(401, I18n.t('devise_token_auth.sessions.bad_credentials'))
113
+ end
114
+
115
+ def render_destroy_success
116
+ render json: {
117
+ success:true
118
+ }, status: 200
119
+ end
120
+
121
+ def render_destroy_error
122
+ render_error(404, I18n.t('devise_token_auth.sessions.user_not_found'))
123
+ end
124
+
125
+ private
126
+
127
+ def resource_params
128
+ params.permit(*params_for_resource(:sign_in))
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeviseTokenAuth
4
+ class TokenValidationsController < DeviseTokenAuth::ApplicationController
5
+ skip_before_action :assert_is_devise_resource!, only: [:validate_token]
6
+ before_action :set_user_by_token, only: [:validate_token]
7
+
8
+ def validate_token
9
+ # @resource will have been set by set_user_by_token concern
10
+ if @resource
11
+ yield @resource if block_given?
12
+ render_validate_token_success
13
+ else
14
+ render_validate_token_error
15
+ end
16
+ end
17
+
18
+ protected
19
+
20
+ def render_validate_token_success
21
+ render json: {
22
+ success: true,
23
+ data: resource_data(resource_json: @resource.token_validation_response)
24
+ }
25
+ end
26
+
27
+ def render_validate_token_error
28
+ render_error(401, I18n.t('devise_token_auth.token_validations.invalid'))
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeviseTokenAuth
4
+ class UnlocksController < DeviseTokenAuth::ApplicationController
5
+ skip_after_action :update_auth_header, only: [:create, :show]
6
+
7
+ # this action is responsible for generating unlock tokens and
8
+ # sending emails
9
+ def create
10
+ return render_create_error_missing_email unless resource_params[:email]
11
+
12
+ @email = get_case_insensitive_field_from_resource_params(:email)
13
+ @resource = find_resource(:email, @email)
14
+
15
+ if @resource
16
+ yield @resource if block_given?
17
+
18
+ @resource.send_unlock_instructions(
19
+ email: @email,
20
+ provider: 'email',
21
+ client_config: params[:config_name]
22
+ )
23
+
24
+ if @resource.errors.empty?
25
+ return render_create_success
26
+ else
27
+ render_create_error @resource.errors
28
+ end
29
+ else
30
+ render_not_found_error
31
+ end
32
+ end
33
+
34
+ def show
35
+ @resource = resource_class.unlock_access_by_token(params[:unlock_token])
36
+
37
+ if @resource.persisted?
38
+ client_id, token = @resource.create_token
39
+ @resource.save!
40
+ yield @resource if block_given?
41
+
42
+ redirect_header_options = { unlock: true }
43
+ redirect_headers = build_redirect_headers(token,
44
+ client_id,
45
+ redirect_header_options)
46
+ redirect_to(@resource.build_auth_url(after_unlock_path_for(@resource),
47
+ redirect_headers))
48
+ else
49
+ render_show_error
50
+ end
51
+ end
52
+
53
+ private
54
+ def after_unlock_path_for(resource)
55
+ #TODO: This should probably be a configuration option at the very least.
56
+ '/'
57
+ end
58
+
59
+ def render_create_error_missing_email
60
+ render_error(401, I18n.t('devise_token_auth.unlocks.missing_email'))
61
+ end
62
+
63
+ def render_create_success
64
+ render json: {
65
+ success: true,
66
+ message: I18n.t('devise_token_auth.unlocks.sended', email: @email)
67
+ }
68
+ end
69
+
70
+ def render_create_error(errors)
71
+ render json: {
72
+ success: false,
73
+ errors: errors
74
+ }, status: 400
75
+ end
76
+
77
+ def render_show_error
78
+ raise ActionController::RoutingError, 'Not Found'
79
+ end
80
+
81
+ def render_not_found_error
82
+ render_error(404, I18n.t('devise_token_auth.unlocks.user_not_found', email: @email))
83
+ end
84
+
85
+ def resource_params
86
+ params.permit(:email, :unlock_token, :config)
87
+ end
88
+ end
89
+ end