sync_attr_with_auth0 0.0.9 → 0.0.10

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
  SHA1:
3
- metadata.gz: c5dbe1442a3af7681a771da1ce3485d163518894
4
- data.tar.gz: efd544f8895ae166faf50ef9e505d506bcd5910d
3
+ metadata.gz: 3ea59d4081c3dac909f2961737233d4449dac051
4
+ data.tar.gz: 363875b8740c8d51ce939b39946e3b4f43b10698
5
5
  SHA512:
6
- metadata.gz: 94774bc790992b01714dfff959f3aa6e15e106be382adef28f8f776a8f3ff05fe28e848f0612d147b79ba4274951ab78fa785123290afe2df4a22d5b69197124
7
- data.tar.gz: 97d1827a0db3fccfcec99b32ba1b382679ce241a210bed1620ec6eaa80943d348516763c446208de91d8f486d19fbe1dbcce084589a283955f3c2db927c7c08f
6
+ metadata.gz: b3f49ff99dac084c3d28d28ef4f0ca66055f60850d4d0c385985e1d7150b13e0f1c68a10b6d347634ed2314ec724f2350645508982c1ccd44c8f7b2674a5804f
7
+ data.tar.gz: b224370cc13a3992ec689979bbee330ca7578a44e2b14795487c75323f2fedb5fa08299c1dd7efd2d65c9481c40f99ef60d99f97a885d1a8d3fdabf284c22a46
@@ -1,6 +1,39 @@
1
1
  module SyncAttrWithAuth0
2
2
  module Auth0
3
+ require "auth0"
4
+ require "uuidtools"
3
5
 
6
+ def self.create_auth0_jwt
7
+ payload = {
8
+ 'aud' => ENV['AUTH0_CLIENT_ID'],
9
+ 'scopes' => {
10
+ 'users' => {
11
+ 'actions' => ['create', 'update', 'read']
12
+ }
13
+ },
14
+ 'iat' => Time.now.to_i,
15
+ 'jti' => UUIDTools::UUID.timestamp_create.to_s
16
+ }
17
+
18
+ jwt = JWT.encode(payload, ENV['AUTH0_CLIENT_SECRET'])
19
+
20
+ return jwt
21
+ end
22
+
23
+ def self.create_auth0_client
24
+ # v1
25
+ # auth0 = Auth0Client.new(client_id: ENV['AUTH0_CLIENT_ID'], client_secret: ENV['AUTH0_CLIENT_SECRET'], namespace: ENV['AUTH0_DOMAIN'])
26
+
27
+ # v2
28
+ auth0 = Auth0Client.new(api_version: 2, token: SyncAttrWithAuth0::Auth0.create_auth0_jwt, namespace: ENV['AUTH0_DOMAIN'])
29
+
30
+ return auth0
31
+ end
32
+
33
+ ###
34
+ # This stuff is legacy now. It's probably best to remove this stuff once the
35
+ # auth0 API stuff is working.
36
+ ###
4
37
  def self.get_access_token
5
38
  payload = {
6
39
  "client_id" => ENV['AUTH0_CLIENT_ID'],
@@ -8,6 +8,7 @@ module SyncAttrWithAuth0
8
8
 
9
9
  def sync_attr_with_auth0(options = {})
10
10
  class_attribute :auth0_uid_att
11
+ class_attribute :auth0_name_att
11
12
  class_attribute :auth0_email_att
12
13
  class_attribute :auth0_password_att
13
14
  class_attribute :auth0_email_verified_att
@@ -17,6 +18,7 @@ module SyncAttrWithAuth0
17
18
  _options = merge_default_options(options)
18
19
 
19
20
  self.auth0_uid_att = _options[:auth0_uid_att]
21
+ self.auth0_name_att = _options[:auth0_name_att]
20
22
  self.auth0_email_att = _options[:auth0_email_att]
21
23
  self.auth0_password_att = _options[:auth0_password_att]
22
24
  self.auth0_email_verified_att = _options[:auth0_email_verified_att]
@@ -33,6 +35,7 @@ module SyncAttrWithAuth0
33
35
  def merge_default_options(options)
34
36
  _options = {
35
37
  auth0_uid_att: :uid,
38
+ auth0_name_att: :name,
36
39
  auth0_email_att: :email,
37
40
  auth0_password_att: :password,
38
41
  auth0_email_verified_att: :email_verified,
@@ -54,13 +57,26 @@ module SyncAttrWithAuth0
54
57
  ok_to_validate = (self.respond_to?(:validate_with_auth0) and !self.validate_with_auth0.nil? ? self.validate_with_auth0 : true)
55
58
 
56
59
  if ok_to_validate and self.email_changed?
57
- # Get an access token
58
- access_token = SyncAttrWithAuth0::Auth0.get_access_token
59
-
60
- response = SyncAttrWithAuth0::Auth0.make_request(
61
- access_token,
62
- 'get',
63
- "/api/users?search=email:#{self.email}")
60
+ # # Get an access token
61
+ # access_token = SyncAttrWithAuth0::Auth0.get_access_token
62
+ #
63
+ # response = SyncAttrWithAuth0::Auth0.make_request(
64
+ # access_token,
65
+ # 'get',
66
+ # "/api/users?search=email:#{self.send(auth0_email_att)}")
67
+
68
+ auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client
69
+
70
+ response = auth0.users(
71
+ 1,
72
+ 0,
73
+ nil,
74
+ nil,
75
+ auth0_connection_name,
76
+ nil,
77
+ nil,
78
+ "email:#{self.send(auth0_email_att)}"
79
+ )
64
80
 
65
81
  return JSON.parse(response).empty?
66
82
  end
@@ -81,8 +97,8 @@ module SyncAttrWithAuth0
81
97
  end
82
98
 
83
99
  if ok_to_sync
84
- # Get an access token
85
- access_token = SyncAttrWithAuth0::Auth0.get_access_token
100
+ # # Get an access token
101
+ # access_token = SyncAttrWithAuth0::Auth0.get_access_token
86
102
 
87
103
  # Look for matches between what's changing
88
104
  # and what needs to be transmitted to Auth0
@@ -113,11 +129,15 @@ module SyncAttrWithAuth0
113
129
  'email_verified' => email_verified
114
130
  }.merge(changes)
115
131
 
116
- response = SyncAttrWithAuth0::Auth0.make_request(
117
- access_token,
118
- 'post',
119
- "/api/users",
120
- args)
132
+ # response = SyncAttrWithAuth0::Auth0.make_request(
133
+ # access_token,
134
+ # 'post',
135
+ # "/api/users",
136
+ # args)
137
+
138
+ auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client
139
+
140
+ response = auth0.create_user(self.send(auth0_name_att), args)
121
141
 
122
142
  response = JSON.parse(response)
123
143
 
@@ -140,8 +160,8 @@ module SyncAttrWithAuth0
140
160
  # If we find matches
141
161
  unless matches.empty?
142
162
 
143
- # Get an access token
144
- access_token = SyncAttrWithAuth0::Auth0.get_access_token
163
+ # # Get an access token
164
+ # access_token = SyncAttrWithAuth0::Auth0.get_access_token
145
165
 
146
166
  # Figure out what needs to be sent to Auth0
147
167
  changes = {}
@@ -156,46 +176,50 @@ module SyncAttrWithAuth0
156
176
 
157
177
  # Don't try to update auth0 if the user doesn't have a uid
158
178
  unless uid.nil?
159
- # Determine if the email was changed
160
- unless changes['email'].nil?
161
- email = changes.delete('email')
162
-
163
- response = SyncAttrWithAuth0::Auth0.make_request(
164
- access_token,
165
- 'put',
166
- "/api/users/#{::URI.escape(uid)}/email",
167
- {
168
- 'email' => email,
169
- 'verify' => false # If the user were to fail to verify it would create a discrepency between auth0 and the local database
170
- })
171
-
172
- response = JSON.parse(response)
173
-
174
- # Update the record with the uid
175
- self.send("#{auth0_uid_att}=", response['user_id'])
176
- self.save
177
- end
178
-
179
- # Determine if the password was changed
180
- unless changes['password'].nil?
181
- password = changes.delete('password')
182
-
183
- response = SyncAttrWithAuth0::Auth0.make_request(
184
- access_token,
185
- 'put',
186
- "/api/users/#{::URI.escape(uid)}/password",
187
- {
188
- 'password' => password,
189
- 'verify' => true
190
- })
191
- end
192
-
193
- # Patch the changes
194
- response = SyncAttrWithAuth0::Auth0.make_request(
195
- access_token,
196
- 'patch',
197
- "/api/users/#{::URI.escape(uid)}/metadata",
198
- changes)
179
+ # # Determine if the email was changed
180
+ # unless changes['email'].nil?
181
+ # email = changes.delete('email')
182
+ #
183
+ # response = SyncAttrWithAuth0::Auth0.make_request(
184
+ # access_token,
185
+ # 'put',
186
+ # "/api/users/#{::URI.escape(uid)}/email",
187
+ # {
188
+ # 'email' => email,
189
+ # 'verify' => false # If the user were to fail to verify it would create a discrepency between auth0 and the local database
190
+ # })
191
+ #
192
+ # response = JSON.parse(response)
193
+ #
194
+ # # Update the record with the uid
195
+ # self.send("#{auth0_uid_att}=", response['user_id'])
196
+ # self.save
197
+ # end
198
+ #
199
+ # # Determine if the password was changed
200
+ # unless changes['password'].nil?
201
+ # password = changes.delete('password')
202
+ #
203
+ # response = SyncAttrWithAuth0::Auth0.make_request(
204
+ # access_token,
205
+ # 'put',
206
+ # "/api/users/#{::URI.escape(uid)}/password",
207
+ # {
208
+ # 'password' => password,
209
+ # 'verify' => true
210
+ # })
211
+ # end
212
+ #
213
+ # # Patch the changes
214
+ # response = SyncAttrWithAuth0::Auth0.make_request(
215
+ # access_token,
216
+ # 'patch',
217
+ # "/api/users/#{::URI.escape(uid)}/metadata",
218
+ # changes)
219
+
220
+ auth0 = SyncAttrWithAuth0::Auth0.create_auth0_client
221
+
222
+ response = auth0.patch_user(uid, changes)
199
223
  end
200
224
 
201
225
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sync_attr_with_auth0
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick McGraw
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - '='
81
81
  - !ruby/object:Gem::Version
82
82
  version: 2.1.5
83
+ - !ruby/object:Gem::Dependency
84
+ name: auth0
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jwt
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.0
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: rails
85
113
  requirement: !ruby/object:Gem::Requirement