cognito_rails 1.2.0 → 1.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d8bc1e0dc872a16d7406a72e5028da49b62c80c5297d6c4428ea45efd5293a1
4
- data.tar.gz: 71d87358a0e584eba16ada273a582caa7e560b84ba58b06d6b5752b7d1769f9e
3
+ metadata.gz: bf2686f37e8a52be09eb2a043f21055fca0823015460b964e448ccc27e7a5637
4
+ data.tar.gz: d5d983d923769b5ff58325e05d5a5f942b69efe80b9e0adbcc9ca384fe3b8960
5
5
  SHA512:
6
- metadata.gz: c22e85bfa4b48b5f5614314ae5cf2f696342fdf3ca204ca92a9d23107a1097da897dcd23a67d7635f1a2cd83b8ecaacbf4bdf20bfeba785fb0c9a1328fb671e9
7
- data.tar.gz: def01df4152f2fa0e0fc3991e1438d5898af2d3485be0824f0d59e83987d65d27268897e90308e353fdc04e7b8658ff88f0e54fd92dad41d263292fed80756d9
6
+ metadata.gz: bf8e6743abb4d4a7968598b84c08a92c04b1838fdab5754e75d6157626b7f1b8106397a917799872693e0da51e0f310b3e353759b36e2c77bc6fbb0d85d321c0
7
+ data.tar.gz: bc2b2a108801f6d108d13db71b1ac7c86ab3c9bf16fbf0cb93d0bd1e59f488b16eb7009df04088eaec9ac6c68706dfb651a30db7b856e36f8a2b7ef03835bdbb
@@ -12,6 +12,7 @@ module CognitoRails
12
12
  class_attribute :_cognito_verify_phone
13
13
  class_attribute :_cognito_custom_attributes
14
14
  class_attribute :_cognito_attribute_name
15
+ class_attribute :_cognito_password_policy
15
16
  self._cognito_custom_attributes = []
16
17
 
17
18
  before_create do
@@ -142,6 +143,10 @@ module CognitoRails
142
143
  self._cognito_verify_phone = true
143
144
  end
144
145
 
146
+ def cognito_password_policy(type)
147
+ self._cognito_password_policy = type
148
+ end
149
+
145
150
  # @param name [String] attribute name
146
151
  # @param value [String] attribute name
147
152
  def define_cognito_attribute(name, value)
@@ -171,6 +171,11 @@ module CognitoRails
171
171
  user_class._cognito_verify_phone
172
172
  end
173
173
 
174
+ # @return [Symbol] :temporary | :user_provided
175
+ def cognito_password_policy
176
+ user_class._cognito_password_policy || :temporary
177
+ end
178
+
174
179
  # @return [Array<Hash>]
175
180
  def general_user_attributes
176
181
  [
@@ -188,18 +193,41 @@ module CognitoRails
188
193
  ]
189
194
  end
190
195
 
196
+ # @return [Array<Hash>]
197
+ def password_attributes
198
+ if cognito_password_policy == :user_provided
199
+ { message_action: 'SUPPRESS' }
200
+ else
201
+ { temporary_password: password }
202
+ end
203
+ end
204
+
205
+ def set_user_provided_password
206
+ cognito_client.admin_set_user_password(
207
+ {
208
+ user_pool_id: CognitoRails::Config.aws_user_pool_id,
209
+ username: email,
210
+ password: password,
211
+ permanent: true
212
+ }
213
+ )
214
+ end
215
+
191
216
  def save_for_create
192
217
  resp = cognito_client.admin_create_user(
193
218
  {
194
219
  user_pool_id: CognitoRails::Config.aws_user_pool_id,
195
220
  username: email,
196
- temporary_password: password,
197
221
  user_attributes: [
198
222
  *general_user_attributes,
199
223
  *verify_user_attributes
200
- ]
224
+ ],
225
+ **password_attributes
201
226
  }
202
227
  )
228
+
229
+ set_user_provided_password if cognito_password_policy == :user_provided
230
+
203
231
  self.id = resp.user.attributes.find { |a| a[:name] == 'sub' }[:value]
204
232
  end
205
233
 
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CognitoRails
4
4
  # @return [String] gem version
5
- VERSION = '1.2.0'
5
+ VERSION = '1.3.0'
6
6
  end
@@ -7,6 +7,7 @@ RSpec.describe CognitoRails::User, type: :model do
7
7
 
8
8
  let(:sample_cognito_email) { 'some@mail.com' }
9
9
  let(:sample_cognito_phone) { '123456789' }
10
+ let(:sample_cognito_password) { '123qweASD!@#' }
10
11
 
11
12
  it 'validates email presence' do
12
13
  expect(subject).to have(1).error_on(:email)
@@ -136,6 +137,14 @@ RSpec.describe CognitoRails::User, type: :model do
136
137
 
137
138
  User.create!(email: sample_cognito_email, name: 'TestName')
138
139
  end
140
+
141
+ it 'creates a cognito user with user_provided' do
142
+ expect(fake_cognito_client).to receive(:admin_set_user_password).exactly(1).time.and_return(OpenStruct.new)
143
+
144
+ allow_any_instance_of(CognitoRails::User).to receive(:cognito_client).and_return(fake_cognito_client)
145
+ PasswordProvidedUser.create!(email: sample_cognito_email, password: sample_cognito_password)
146
+ User.create!(email: sample_cognito_email)
147
+ end
139
148
  end
140
149
 
141
150
  context 'class methods' do
@@ -184,8 +193,8 @@ RSpec.describe CognitoRails::User, type: :model do
184
193
  end.to change { EnrichedUser.count }.by(2)
185
194
 
186
195
  expect(EnrichedUser.pluck(:email)).to match_array(['some@example.com', 'some2@example.com'])
187
- expect(EnrichedUser.order(:id).pluck(:first_name)).to match_array(['John1', 'John2'])
188
- expect(EnrichedUser.order(:id).pluck(:last_name)).to match_array(['Doe', 'Doe'])
196
+ expect(EnrichedUser.order(:id).pluck(:first_name)).to match_array(%w[John1 John2])
197
+ expect(EnrichedUser.order(:id).pluck(:last_name)).to match_array(%w[Doe Doe])
189
198
  end
190
199
  end
191
200
 
@@ -11,8 +11,7 @@ module CognitoRails::Helpers
11
11
  expect(params).to match_structure(
12
12
  user_pool_id: one_of(String, nil),
13
13
  username: String,
14
- temporary_password: String,
15
- user_attributes: a_list_of(name: String, value: one_of(String, nil))
14
+ user_attributes: a_list_of(name: String, value: one_of(String, nil)),
16
15
  )
17
16
  OpenStruct.new(user: OpenStruct.new(attributes: [{ name: 'sub', value: sample_cognito_id }]))
18
17
  end
@@ -42,6 +42,20 @@ class Admin < ActiveRecord::Base
42
42
  define_cognito_attribute 'role', 'admin'
43
43
  end
44
44
 
45
+
46
+ class PasswordProvidedUser < ActiveRecord::Base
47
+ validates :email, presence: true
48
+ validates :email, uniqueness: true
49
+
50
+ as_cognito_user
51
+ cognito_verify_email
52
+ cognito_password_policy :user_provided
53
+ define_cognito_attribute 'role', 'user'
54
+ define_cognito_attribute 'name', :name
55
+
56
+ attr_accessor :password
57
+ end
58
+
45
59
  module Schema
46
60
  def self.create
47
61
  ActiveRecord::Migration.verbose = false
@@ -68,6 +82,13 @@ module Schema
68
82
  t.string "cognito_id", null: false
69
83
  t.timestamps null: false
70
84
  end
85
+
86
+ create_table :password_provided_users, force: true do |t|
87
+ t.string "email", null: false
88
+ t.string "name"
89
+ t.string "external_id", null: false
90
+ t.timestamps null: false
91
+ end
71
92
  end
72
93
 
73
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cognito_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mònade
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-23 00:00:00.000000000 Z
11
+ date: 2023-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport