cognito_rails 1.6.1 → 1.7.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: 4a632d6d69bb5fe154466be5a348d85be21fbdc4724d8d36e8f6277d3c25272a
4
- data.tar.gz: 80cfe3e7dff4e66bbddb1216490661fc057777248e25be73b142d9e727263888
3
+ metadata.gz: 3c56a423a7d1738a9b7a521d4a9856c6abf1f66abbe8ae743963a115cc9b4cf8
4
+ data.tar.gz: eb6a06e28793e1306a77c68c26884625eca2005ac4017895be317a8571db30af
5
5
  SHA512:
6
- metadata.gz: 20f84101146cfbe29e6f447e7b7eae1d08526383d8d33ea4ce98c93ea7d25b50da29464064ea023b2425e484afc874f67fb775c189df631d9b0e802047e62df7
7
- data.tar.gz: 88ffa88de959b6e6f335723ef8d423842a9bdd18b3e1ccff16791d98575e77f30ef7070eb588f7556913f4c3f7c2f600bf38c7274a70b73957b835893a52e33c
6
+ metadata.gz: dec481affcb3985407c1908297df5dccf1a2c3681f545b9c8a0c9c9ad1303ece7bb101ae0c916173fa53a8ddbc209e53fcd5fe524ad45d2fd7f8336c7b116eb6
7
+ data.tar.gz: 9a880a87e8a1247eaf4132d860aaed216ac4aa246f51cb515cfef8b865bfbe54eb9831a57f1fc06c2fd94d93e814a0770e61c1be24e899786d8ec1a3c33b6fc0
@@ -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_attribute_type
15
16
  class_attribute :_cognito_password_policy
16
17
  class_attribute :_cognito_aws_user_pool_id
17
18
  class_attribute :_cognito_aws_client_credentials
@@ -60,11 +61,12 @@ module CognitoRails
60
61
  private
61
62
 
62
63
  def sync_user!(user_data)
63
- external_id = user_data.username
64
+ attributes = user_data.respond_to?(:attributes) ? user_data.attributes : user_data.user_attributes
65
+
66
+ external_id = _cognito_resolve_attribute_type(user_data)
64
67
  return if external_id.blank?
65
68
 
66
69
  user = find_or_initialize_by(_cognito_attribute_name => external_id)
67
- attributes = user_data.respond_to?(:attributes) ? user_data.attributes : user_data.user_attributes
68
70
  user.email = User.extract_cognito_attribute(attributes, :email)
69
71
  user.phone = User.extract_cognito_attribute(attributes, :phone_number) if user.respond_to?(:phone)
70
72
  _cognito_resolve_custom_attribute(user, user_data)
@@ -75,6 +77,19 @@ module CognitoRails
75
77
  user
76
78
  end
77
79
 
80
+ def _cognito_resolve_attribute_type(user_data)
81
+ attributes = user_data.respond_to?(:attributes) ? user_data.attributes : user_data.user_attributes
82
+
83
+ case _cognito_attribute_type
84
+ when :username
85
+ user_data.username
86
+ when :sub
87
+ User.extract_cognito_attribute(attributes, :sub)
88
+ else
89
+ raise "Unsupported attribute type: #{_cognito_attribute_type}"
90
+ end
91
+ end
92
+
78
93
  def _cognito_resolve_custom_attribute(user, user_data)
79
94
  _cognito_custom_attributes.each do |attribute|
80
95
  next if attribute[:value].is_a?(String)
@@ -2,5 +2,5 @@
2
2
 
3
3
  module CognitoRails
4
4
  # @return [String] gem version
5
- VERSION = '1.6.1'
5
+ VERSION = '1.7.0'
6
6
  end
data/lib/cognito_rails.rb CHANGED
@@ -25,9 +25,10 @@ module CognitoRails
25
25
  # @param user_pool_id [String]
26
26
  # @param aws_credentials [Hash,nil]
27
27
  # @return [void]
28
- def as_cognito_user(attribute_name: 'external_id', user_pool_id: nil, aws_credentials: nil)
28
+ def as_cognito_user(attribute_name: 'external_id', attribute_type: :username, user_pool_id: nil, aws_credentials: nil)
29
29
  send :include, CognitoRails::Model
30
30
  self._cognito_attribute_name = attribute_name
31
+ self._cognito_attribute_type = attribute_type
31
32
  self._cognito_aws_user_pool_id = user_pool_id
32
33
  self._cognito_aws_client_credentials = aws_credentials
33
34
 
@@ -156,7 +156,7 @@ RSpec.describe CognitoRails::User, type: :model do
156
156
 
157
157
  context '#sync_from_cognito!' do
158
158
  before do
159
- expect(fake_cognito_client).to receive(:list_users).and_return(
159
+ allow(fake_cognito_client).to receive(:list_users).and_return(
160
160
  OpenStruct.new(
161
161
  users: [
162
162
  build_cognito_user_data('some@example.com'),
@@ -179,6 +179,40 @@ RSpec.describe CognitoRails::User, type: :model do
179
179
  expect(User.pluck(:name)).to match_array(['John Doe', 'John Doe'])
180
180
  end
181
181
 
182
+ it 'resolves external_id from username when attribute_type is :username' do
183
+ allow(fake_cognito_client).to receive(:list_users).and_return(
184
+ OpenStruct.new(
185
+ users: [
186
+ build_cognito_user_data('u1@example.com', username: 'username-1'),
187
+ build_cognito_user_data('u2@example.com', username: 'username-2')
188
+ ],
189
+ pagination_token: nil
190
+ )
191
+ )
192
+
193
+ expect do
194
+ users = User.sync_from_cognito!
195
+ expect(users.map(&:external_id)).to match_array(%w[username-1 username-2])
196
+ end.to change { User.count }.by(2)
197
+ end
198
+
199
+ it 'resolves external_id from sub when attribute_type is :sub' do
200
+ allow(fake_cognito_client).to receive(:list_users).and_return(
201
+ OpenStruct.new(
202
+ users: [
203
+ build_cognito_user_data('s1@example.com', sub: 'sub-1'),
204
+ build_cognito_user_data('s2@example.com', sub: 'sub-2')
205
+ ],
206
+ pagination_token: nil
207
+ )
208
+ )
209
+
210
+ expect do
211
+ users = SubAttributeUser.sync_from_cognito!
212
+ expect(users.map(&:external_id)).to match_array(%w[sub-1 sub-2])
213
+ end.to change { SubAttributeUser.count }.by(2)
214
+ end
215
+
182
216
  it 'allows to specify a block with extra changes applied pre-save' do
183
217
  expect do
184
218
  i = 0
@@ -40,9 +40,9 @@ module CognitoRails::Helpers
40
40
  end
41
41
  end
42
42
 
43
- def build_cognito_user_data(email)
43
+ def build_cognito_user_data(email, username: SecureRandom.uuid, sub: nil)
44
44
  OpenStruct.new(
45
- username: SecureRandom.uuid,
45
+ username: username,
46
46
  user_status: 'CONFIRMED',
47
47
  enabled: true,
48
48
  user_last_modified_date: Time.now,
@@ -51,11 +51,12 @@ module CognitoRails::Helpers
51
51
  name: 'email',
52
52
  value: email
53
53
  ),
54
+ (sub ? OpenStruct.new(name: 'sub', value: sub) : nil),
54
55
  OpenStruct.new(
55
56
  name: 'custom:name',
56
57
  value: 'John Doe'
57
58
  )
58
- ],
59
+ ].compact,
59
60
  mfa_options: []
60
61
  )
61
62
  end
@@ -63,6 +63,16 @@ class PasswordProvidedUser < ActiveRecord::Base
63
63
  attr_accessor :password
64
64
  end
65
65
 
66
+ class SubAttributeUser < ActiveRecord::Base
67
+ validates :email, presence: true
68
+ validates :email, uniqueness: true
69
+
70
+ as_cognito_user(attribute_type: :sub)
71
+ cognito_verify_email
72
+
73
+ attr_accessor :password
74
+ end
75
+
66
76
  module Schema
67
77
  def self.create
68
78
  ActiveRecord::Migration.verbose = false
@@ -96,6 +106,12 @@ module Schema
96
106
  t.string "external_id", null: false
97
107
  t.timestamps null: false
98
108
  end
109
+
110
+ create_table :sub_attribute_users, force: true do |t|
111
+ t.string "email", null: false
112
+ t.string "external_id", null: false
113
+ t.timestamps null: false
114
+ end
99
115
  end
100
116
 
101
117
  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.6.1
4
+ version: 1.7.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: 2026-03-13 00:00:00.000000000 Z
11
+ date: 2026-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport