sync_attr_with_auth0 0.0.8 → 0.0.9
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/lib/sync_attr_with_auth0/model.rb +35 -28
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c5dbe1442a3af7681a771da1ce3485d163518894
|
4
|
+
data.tar.gz: efd544f8895ae166faf50ef9e505d506bcd5910d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94774bc790992b01714dfff959f3aa6e15e106be382adef28f8f776a8f3ff05fe28e848f0612d147b79ba4274951ab78fa785123290afe2df4a22d5b69197124
|
7
|
+
data.tar.gz: 97d1827a0db3fccfcec99b32ba1b382679ce241a210bed1620ec6eaa80943d348516763c446208de91d8f486d19fbe1dbcce084589a283955f3c2db927c7c08f
|
@@ -7,21 +7,21 @@ module SyncAttrWithAuth0
|
|
7
7
|
module ClassMethods
|
8
8
|
|
9
9
|
def sync_attr_with_auth0(options = {})
|
10
|
-
class_attribute :
|
11
|
-
class_attribute :
|
12
|
-
class_attribute :
|
13
|
-
class_attribute :
|
14
|
-
class_attribute :
|
15
|
-
class_attribute :
|
10
|
+
class_attribute :auth0_uid_att
|
11
|
+
class_attribute :auth0_email_att
|
12
|
+
class_attribute :auth0_password_att
|
13
|
+
class_attribute :auth0_email_verified_att
|
14
|
+
class_attribute :auth0_connection_name
|
15
|
+
class_attribute :auth0_sync_atts
|
16
16
|
|
17
17
|
_options = merge_default_options(options)
|
18
18
|
|
19
|
-
self.
|
20
|
-
self.
|
21
|
-
self.
|
22
|
-
self.
|
23
|
-
self.
|
24
|
-
self.
|
19
|
+
self.auth0_uid_att = _options[:auth0_uid_att]
|
20
|
+
self.auth0_email_att = _options[:auth0_email_att]
|
21
|
+
self.auth0_password_att = _options[:auth0_password_att]
|
22
|
+
self.auth0_email_verified_att = _options[:auth0_email_verified_att]
|
23
|
+
self.auth0_connection_name = _options[:auth0_connection_name]
|
24
|
+
self.auth0_sync_atts = _options[:auth0_sync_atts].collect(&:to_s)
|
25
25
|
|
26
26
|
after_validation :validate_email_with_auth0
|
27
27
|
after_create :create_user_in_auth0
|
@@ -32,12 +32,12 @@ module SyncAttrWithAuth0
|
|
32
32
|
|
33
33
|
def merge_default_options(options)
|
34
34
|
_options = {
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
auth0_uid_att: :uid,
|
36
|
+
auth0_email_att: :email,
|
37
|
+
auth0_password_att: :password,
|
38
|
+
auth0_email_verified_att: :email_verified,
|
39
|
+
auth0_connection_name: 'Username-Password-Authentication',
|
40
|
+
auth0_sync_atts: []
|
41
41
|
}
|
42
42
|
|
43
43
|
_options.merge!(options)
|
@@ -73,13 +73,20 @@ module SyncAttrWithAuth0
|
|
73
73
|
|
74
74
|
ok_to_sync = (self.respond_to?(:sync_with_auth0_on_create) and !self.sync_with_auth0_on_create.nil? ? self.sync_with_auth0_on_create : true)
|
75
75
|
|
76
|
+
# Do not create a user in auth0 if the user already has a uid from auth0
|
77
|
+
if ok_to_sync
|
78
|
+
unless self.send(auth0_uid_att).nil? or self.send(auth0_uid_att).empty?
|
79
|
+
ok_to_sync = false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
76
83
|
if ok_to_sync
|
77
84
|
# Get an access token
|
78
85
|
access_token = SyncAttrWithAuth0::Auth0.get_access_token
|
79
86
|
|
80
87
|
# Look for matches between what's changing
|
81
88
|
# and what needs to be transmitted to Auth0
|
82
|
-
matches = ( (self.class.
|
89
|
+
matches = ( (self.class.auth0_sync_atts || []) & (self.changes.keys || []) )
|
83
90
|
|
84
91
|
# Figure out what needs to be sent to Auth0
|
85
92
|
changes = {}
|
@@ -100,10 +107,10 @@ module SyncAttrWithAuth0
|
|
100
107
|
password = auth0_user_password
|
101
108
|
email_verified = auth0_email_verified?
|
102
109
|
args = {
|
103
|
-
'email' => self.send(
|
110
|
+
'email' => self.send(auth0_email_att),
|
104
111
|
'password' => password,
|
105
|
-
'connection' =>
|
106
|
-
'email_verified' =>
|
112
|
+
'connection' => auth0_connection_name,
|
113
|
+
'email_verified' => email_verified
|
107
114
|
}.merge(changes)
|
108
115
|
|
109
116
|
response = SyncAttrWithAuth0::Auth0.make_request(
|
@@ -115,7 +122,7 @@ module SyncAttrWithAuth0
|
|
115
122
|
response = JSON.parse(response)
|
116
123
|
|
117
124
|
# Update the record with the uid
|
118
|
-
self.send("#{
|
125
|
+
self.send("#{auth0_uid_att}=", response['user_id'])
|
119
126
|
self.save
|
120
127
|
end
|
121
128
|
|
@@ -128,7 +135,7 @@ module SyncAttrWithAuth0
|
|
128
135
|
if ok_to_sync
|
129
136
|
# Look for matches between what's changing
|
130
137
|
# and what needs to be transmitted to Auth0
|
131
|
-
matches = ( (self.class.
|
138
|
+
matches = ( (self.class.auth0_sync_atts || []) & (self.changes.keys || []) )
|
132
139
|
|
133
140
|
# If we find matches
|
134
141
|
unless matches.empty?
|
@@ -145,7 +152,7 @@ module SyncAttrWithAuth0
|
|
145
152
|
# If we actually have changes
|
146
153
|
unless changes.empty?
|
147
154
|
# Get the auth0 uid
|
148
|
-
uid = self.send(
|
155
|
+
uid = self.send(auth0_uid_att)
|
149
156
|
|
150
157
|
# Don't try to update auth0 if the user doesn't have a uid
|
151
158
|
unless uid.nil?
|
@@ -165,7 +172,7 @@ module SyncAttrWithAuth0
|
|
165
172
|
response = JSON.parse(response)
|
166
173
|
|
167
174
|
# Update the record with the uid
|
168
|
-
self.send("#{
|
175
|
+
self.send("#{auth0_uid_att}=", response['user_id'])
|
169
176
|
self.save
|
170
177
|
end
|
171
178
|
|
@@ -200,11 +207,11 @@ module SyncAttrWithAuth0
|
|
200
207
|
end
|
201
208
|
|
202
209
|
def auth0_user_password
|
203
|
-
self.respond_to?(
|
210
|
+
self.respond_to?(auth0_password_att) ? self.send(auth0_password_att) : auth0_default_password
|
204
211
|
end
|
205
212
|
|
206
213
|
def auth0_email_verified?
|
207
|
-
!!(self.respond_to?(
|
214
|
+
!!(self.respond_to?(auth0_email_verified_att) ? self.send(auth0_email_verified_att) : false)
|
208
215
|
end
|
209
216
|
|
210
217
|
def auth0_default_password
|