devise_token_auth 0.1.43.beta1 → 0.1.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/Rakefile +1 -0
- data/app/controllers/devise_token_auth/application_controller.rb +18 -2
- data/app/controllers/devise_token_auth/concerns/resource_finder.rb +5 -0
- data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +51 -38
- data/app/controllers/devise_token_auth/confirmations_controller.rb +3 -11
- data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +3 -14
- data/app/controllers/devise_token_auth/passwords_controller.rb +34 -55
- data/app/controllers/devise_token_auth/registrations_controller.rb +34 -46
- data/app/controllers/devise_token_auth/sessions_controller.rb +15 -24
- data/app/controllers/devise_token_auth/token_validations_controller.rb +2 -5
- data/app/controllers/devise_token_auth/unlocks_controller.rb +11 -27
- data/app/models/devise_token_auth/concerns/user.rb +60 -85
- data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +2 -1
- data/config/initializers/devise.rb +4 -4
- data/config/locales/pl.yml +10 -10
- data/config/locales/uk.yml +59 -0
- data/config/locales/vi.yml +50 -0
- data/lib/devise_token_auth/controllers/helpers.rb +1 -1
- data/lib/devise_token_auth/engine.rb +3 -1
- data/lib/devise_token_auth/version.rb +1 -1
- data/lib/generators/devise_token_auth/templates/user.rb +3 -3
- data/test/controllers/demo_user_controller_test.rb +56 -0
- data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +10 -6
- data/test/controllers/devise_token_auth/passwords_controller_test.rb +103 -1
- data/test/controllers/devise_token_auth/sessions_controller_test.rb +24 -1
- data/test/dummy/app/controllers/overrides/confirmations_controller.rb +1 -11
- data/test/dummy/app/controllers/overrides/passwords_controller.rb +1 -9
- data/test/dummy/app/controllers/overrides/sessions_controller.rb +1 -8
- data/test/models/user_test.rb +11 -1
- data/test/test_helper.rb +9 -0
- metadata +8 -6
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
module DeviseTokenAuth
|
|
2
2
|
class RegistrationsController < DeviseTokenAuth::ApplicationController
|
|
3
|
-
before_action :set_user_by_token, :
|
|
4
|
-
before_action :validate_sign_up_params, :
|
|
5
|
-
before_action :validate_account_update_params, :
|
|
6
|
-
skip_after_action :update_auth_header, :
|
|
3
|
+
before_action :set_user_by_token, only: [:destroy, :update]
|
|
4
|
+
before_action :validate_sign_up_params, only: :create
|
|
5
|
+
before_action :validate_account_update_params, only: :update
|
|
6
|
+
skip_after_action :update_auth_header, only: [:create, :destroy]
|
|
7
7
|
|
|
8
8
|
def create
|
|
9
|
-
@resource = resource_class.new(sign_up_params)
|
|
9
|
+
@resource = resource_class.new(sign_up_params.except(:confirm_success_url))
|
|
10
10
|
@resource.provider = provider
|
|
11
11
|
|
|
12
12
|
# honor devise configuration for case_insensitive_keys
|
|
@@ -17,13 +17,13 @@ module DeviseTokenAuth
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
# give redirect value from params priority
|
|
20
|
-
@redirect_url =
|
|
20
|
+
@redirect_url = sign_up_params[:confirm_success_url]
|
|
21
21
|
|
|
22
22
|
# fall back to default value if provided
|
|
23
23
|
@redirect_url ||= DeviseTokenAuth.default_confirm_success_url
|
|
24
24
|
|
|
25
25
|
# success redirect url is required
|
|
26
|
-
if
|
|
26
|
+
if confirmable_enabled? && !@redirect_url
|
|
27
27
|
return render_create_error_missing_confirm_success_url
|
|
28
28
|
end
|
|
29
29
|
|
|
@@ -54,13 +54,7 @@ module DeviseTokenAuth
|
|
|
54
54
|
|
|
55
55
|
else
|
|
56
56
|
# email auth has been bypassed, authenticate user
|
|
57
|
-
@client_id =
|
|
58
|
-
@token = SecureRandom.urlsafe_base64(nil, false)
|
|
59
|
-
|
|
60
|
-
@resource.tokens[@client_id] = {
|
|
61
|
-
token: BCrypt::Password.create(@token),
|
|
62
|
-
expiry: (Time.now + @resource.token_lifespan).to_i
|
|
63
|
-
}
|
|
57
|
+
@client_id, @token = @resource.create_token
|
|
64
58
|
|
|
65
59
|
@resource.save!
|
|
66
60
|
|
|
@@ -102,7 +96,7 @@ module DeviseTokenAuth
|
|
|
102
96
|
end
|
|
103
97
|
|
|
104
98
|
def sign_up_params
|
|
105
|
-
params.permit(*params_for_resource(:sign_up))
|
|
99
|
+
params.permit([*params_for_resource(:sign_up), :confirm_success_url])
|
|
106
100
|
end
|
|
107
101
|
|
|
108
102
|
def account_update_params
|
|
@@ -112,19 +106,21 @@ module DeviseTokenAuth
|
|
|
112
106
|
protected
|
|
113
107
|
|
|
114
108
|
def render_create_error_missing_confirm_success_url
|
|
115
|
-
|
|
109
|
+
response = {
|
|
116
110
|
status: 'error',
|
|
117
|
-
data: resource_data
|
|
118
|
-
|
|
119
|
-
|
|
111
|
+
data: resource_data
|
|
112
|
+
}
|
|
113
|
+
message = I18n.t('devise_token_auth.registrations.missing_confirm_success_url')
|
|
114
|
+
render_error(422, message, response)
|
|
120
115
|
end
|
|
121
116
|
|
|
122
117
|
def render_create_error_redirect_url_not_allowed
|
|
123
|
-
|
|
118
|
+
response = {
|
|
124
119
|
status: 'error',
|
|
125
|
-
data: resource_data
|
|
126
|
-
|
|
127
|
-
|
|
120
|
+
data: resource_data
|
|
121
|
+
}
|
|
122
|
+
message = I18n.t('devise_token_auth.registrations.redirect_url_not_allowed', redirect_url: @redirect_url)
|
|
123
|
+
render_error(422, message, response)
|
|
128
124
|
end
|
|
129
125
|
|
|
130
126
|
def render_create_success
|
|
@@ -143,11 +139,12 @@ module DeviseTokenAuth
|
|
|
143
139
|
end
|
|
144
140
|
|
|
145
141
|
def render_create_error_email_already_exists
|
|
146
|
-
|
|
142
|
+
response = {
|
|
147
143
|
status: 'error',
|
|
148
|
-
data: resource_data
|
|
149
|
-
|
|
150
|
-
|
|
144
|
+
data: resource_data
|
|
145
|
+
}
|
|
146
|
+
message = I18n.t('devise_token_auth.registrations.email_already_exists', email: @resource.email)
|
|
147
|
+
render_error(422, message, response)
|
|
151
148
|
end
|
|
152
149
|
|
|
153
150
|
def render_update_success
|
|
@@ -165,53 +162,44 @@ module DeviseTokenAuth
|
|
|
165
162
|
end
|
|
166
163
|
|
|
167
164
|
def render_update_error_user_not_found
|
|
168
|
-
|
|
169
|
-
status: 'error',
|
|
170
|
-
errors: [I18n.t("devise_token_auth.registrations.user_not_found")]
|
|
171
|
-
}, status: 404
|
|
165
|
+
render_error(404, I18n.t('devise_token_auth.registrations.user_not_found'), { status: 'error' })
|
|
172
166
|
end
|
|
173
167
|
|
|
174
168
|
def render_destroy_success
|
|
175
169
|
render json: {
|
|
176
170
|
status: 'success',
|
|
177
|
-
message: I18n.t(
|
|
171
|
+
message: I18n.t('devise_token_auth.registrations.account_with_uid_destroyed', uid: @resource.uid)
|
|
178
172
|
}
|
|
179
173
|
end
|
|
180
174
|
|
|
181
175
|
def render_destroy_error
|
|
182
|
-
|
|
183
|
-
status: 'error',
|
|
184
|
-
errors: [I18n.t("devise_token_auth.registrations.account_to_destroy_not_found")]
|
|
185
|
-
}, status: 404
|
|
176
|
+
render_error(404, I18n.t('devise_token_auth.registrations.account_to_destroy_not_found'), { status: 'error' })
|
|
186
177
|
end
|
|
187
178
|
|
|
188
179
|
private
|
|
189
180
|
|
|
190
181
|
def resource_update_method
|
|
191
182
|
if DeviseTokenAuth.check_current_password_before_update == :attributes
|
|
192
|
-
|
|
183
|
+
'update_with_password'
|
|
193
184
|
elsif DeviseTokenAuth.check_current_password_before_update == :password && account_update_params.has_key?(:password)
|
|
194
|
-
|
|
185
|
+
'update_with_password'
|
|
195
186
|
elsif account_update_params.has_key?(:current_password)
|
|
196
|
-
|
|
187
|
+
'update_with_password'
|
|
197
188
|
else
|
|
198
|
-
|
|
189
|
+
'update_attributes'
|
|
199
190
|
end
|
|
200
191
|
end
|
|
201
192
|
|
|
202
193
|
def validate_sign_up_params
|
|
203
|
-
validate_post_data sign_up_params, I18n.t(
|
|
194
|
+
validate_post_data sign_up_params, I18n.t('errors.messages.validate_sign_up_params')
|
|
204
195
|
end
|
|
205
196
|
|
|
206
197
|
def validate_account_update_params
|
|
207
|
-
validate_post_data account_update_params, I18n.t(
|
|
198
|
+
validate_post_data account_update_params, I18n.t('errors.messages.validate_account_update_params')
|
|
208
199
|
end
|
|
209
200
|
|
|
210
201
|
def validate_post_data which, message
|
|
211
|
-
|
|
212
|
-
status: 'error',
|
|
213
|
-
errors: [message]
|
|
214
|
-
}, status: :unprocessable_entity if which.empty?
|
|
202
|
+
render_error(:unprocessable_entity, message, { status: 'error' }) if which.empty?
|
|
215
203
|
end
|
|
216
204
|
end
|
|
217
205
|
end
|
|
@@ -22,17 +22,9 @@ module DeviseTokenAuth
|
|
|
22
22
|
if @resource && valid_params?(field, q_value) && (!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
|
|
23
23
|
valid_password = @resource.valid_password?(resource_params[:password])
|
|
24
24
|
if (@resource.respond_to?(:valid_for_authentication?) && !@resource.valid_for_authentication? { valid_password }) || !valid_password
|
|
25
|
-
|
|
26
|
-
return
|
|
25
|
+
return render_create_error_bad_credentials
|
|
27
26
|
end
|
|
28
|
-
|
|
29
|
-
@client_id = SecureRandom.urlsafe_base64(nil, false)
|
|
30
|
-
@token = SecureRandom.urlsafe_base64(nil, false)
|
|
31
|
-
|
|
32
|
-
@resource.tokens[@client_id] = {
|
|
33
|
-
token: BCrypt::Password.create(@token),
|
|
34
|
-
expiry: (Time.now + @resource.token_lifespan).to_i
|
|
35
|
-
}
|
|
27
|
+
@client_id, @token = @resource.create_token
|
|
36
28
|
@resource.save
|
|
37
29
|
|
|
38
30
|
sign_in(:user, @resource, store: false, bypass: false)
|
|
@@ -41,7 +33,11 @@ module DeviseTokenAuth
|
|
|
41
33
|
|
|
42
34
|
render_create_success
|
|
43
35
|
elsif @resource && !(!@resource.respond_to?(:active_for_authentication?) || @resource.active_for_authentication?)
|
|
44
|
-
|
|
36
|
+
if @resource.respond_to?(:locked_at) && @resource.locked_at
|
|
37
|
+
render_create_error_account_locked
|
|
38
|
+
else
|
|
39
|
+
render_create_error_not_confirmed
|
|
40
|
+
end
|
|
45
41
|
else
|
|
46
42
|
render_create_error_bad_credentials
|
|
47
43
|
end
|
|
@@ -96,9 +92,7 @@ module DeviseTokenAuth
|
|
|
96
92
|
end
|
|
97
93
|
|
|
98
94
|
def render_new_error
|
|
99
|
-
|
|
100
|
-
errors: [ I18n.t("devise_token_auth.sessions.not_supported")]
|
|
101
|
-
}, status: 405
|
|
95
|
+
render_error(405, I18n.t("devise_token_auth.sessions.not_supported"))
|
|
102
96
|
end
|
|
103
97
|
|
|
104
98
|
def render_create_success
|
|
@@ -108,16 +102,15 @@ module DeviseTokenAuth
|
|
|
108
102
|
end
|
|
109
103
|
|
|
110
104
|
def render_create_error_not_confirmed
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
105
|
+
render_error(401, I18n.t("devise_token_auth.sessions.not_confirmed", email: @resource.email))
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def render_create_error_account_locked
|
|
109
|
+
render_error(401, I18n.t("devise.mailer.unlock_instructions.account_lock_msg"))
|
|
115
110
|
end
|
|
116
111
|
|
|
117
112
|
def render_create_error_bad_credentials
|
|
118
|
-
|
|
119
|
-
errors: [I18n.t("devise_token_auth.sessions.bad_credentials")]
|
|
120
|
-
}, status: 401
|
|
113
|
+
render_error(401, I18n.t("devise_token_auth.sessions.bad_credentials"))
|
|
121
114
|
end
|
|
122
115
|
|
|
123
116
|
def render_destroy_success
|
|
@@ -127,9 +120,7 @@ module DeviseTokenAuth
|
|
|
127
120
|
end
|
|
128
121
|
|
|
129
122
|
def render_destroy_error
|
|
130
|
-
|
|
131
|
-
errors: [I18n.t("devise_token_auth.sessions.user_not_found")]
|
|
132
|
-
}, status: 404
|
|
123
|
+
render_error(404, I18n.t("devise_token_auth.sessions.user_not_found"))
|
|
133
124
|
end
|
|
134
125
|
|
|
135
126
|
private
|
|
@@ -4,7 +4,7 @@ module DeviseTokenAuth
|
|
|
4
4
|
before_action :set_user_by_token, :only => [:validate_token]
|
|
5
5
|
|
|
6
6
|
def validate_token
|
|
7
|
-
# @resource will have been set by
|
|
7
|
+
# @resource will have been set by set_user_by_token concern
|
|
8
8
|
if @resource
|
|
9
9
|
yield @resource if block_given?
|
|
10
10
|
render_validate_token_success
|
|
@@ -23,10 +23,7 @@ module DeviseTokenAuth
|
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def render_validate_token_error
|
|
26
|
-
|
|
27
|
-
success: false,
|
|
28
|
-
errors: [I18n.t("devise_token_auth.token_validations.invalid")]
|
|
29
|
-
}, status: 401
|
|
26
|
+
render_error(401, I18n.t("devise_token_auth.token_validations.invalid"))
|
|
30
27
|
end
|
|
31
28
|
end
|
|
32
29
|
end
|
|
@@ -12,9 +12,6 @@ module DeviseTokenAuth
|
|
|
12
12
|
@email = get_case_insensitive_field_from_resource_params(:email)
|
|
13
13
|
@resource = find_resource(:email, @email)
|
|
14
14
|
|
|
15
|
-
@errors = nil
|
|
16
|
-
@error_status = 400
|
|
17
|
-
|
|
18
15
|
if @resource
|
|
19
16
|
yield @resource if block_given?
|
|
20
17
|
|
|
@@ -27,15 +24,10 @@ module DeviseTokenAuth
|
|
|
27
24
|
if @resource.errors.empty?
|
|
28
25
|
return render_create_success
|
|
29
26
|
else
|
|
30
|
-
|
|
27
|
+
render_create_error @resource.errors
|
|
31
28
|
end
|
|
32
29
|
else
|
|
33
|
-
|
|
34
|
-
@error_status = 404
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
if @errors
|
|
38
|
-
return render_create_error
|
|
30
|
+
render_not_found_error
|
|
39
31
|
end
|
|
40
32
|
end
|
|
41
33
|
|
|
@@ -43,16 +35,7 @@ module DeviseTokenAuth
|
|
|
43
35
|
@resource = resource_class.unlock_access_by_token(params[:unlock_token])
|
|
44
36
|
|
|
45
37
|
if @resource && @resource.id
|
|
46
|
-
client_id
|
|
47
|
-
token = SecureRandom.urlsafe_base64(nil, false)
|
|
48
|
-
token_hash = BCrypt::Password.create(token)
|
|
49
|
-
expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i
|
|
50
|
-
|
|
51
|
-
@resource.tokens[client_id] = {
|
|
52
|
-
token: token_hash,
|
|
53
|
-
expiry: expiry
|
|
54
|
-
}
|
|
55
|
-
|
|
38
|
+
client_id, token = @resource.create_token
|
|
56
39
|
@resource.save!
|
|
57
40
|
yield @resource if block_given?
|
|
58
41
|
|
|
@@ -74,10 +57,7 @@ module DeviseTokenAuth
|
|
|
74
57
|
end
|
|
75
58
|
|
|
76
59
|
def render_create_error_missing_email
|
|
77
|
-
|
|
78
|
-
success: false,
|
|
79
|
-
errors: [I18n.t("devise_token_auth.unlocks.missing_email")]
|
|
80
|
-
}, status: 401
|
|
60
|
+
render_error(401, I18n.t("devise_token_auth.unlocks.missing_email"))
|
|
81
61
|
end
|
|
82
62
|
|
|
83
63
|
def render_create_success
|
|
@@ -87,17 +67,21 @@ module DeviseTokenAuth
|
|
|
87
67
|
}
|
|
88
68
|
end
|
|
89
69
|
|
|
90
|
-
def render_create_error
|
|
70
|
+
def render_create_error(errors)
|
|
91
71
|
render json: {
|
|
92
72
|
success: false,
|
|
93
|
-
errors:
|
|
94
|
-
}, status:
|
|
73
|
+
errors: errors,
|
|
74
|
+
}, status: 400
|
|
95
75
|
end
|
|
96
76
|
|
|
97
77
|
def render_show_error
|
|
98
78
|
raise ActionController::RoutingError.new('Not Found')
|
|
99
79
|
end
|
|
100
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
|
+
|
|
101
85
|
def resource_params
|
|
102
86
|
params.permit(:email, :unlock_token, :config)
|
|
103
87
|
end
|
|
@@ -42,17 +42,9 @@ module DeviseTokenAuth::Concerns::User
|
|
|
42
42
|
before_save :remove_tokens_after_password_reset
|
|
43
43
|
|
|
44
44
|
# don't use default devise email validation
|
|
45
|
-
def email_required
|
|
46
|
-
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def email_changed?
|
|
50
|
-
false
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def will_save_change_to_email?
|
|
54
|
-
false
|
|
55
|
-
end
|
|
45
|
+
def email_required?; false; end
|
|
46
|
+
def email_changed?; false; end
|
|
47
|
+
def will_save_change_to_email?; false; end
|
|
56
48
|
|
|
57
49
|
def password_required?
|
|
58
50
|
return false unless provider == 'email'
|
|
@@ -60,46 +52,34 @@ module DeviseTokenAuth::Concerns::User
|
|
|
60
52
|
end
|
|
61
53
|
|
|
62
54
|
# override devise method to include additional info as opts hash
|
|
63
|
-
def send_confirmation_instructions(opts=
|
|
64
|
-
unless @raw_confirmation_token
|
|
65
|
-
generate_confirmation_token!
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
opts ||= {}
|
|
55
|
+
def send_confirmation_instructions(opts={})
|
|
56
|
+
generate_confirmation_token! unless @raw_confirmation_token
|
|
69
57
|
|
|
70
58
|
# fall back to "default" config name
|
|
71
59
|
opts[:client_config] ||= "default"
|
|
72
|
-
|
|
73
|
-
if pending_reconfirmation?
|
|
74
|
-
opts[:to] = unconfirmed_email
|
|
75
|
-
end
|
|
60
|
+
opts[:to] = unconfirmed_email if pending_reconfirmation?
|
|
76
61
|
opts[:redirect_url] ||= DeviseTokenAuth.default_confirm_success_url
|
|
77
62
|
|
|
78
63
|
send_devise_notification(:confirmation_instructions, @raw_confirmation_token, opts)
|
|
79
64
|
end
|
|
80
65
|
|
|
81
66
|
# override devise method to include additional info as opts hash
|
|
82
|
-
def send_reset_password_instructions(opts=
|
|
67
|
+
def send_reset_password_instructions(opts={})
|
|
83
68
|
token = set_reset_password_token
|
|
84
69
|
|
|
85
|
-
opts ||= {}
|
|
86
|
-
|
|
87
70
|
# fall back to "default" config name
|
|
88
71
|
opts[:client_config] ||= "default"
|
|
89
72
|
|
|
90
73
|
send_devise_notification(:reset_password_instructions, token, opts)
|
|
91
|
-
|
|
92
74
|
token
|
|
93
75
|
end
|
|
94
76
|
|
|
95
77
|
# override devise method to include additional info as opts hash
|
|
96
|
-
def send_unlock_instructions(opts=
|
|
78
|
+
def send_unlock_instructions(opts={})
|
|
97
79
|
raw, enc = Devise.token_generator.generate(self.class, :unlock_token)
|
|
98
80
|
self.unlock_token = enc
|
|
99
81
|
save(validate: false)
|
|
100
82
|
|
|
101
|
-
opts ||= {}
|
|
102
|
-
|
|
103
83
|
# fall back to "default" config name
|
|
104
84
|
opts[:client_config] ||= "default"
|
|
105
85
|
|
|
@@ -108,10 +88,22 @@ module DeviseTokenAuth::Concerns::User
|
|
|
108
88
|
end
|
|
109
89
|
end
|
|
110
90
|
|
|
91
|
+
def create_token(client_id: nil, token: nil, expiry: nil, **token_extras)
|
|
92
|
+
client_id ||= SecureRandom.urlsafe_base64(nil, false)
|
|
93
|
+
token ||= SecureRandom.urlsafe_base64(nil, false)
|
|
94
|
+
expiry ||= (Time.now + token_lifespan).to_i
|
|
95
|
+
|
|
96
|
+
self.tokens[client_id] = {
|
|
97
|
+
token: BCrypt::Password.create(token),
|
|
98
|
+
expiry: expiry
|
|
99
|
+
}.merge!(token_extras)
|
|
100
|
+
|
|
101
|
+
[client_id, token, expiry]
|
|
102
|
+
end
|
|
103
|
+
|
|
111
104
|
module ClassMethods
|
|
112
105
|
protected
|
|
113
106
|
|
|
114
|
-
|
|
115
107
|
def tokens_has_json_column_type?
|
|
116
108
|
database_exists? && table_exists? && self.columns_hash['tokens'] && self.columns_hash['tokens'].type.in?([:json, :jsonb])
|
|
117
109
|
end
|
|
@@ -123,10 +115,7 @@ module DeviseTokenAuth::Concerns::User
|
|
|
123
115
|
|
|
124
116
|
|
|
125
117
|
def valid_token?(token, client_id='default')
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return false unless self.tokens[client_id]
|
|
129
|
-
|
|
118
|
+
return false unless tokens[client_id]
|
|
130
119
|
return true if token_is_current?(token, client_id)
|
|
131
120
|
return true if token_can_be_reused?(token, client_id)
|
|
132
121
|
|
|
@@ -137,15 +126,13 @@ module DeviseTokenAuth::Concerns::User
|
|
|
137
126
|
|
|
138
127
|
# this must be done from the controller so that additional params
|
|
139
128
|
# can be passed on from the client
|
|
140
|
-
def send_confirmation_notification
|
|
141
|
-
false
|
|
142
|
-
end
|
|
129
|
+
def send_confirmation_notification?; false; end
|
|
143
130
|
|
|
144
131
|
|
|
145
132
|
def token_is_current?(token, client_id)
|
|
146
133
|
# ghetto HashWithIndifferentAccess
|
|
147
|
-
expiry =
|
|
148
|
-
token_hash =
|
|
134
|
+
expiry = tokens[client_id]['expiry'] || tokens[client_id][:expiry]
|
|
135
|
+
token_hash = tokens[client_id]['token'] || tokens[client_id][:token]
|
|
149
136
|
|
|
150
137
|
return true if (
|
|
151
138
|
# ensure that expiry and token are set
|
|
@@ -163,9 +150,8 @@ module DeviseTokenAuth::Concerns::User
|
|
|
163
150
|
# allow batch requests to use the previous token
|
|
164
151
|
def token_can_be_reused?(token, client_id)
|
|
165
152
|
# ghetto HashWithIndifferentAccess
|
|
166
|
-
updated_at =
|
|
167
|
-
last_token =
|
|
168
|
-
|
|
153
|
+
updated_at = tokens[client_id]['updated_at'] || tokens[client_id][:updated_at]
|
|
154
|
+
last_token = tokens[client_id]['last_token'] || tokens[client_id][:last_token]
|
|
169
155
|
|
|
170
156
|
return true if (
|
|
171
157
|
# ensure that the last token and its creation time exist
|
|
@@ -182,55 +168,48 @@ module DeviseTokenAuth::Concerns::User
|
|
|
182
168
|
|
|
183
169
|
# update user's auth token (should happen on each request)
|
|
184
170
|
def create_new_auth_token(client_id=nil)
|
|
185
|
-
|
|
186
|
-
last_token ||= nil
|
|
187
|
-
token = SecureRandom.urlsafe_base64(nil, false)
|
|
188
|
-
token_hash = ::BCrypt::Password.create(token)
|
|
189
|
-
expiry = (Time.now + token_lifespan).to_i
|
|
190
|
-
|
|
191
|
-
if self.tokens[client_id] && self.tokens[client_id]['token']
|
|
192
|
-
last_token = self.tokens[client_id]['token']
|
|
193
|
-
end
|
|
171
|
+
now = Time.now
|
|
194
172
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
expiry:
|
|
198
|
-
last_token:
|
|
199
|
-
updated_at:
|
|
200
|
-
|
|
173
|
+
client_id, token = create_token(
|
|
174
|
+
client_id: client_id,
|
|
175
|
+
expiry: (now + token_lifespan).to_i,
|
|
176
|
+
last_token: tokens.fetch(client_id, {})['token'],
|
|
177
|
+
updated_at: now
|
|
178
|
+
)
|
|
201
179
|
|
|
202
|
-
|
|
180
|
+
update_auth_header(token, client_id)
|
|
203
181
|
end
|
|
204
182
|
|
|
205
|
-
|
|
206
183
|
def build_auth_header(token, client_id='default')
|
|
207
|
-
client_id ||= 'default'
|
|
208
|
-
|
|
209
184
|
# client may use expiry to prevent validation request if expired
|
|
210
185
|
# must be cast as string or headers will break
|
|
211
|
-
expiry =
|
|
186
|
+
expiry = tokens[client_id]['expiry'] || tokens[client_id][:expiry]
|
|
212
187
|
|
|
213
|
-
|
|
214
|
-
while self.tokens.keys.length > 0 && max_clients < self.tokens.keys.length
|
|
215
|
-
oldest_token = self.tokens.min_by { |cid, v| v[:expiry] || v["expiry"] }
|
|
216
|
-
self.tokens.delete(oldest_token.first)
|
|
217
|
-
end
|
|
218
|
-
|
|
219
|
-
self.save!
|
|
220
|
-
|
|
221
|
-
return {
|
|
188
|
+
{
|
|
222
189
|
DeviseTokenAuth.headers_names[:"access-token"] => token,
|
|
223
190
|
DeviseTokenAuth.headers_names[:"token-type"] => "Bearer",
|
|
224
191
|
DeviseTokenAuth.headers_names[:"client"] => client_id,
|
|
225
192
|
DeviseTokenAuth.headers_names[:"expiry"] => expiry.to_s,
|
|
226
|
-
DeviseTokenAuth.headers_names[:"uid"] =>
|
|
193
|
+
DeviseTokenAuth.headers_names[:"uid"] => uid
|
|
227
194
|
}
|
|
228
195
|
end
|
|
229
196
|
|
|
197
|
+
def update_auth_header(token, client_id='default')
|
|
198
|
+
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
|
+
|
|
204
|
+
save!
|
|
205
|
+
|
|
206
|
+
headers
|
|
207
|
+
end
|
|
208
|
+
|
|
230
209
|
|
|
231
210
|
def build_auth_url(base_url, args)
|
|
232
|
-
args[:uid] =
|
|
233
|
-
args[:expiry] =
|
|
211
|
+
args[:uid] = uid
|
|
212
|
+
args[:expiry] = tokens[args[:client_id]]['expiry']
|
|
234
213
|
|
|
235
214
|
DeviseTokenAuth::Url.generate(base_url, args)
|
|
236
215
|
end
|
|
@@ -238,18 +217,15 @@ module DeviseTokenAuth::Concerns::User
|
|
|
238
217
|
|
|
239
218
|
def extend_batch_buffer(token, client_id)
|
|
240
219
|
self.tokens[client_id]['updated_at'] = Time.now
|
|
241
|
-
|
|
242
|
-
return build_auth_header(token, client_id)
|
|
220
|
+
update_auth_header(token, client_id)
|
|
243
221
|
end
|
|
244
222
|
|
|
245
223
|
def confirmed?
|
|
246
|
-
|
|
224
|
+
devise_modules.exclude?(:confirmable) || super
|
|
247
225
|
end
|
|
248
226
|
|
|
249
227
|
def token_validation_response
|
|
250
|
-
|
|
251
|
-
:tokens, :created_at, :updated_at
|
|
252
|
-
])
|
|
228
|
+
as_json(except: [:tokens, :created_at, :updated_at])
|
|
253
229
|
end
|
|
254
230
|
|
|
255
231
|
def token_lifespan
|
|
@@ -263,8 +239,8 @@ module DeviseTokenAuth::Concerns::User
|
|
|
263
239
|
end
|
|
264
240
|
|
|
265
241
|
def destroy_expired_tokens
|
|
266
|
-
if
|
|
267
|
-
|
|
242
|
+
if tokens
|
|
243
|
+
tokens.delete_if do |cid, v|
|
|
268
244
|
expiry = v[:expiry] || v["expiry"]
|
|
269
245
|
DateTime.strptime(expiry.to_s, '%s') < Time.now
|
|
270
246
|
end
|
|
@@ -272,13 +248,12 @@ module DeviseTokenAuth::Concerns::User
|
|
|
272
248
|
end
|
|
273
249
|
|
|
274
250
|
def remove_tokens_after_password_reset
|
|
275
|
-
there_is_more_than_one_token = self.tokens && self.tokens.keys.length > 1
|
|
276
251
|
should_remove_old_tokens = DeviseTokenAuth.remove_tokens_after_password_reset &&
|
|
277
|
-
encrypted_password_changed? &&
|
|
252
|
+
encrypted_password_changed? && tokens && tokens.many?
|
|
278
253
|
|
|
279
254
|
if should_remove_old_tokens
|
|
280
|
-
|
|
281
|
-
self.tokens = {
|
|
255
|
+
client_id, token_data = tokens.max_by { |cid, v| v[:expiry] || v["expiry"] }
|
|
256
|
+
self.tokens = {client_id => token_data}
|
|
282
257
|
end
|
|
283
258
|
end
|
|
284
259
|
|