cognito_rails 1.2.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cognito_rails/model.rb +5 -0
- data/lib/cognito_rails/user.rb +30 -2
- data/lib/cognito_rails/version.rb +1 -1
- data/lib/cognito_rails.rb +1 -0
- data/spec/cognito_rails/user_spec.rb +11 -2
- data/spec/spec_helper.rb +1 -0
- data/spec/support/cognito_helpers.rb +1 -2
- data/spec/support/schema.rb +21 -0
- metadata +24 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d013a998e0deb672975845d617ee6e7bd98985e9776005574edddc60efea303
|
4
|
+
data.tar.gz: ddf9787f8bb69aff3ffcff50464fda787b9097875d9d8da165ef9074138ac4b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b943c7b79fc60f473b99bd93b52319a4f7abe01f78266ed587f94ca9daf0c74d13ed0c8d72cb44d77c14813a7509b805adb96e929d43d7f22c5ba2b127a3a55b
|
7
|
+
data.tar.gz: 7a65fadd47485c198d6a70696c9e79d52da01130b35d2fd9479e86de64784f15786b4956d4329a873335bec7db794673f86f5971c938e3a234f7f72ad06d7969
|
data/lib/cognito_rails/model.rb
CHANGED
@@ -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)
|
data/lib/cognito_rails/user.rb
CHANGED
@@ -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
|
|
data/lib/cognito_rails.rb
CHANGED
@@ -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([
|
188
|
-
expect(EnrichedUser.order(:id).pluck(:last_name)).to match_array([
|
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
|
|
data/spec/spec_helper.rb
CHANGED
@@ -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
|
-
|
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
|
data/spec/support/schema.rb
CHANGED
@@ -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.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mònade
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,20 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '9'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '6'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '9'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: aws-sdk-cognitoidentityprovider
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,6 +58,20 @@ dependencies:
|
|
58
58
|
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: ostruct
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
61
75
|
- !ruby/object:Gem::Dependency
|
62
76
|
name: rspec
|
63
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -113,7 +127,7 @@ homepage: https://rubygems.org/gems/cognito_rails
|
|
113
127
|
licenses:
|
114
128
|
- MIT
|
115
129
|
metadata: {}
|
116
|
-
post_install_message:
|
130
|
+
post_install_message:
|
117
131
|
rdoc_options: []
|
118
132
|
require_paths:
|
119
133
|
- lib
|
@@ -121,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
121
135
|
requirements:
|
122
136
|
- - ">="
|
123
137
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
138
|
+
version: 3.0.0
|
125
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
140
|
requirements:
|
127
141
|
- - ">="
|
@@ -129,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
143
|
version: '0'
|
130
144
|
requirements: []
|
131
145
|
rubygems_version: 3.4.6
|
132
|
-
signing_key:
|
146
|
+
signing_key:
|
133
147
|
specification_version: 4
|
134
148
|
summary: Add Cognito authentication to your Rails API
|
135
149
|
test_files:
|