rails_jwt_auth 1.5.0 → 1.7.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c14c4018d6e871d503f3573886bdc7822a620859aef7b32f0a719a2252b5aa8
4
- data.tar.gz: 4d4e2c16de9787f6c7b940274a3583e112b81cd438623528e0b4cdbc5840b6a5
3
+ metadata.gz: 6c5dc21f39ff43ad410a4bc4060a5a027ec011f0a632f4b03244ce99852a88ee
4
+ data.tar.gz: abd710caa09455b2471b7138b59b33c92707c1d396bd78ca8fd8713def4ad5dc
5
5
  SHA512:
6
- metadata.gz: 6c4caceaa1fdcb313994525a8d734df570202beccd88f9f76131e5d5ba46c4c551a87ac3091261e92feb46339e6a52b3fe407c4768b62f6c834508b1193713dc
7
- data.tar.gz: 427687273a0d51fc837cac6ed841363d707ef87790b1a0d52283ca33bc9a1484e16ae1c5c4691b3649687b1cd2ce9e3a83827aa61afe1757a589b416f2b1d05b
6
+ metadata.gz: 9b3165148a4d567a440e1f7d157e4a24d2d4ea027e6629a6c3ab4c1b02a8dfdbf1a3434e3fcfa31eaf27f2a0a97c3cf5c430c323a48f9ad48787491f4b195bbb
7
+ data.tar.gz: 5fbf634a2de1544f1b63871f36b86e3413b6a3d1382c89f415a0a3af7ef2c51dfa0370892ab3f76f57dc646250a7c5b840fdf1137cfcdd6e2973eaa006344d26
data/README.md CHANGED
@@ -197,6 +197,21 @@ end
197
197
 
198
198
  ## Default Controllers API
199
199
 
200
+ | Prefix | Verb | URI Pattern | Controller#Action |
201
+ | ------------ | ------ | ---------------------------- | ----------------------------------- |
202
+ | session | DELETE | /session(.:format) | rails_jwt_auth/sessions#destroy |
203
+ | | POST | /session(.:format) | rails_jwt_auth/sessions#create |
204
+ | registration | POST | /registration(.:format) | rails_jwt_auth/registrations#create |
205
+ |confirmations | POST | /confirmations(.:format) | rails_jwt_auth/confirmations#create |
206
+ | confirmation | PATCH | /confirmations/:id(.:format) | rails_jwt_auth/confirmations#update |
207
+ | | PUT | /confirmations/:id(.:format) | rails_jwt_auth/confirmations#update |
208
+ | passwords | POST | /passwords(.:format) | rails_jwt_auth/passwords#create |
209
+ | password | PATCH | /passwords/:id(.:format) | rails_jwt_auth/passwords#update |
210
+ | | PUT | /passwords/:id(.:format) | rails_jwt_auth/passwords#update |
211
+ | invitations | POST | /invitations(.:format) | rails_jwt_auth/invitations#create |
212
+ | invitation | PATCH | /invitations/:id(.:format) | rails_jwt_auth/invitations#update |
213
+ | | PUT | /invitations/:id(.:format) | rails_jwt_auth/invitations#update |
214
+
200
215
  ### Session
201
216
 
202
217
  Session api is defined by `RailsJwtAuth::SessionsController`.
@@ -255,11 +270,9 @@ It is necessary to set a value for `confirmations_url` option into `config/initi
255
270
 
256
271
  ```js
257
272
  {
258
- url: host/confirmation,
273
+ url: host/confirmations/:token,
259
274
  method: PUT
260
- data: {
261
- confirmation_token: 'token'
262
- }
275
+ data: {}
263
276
  }
264
277
  ```
265
278
 
@@ -267,7 +280,7 @@ It is necessary to set a value for `confirmations_url` option into `config/initi
267
280
 
268
281
  ```js
269
282
  {
270
- url: host/confirmation,
283
+ url: host/confirmations,
271
284
  method: POST,
272
285
  data: {
273
286
  confirmation: {
@@ -285,7 +298,7 @@ Password api is defined by `RailsJwtAuth::PasswordsController`.
285
298
 
286
299
  ```js
287
300
  {
288
- url: host/password,
301
+ url: host/passwords,
289
302
  method: POST,
290
303
  data: {
291
304
  password: {
@@ -299,10 +312,9 @@ Password api is defined by `RailsJwtAuth::PasswordsController`.
299
312
 
300
313
  ```js
301
314
  {
302
- url: host/password,
315
+ url: host/passwords/:token,
303
316
  method: PUT,
304
317
  data: {
305
- reset_password_token: 'token',
306
318
  password: {
307
319
  password: '1234',
308
320
  password_confirmation: '1234'
@@ -356,7 +368,8 @@ Unlock api is provided by `RailsJwtAuth::UnlocksController`.
356
368
  ```js
357
369
  {
358
370
  url: host/unlocks/:unlock_token,
359
- method: PUT
371
+ method: PUT,
372
+ data: {}
360
373
  }
361
374
  ```
362
375
 
@@ -9,7 +9,7 @@ module RailsJwtAuth
9
9
  end
10
10
 
11
11
  def confirmation_create_params
12
- params.require(:confirmation).permit(:email)
12
+ params.require(:confirmation).permit(RailsJwtAuth.email_field_name)
13
13
  end
14
14
 
15
15
  def session_create_params
@@ -17,7 +17,7 @@ module RailsJwtAuth
17
17
  end
18
18
 
19
19
  def password_create_params
20
- params.require(:password).permit(:email)
20
+ params.require(:password).permit(RailsJwtAuth.email_field_name)
21
21
  end
22
22
 
23
23
  def password_update_params
@@ -25,7 +25,7 @@ module RailsJwtAuth
25
25
  end
26
26
 
27
27
  def invitation_create_params
28
- params.require(:invitation).permit(:email)
28
+ params.require(:invitation).permit(RailsJwtAuth.email_field_name)
29
29
  end
30
30
 
31
31
  def invitation_update_params
@@ -4,7 +4,10 @@ module RailsJwtAuth
4
4
  include RenderHelper
5
5
 
6
6
  def create
7
- user = RailsJwtAuth.model.where(email: confirmation_create_params[:email]).first
7
+ user = RailsJwtAuth.model.where(
8
+ email: confirmation_create_params[RailsJwtAuth.email_field_name]
9
+ ).first
10
+
8
11
  return render_422(email: [{error: :not_found}]) unless user
9
12
 
10
13
  user.send_confirmation_instructions ? render_204 : render_422(user.errors.details)
@@ -4,6 +4,7 @@ module RailsJwtAuth
4
4
  include RenderHelper
5
5
 
6
6
  def create
7
+ authenticate!
7
8
  user = RailsJwtAuth.model.invite!(invitation_create_params)
8
9
  user.errors.empty? ? render_204 : render_422(user.errors.details)
9
10
  end
@@ -4,8 +4,17 @@ module RailsJwtAuth
4
4
  include RenderHelper
5
5
 
6
6
  def create
7
- user = RailsJwtAuth.model.where(email: password_create_params[:email].to_s.downcase).first
8
- return render_422(email: [{error: :not_found}]) unless user
7
+ email_field = RailsJwtAuth.email_field_name
8
+
9
+ if password_create_params[email_field].blank?
10
+ return render_422(email_field => [{error: :blank}])
11
+ end
12
+
13
+ user = RailsJwtAuth.model.where(
14
+ email_field => password_create_params[email_field].to_s.strip.downcase
15
+ ).first
16
+
17
+ return render_422(email_field => [{error: :not_found}]) unless user
9
18
 
10
19
  user.send_reset_password_instructions ? render_204 : render_422(user.errors.details)
11
20
  end
@@ -2,9 +2,13 @@ if defined?(ActionMailer)
2
2
  class RailsJwtAuth::Mailer < ApplicationMailer
3
3
  default from: RailsJwtAuth.mailer_sender
4
4
 
5
- def confirmation_instructions(user)
5
+ before_action do
6
+ @user = RailsJwtAuth.model.find(params[:user_id])
7
+ @subject = I18n.t("rails_jwt_auth.mailer.#{action_name}.subject")
8
+ end
9
+
10
+ def confirmation_instructions
6
11
  raise RailsJwtAuth::NotConfirmationsUrl unless RailsJwtAuth.confirmations_url.present?
7
- @user = user
8
12
 
9
13
  @confirmations_url = add_param_to_url(
10
14
  RailsJwtAuth.confirmations_url,
@@ -12,19 +16,15 @@ if defined?(ActionMailer)
12
16
  @user.confirmation_token
13
17
  )
14
18
 
15
- subject = I18n.t('rails_jwt_auth.mailer.confirmation_instructions.subject')
16
- mail(to: @user.unconfirmed_email || @user[RailsJwtAuth.email_field_name], subject: subject)
19
+ mail(to: @user.unconfirmed_email || @user[RailsJwtAuth.email_field_name], subject: @subject)
17
20
  end
18
21
 
19
- def email_changed(user)
20
- @user = user
21
- subject = I18n.t('rails_jwt_auth.mailer.email_changed.subject')
22
- mail(to: @user[RailsJwtAuth.email_field_name!], subject: subject)
22
+ def email_changed
23
+ mail(to: @user[RailsJwtAuth.email_field_name!], subject: @subject)
23
24
  end
24
25
 
25
- def reset_password_instructions(user)
26
+ def reset_password_instructions
26
27
  raise RailsJwtAuth::NotResetPasswordsUrl unless RailsJwtAuth.reset_passwords_url.present?
27
- @user = user
28
28
 
29
29
  @reset_passwords_url = add_param_to_url(
30
30
  RailsJwtAuth.reset_passwords_url,
@@ -32,13 +32,11 @@ if defined?(ActionMailer)
32
32
  @user.reset_password_token
33
33
  )
34
34
 
35
- subject = I18n.t('rails_jwt_auth.mailer.reset_password_instructions.subject')
36
- mail(to: @user[RailsJwtAuth.email_field_name], subject: subject)
35
+ mail(to: @user[RailsJwtAuth.email_field_name], subject: @subject)
37
36
  end
38
37
 
39
- def set_password_instructions(user)
38
+ def set_password_instructions
40
39
  raise RailsJwtAuth::NotSetPasswordsUrl unless RailsJwtAuth.set_passwords_url.present?
41
- @user = user
42
40
 
43
41
  @reset_passwords_url = add_param_to_url(
44
42
  RailsJwtAuth.set_passwords_url,
@@ -46,13 +44,11 @@ if defined?(ActionMailer)
46
44
  @user.reset_password_token
47
45
  )
48
46
 
49
- subject = I18n.t('rails_jwt_auth.mailer.set_password_instructions.subject')
50
- mail(to: @user[RailsJwtAuth.email_field_name], subject: subject)
47
+ mail(to: @user[RailsJwtAuth.email_field_name], subject: @subject)
51
48
  end
52
49
 
53
- def send_invitation(user)
50
+ def send_invitation
54
51
  raise RailsJwtAuth::NotInvitationsUrl unless RailsJwtAuth.invitations_url.present?
55
- @user = user
56
52
 
57
53
  @invitations_url = add_param_to_url(
58
54
  RailsJwtAuth.invitations_url,
@@ -60,17 +56,13 @@ if defined?(ActionMailer)
60
56
  @user.invitation_token
61
57
  )
62
58
 
63
- subject = I18n.t('rails_jwt_auth.mailer.send_invitation.subject')
64
- mail(to: @user[RailsJwtAuth.email_field_name], subject: subject)
59
+ mail(to: @user[RailsJwtAuth.email_field_name], subject: @subject)
65
60
  end
66
61
 
67
- def send_unlock_instructions(user)
68
- @user = user
69
- subject = I18n.t('rails_jwt_auth.mailer.send_unlock_instructions.subject')
70
-
62
+ def send_unlock_instructions
71
63
  @unlock_url = add_param_to_url(RailsJwtAuth.unlock_url, 'unlock_token', @user.unlock_token)
72
64
 
73
- mail(to: @user[RailsJwtAuth.email_field_name], subject: subject)
65
+ mail(to: @user[RailsJwtAuth.email_field_name], subject: @subject)
74
66
  end
75
67
 
76
68
  protected
@@ -46,8 +46,10 @@ module RailsJwtAuth
46
46
  'invalid'
47
47
  end
48
48
 
49
- # abort reset password if exists to allow save
50
- self.reset_password_token = self.reset_password_sent_at = nil if reset_password_token
49
+ # if recoberable module is enabled ensure clean recovery to allow save
50
+ if self.respond_to? :reset_password_token
51
+ self.reset_password_token = self.reset_password_sent_at = nil
52
+ end
51
53
 
52
54
  assign_attributes(params)
53
55
  valid? # validates first other fields
@@ -33,13 +33,19 @@ module RailsJwtAuth
33
33
 
34
34
  self.confirmation_token = SecureRandom.base58(24)
35
35
  self.confirmation_sent_at = Time.current
36
+ end
37
+ end
36
38
 
37
- mailer = Mailer.confirmation_instructions(self)
38
- RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
39
-
40
- if RailsJwtAuth.send_email_changed_notification
41
- mailer = Mailer.email_changed(self)
42
- RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
39
+ if defined?(ActiveRecord) && ancestors.include?(ActiveRecord::Base)
40
+ after_commit do
41
+ if unconfirmed_email && saved_change_to_unconfirmed_email?
42
+ deliver_email_changed_emails
43
+ end
44
+ end
45
+ elsif defined?(Mongoid) && ancestors.include?(Mongoid::Document)
46
+ after_update do
47
+ if unconfirmed_email && unconfirmed_email_changed?
48
+ deliver_email_changed_emails
43
49
  end
44
50
  end
45
51
  end
@@ -58,8 +64,7 @@ module RailsJwtAuth
58
64
  self.confirmation_sent_at = Time.current
59
65
  return false unless save
60
66
 
61
- mailer = Mailer.confirmation_instructions(self)
62
- RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
67
+ RailsJwtAuth.send_email(:confirmation_instructions, self)
63
68
  true
64
69
  end
65
70
 
@@ -72,9 +77,15 @@ module RailsJwtAuth
72
77
  self.confirmation_token = nil
73
78
 
74
79
  if unconfirmed_email
75
- self[RailsJwtAuth.email_field_name!] = unconfirmed_email
76
- self.email_confirmation = unconfirmed_email if respond_to?(:email_confirmation)
80
+ email_field = RailsJwtAuth.email_field_name!
81
+
82
+ self[email_field] = unconfirmed_email
77
83
  self.unconfirmed_email = nil
84
+
85
+ # supports email confirmation attr_accessor validation
86
+ if respond_to?("#{email_field}_confirmation")
87
+ instance_variable_set("@#{email_field}_confirmation", self[email_field])
88
+ end
78
89
  end
79
90
 
80
91
  save
@@ -89,6 +100,7 @@ module RailsJwtAuth
89
100
 
90
101
  def validate_confirmation
91
102
  return true unless confirmed_at
103
+
92
104
  email_field = RailsJwtAuth.email_field_name!
93
105
 
94
106
  if confirmed_at_was && !public_send("#{email_field}_changed?")
@@ -98,5 +110,15 @@ module RailsJwtAuth
98
110
  errors.add(:confirmation_token, :expired)
99
111
  end
100
112
  end
113
+
114
+ def deliver_email_changed_emails
115
+ # send confirmation to new email
116
+ RailsJwtAuth.send_email(:confirmation_instructions, self)
117
+
118
+ # send notify to old email
119
+ if RailsJwtAuth.send_email_changed_notification
120
+ RailsJwtAuth.send_email(:email_changed, self)
121
+ end
122
+ end
101
123
  end
102
124
  end
@@ -112,9 +112,8 @@ module RailsJwtAuth
112
112
  end
113
113
 
114
114
  def send_invitation_mail
115
- RailsJwtAuth.email_field_name! # ensure email field es valid
116
- mailer = Mailer.send_invitation(self)
117
- RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
115
+ RailsJwtAuth.email_field_name! # ensure email field is valid
116
+ RailsJwtAuth.send_email(:send_invitation, self)
118
117
  end
119
118
 
120
119
  def invitation_period_valid?
@@ -68,8 +68,7 @@ module RailsJwtAuth
68
68
  self.unlock_token = SecureRandom.base58(24)
69
69
  save(validate: false)
70
70
 
71
- mailer = Mailer.send_unlock_instructions(self)
72
- RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
71
+ RailsJwtAuth.send_email(:send_unlock_instructions, self)
73
72
  end
74
73
 
75
74
  def access_locked?
@@ -40,8 +40,7 @@ module RailsJwtAuth
40
40
  self.reset_password_sent_at = Time.current
41
41
  return false unless save
42
42
 
43
- mailer = Mailer.reset_password_instructions(self)
44
- RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
43
+ RailsJwtAuth.send_email(:reset_password_instructions, self)
45
44
  end
46
45
 
47
46
  def set_and_send_password_instructions
@@ -56,8 +55,7 @@ module RailsJwtAuth
56
55
  self.reset_password_sent_at = Time.current
57
56
  return false unless save
58
57
 
59
- mailer = Mailer.set_password_instructions(self)
60
- RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
58
+ RailsJwtAuth.send_email(:set_password_instructions, self)
61
59
  true
62
60
  end
63
61
 
@@ -114,4 +114,9 @@ module RailsJwtAuth
114
114
 
115
115
  field_name
116
116
  end
117
+
118
+ def self.send_email(method, user)
119
+ mailer = RailsJwtAuth::Mailer.with(user_id: user.id.to_s).public_send(method)
120
+ RailsJwtAuth.deliver_later ? mailer.deliver_later : mailer.deliver
121
+ end
117
122
  end
@@ -1,3 +1,3 @@
1
1
  module RailsJwtAuth
2
- VERSION = '1.5.0'
2
+ VERSION = '1.7.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_jwt_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - rjurado
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-06 00:00:00.000000000 Z
11
+ date: 2020-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcrypt
@@ -120,8 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  - !ruby/object:Gem::Version
121
121
  version: '0'
122
122
  requirements: []
123
- rubyforge_project:
124
- rubygems_version: 2.7.3
123
+ rubygems_version: 3.0.3
125
124
  signing_key:
126
125
  specification_version: 4
127
126
  summary: Rails jwt authentication.