@@ -131,6 +131,18 @@ def createNewClient(auth_client, client_name, project_id):
131131
132132 return client_id
133133
134+ def createNewRedirectClient (auth_client , client_name , project_id , redirect_uri ):
135+ client_id = getClientId (auth_client , client_name , project_id )
136+
137+ client_exists = bool (client_id )
138+ if not client_exists :
139+ result = auth_client .create_client (
140+ client_name , project = project_id , public_client = False ,
141+ redirect_uris = [redirect_uri ]
142+ )
143+ client_id = result ["client" ]["id" ]
144+
145+ return client_id
134146
135147def getCredentialID (auth_client , client_id , cred_name ):
136148 get_client_cred_result = auth_client .get_client_credentials (client_id )
@@ -179,29 +191,41 @@ def validFile(file_name):
179191 return file_exists , file_empty
180192
181193
182- def getCredentialFromFile (cred_file_name , cred_id ):
194+ def getCredentialFromFile (cred_file_name , cred_id , cred_type = "setup" ):
183195 # Check to see if the local secret is the same id and not just the same
184196 # name
185197 _ , cred_empty = validFile (cred_file_name )
186198 if cred_empty is False :
187199 with open (cred_file_name , "r" ) as f :
188200 loaded_data = json .load (f )
201+ if cred_type in loaded_data .keys ():
202+ loaded_data = loaded_data [cred_type ]
203+ else :
204+ print (f"Failed to get credential from file '{ cred_file_name } ': credential type '{ cred_type } ' not found." )
189205 if loaded_data ["client" ] == cred_id :
190206 return loaded_data ["secret" ]
191207 return None
192208
193209
194- def getClientIdFromCredFile (cred_file_name ):
210+ def getClientIdFromCredFile (cred_file_name , cred_type = "setup" ):
195211 # Check to see if the local secret is the same id and not just the same
196212 # name
197213 _ , cred_empty = validFile (cred_file_name )
198214 if cred_empty is False :
199215 with open (cred_file_name , "r" ) as f :
200216 loaded_data = json .load (f )
201- return loaded_data ["client" ]
217+ if cred_type in loaded_data .keys ():
218+ loaded_data = loaded_data [cred_type ]
219+ return loaded_data ["client" ]
220+ else :
221+ print (f"Failed to get client ID from file '{ cred_file_name } ': credential type '{ cred_type } ' not found." )
202222 return None
203223
204224
225+ # Doesn't appear to be used.
226+ # Not removing until we have a chance to refactor the entire
227+ # globus configuration setup process.
228+
205229def getEndpointIdFromFile (deployment_key_file_path ):
206230 # Check to see if the local secret is the same id and not just the same
207231 # name
@@ -213,22 +237,24 @@ def getEndpointIdFromFile(deployment_key_file_path):
213237 return None
214238
215239
216- def createNewCredential (auth_client , client_id , cred_name , cred_file ):
217-
240+ def createNewCredential (auth_client , client_id , cred_name , cred_file , cred_type ):
218241 get_client_cred_result = auth_client .get_client_credentials (client_id )
219242 for cred in get_client_cred_result ["credentials" ]:
220243 # Should have stored secret locally
221244 auth_client .delete_client_credential (client_id , cred ["id" ])
222245
223246 cred_result = auth_client .create_client_credential (client_id , cred_name )
224247 # Have to change this to a dict
225- obj = {
226- "client" : cred_result ["credential" ]["client" ],
227- "id" : cred_result ["credential" ]["id" ],
228- "name" : cred_result ["credential" ]["name" ],
229- "secret" : cred_result ["credential" ]["secret" ],
230- }
231248
249+ obj = { cred_type :
250+ {
251+ "client" : cred_result ["credential" ]["client" ],
252+ "id" : cred_result ["credential" ]["id" ],
253+ "name" : cred_result ["credential" ]["name" ],
254+ "secret" : cred_result ["credential" ]["secret" ],
255+ }
256+ }
257+
232258 # Check that the folder exists
233259 folder_path = os .path .dirname (cred_file )
234260 if not os .path .exists (folder_path ):
@@ -246,9 +272,9 @@ def createNewCredential(auth_client, client_id, cred_name, cred_file):
246272 return cred_result ["credential" ]["secret" ]
247273
248274
249- def getClientSecret (auth_client , client_id , cred_name , cred_id , cred_file ):
275+ def getClientSecret (auth_client , client_id , cred_name , cred_id , cred_file , cred_type ):
250276
251- client_secret = getCredentialFromFile (cred_file , cred_id )
277+ client_secret = getCredentialFromFile (cred_file , cred_id , cred_type )
252278
253279 create_new_credential = True
254280 remove_cached_credential = True
@@ -268,23 +294,30 @@ def getClientSecret(auth_client, client_id, cred_name, cred_id, cred_file):
268294 if create_new_credential :
269295 # Remove credentials from cloud
270296 client_secret = createNewCredential (
271- auth_client , client_id , cred_name , cred_file
297+ auth_client , client_id , cred_name , cred_file , cred_type
272298 )
273299
274300 return client_secret
275301
276302
277- def createClient (auth_client , client_name , project_id , cred_name , cred_file ):
278- client_id = createNewClient (auth_client , client_name , project_id )
303+ def createClient (auth_client , client_name , project_id , cred_name , cred_file , redirect_uri = None ):
304+ if redirect_uri is not None :
305+ client_id = createNewRedirectClient (auth_client , client_name , project_id , redirect_uri )
306+ cred_type = "web"
307+ else :
308+ # if we haven't provided a redirect_uri, assume we're trying to
309+ # create a setup client, because that was previously the only
310+ # time this function was called.
311+ client_id = createNewClient (auth_client , client_name , project_id )
312+ cred_type = "setup"
279313
280314 cred_id = getCredentialID (auth_client , client_id , cred_name )
281315
282316 client_secret = getClientSecret (
283- auth_client , client_id , cred_name , cred_id , cred_file
317+ auth_client , client_id , cred_name , cred_id , cred_file , cred_type
284318 )
285319 return client_id , client_secret
286320
287-
288321def getGCSClientIDFromDeploymentFile (deployment_key_file ):
289322 deployment_key_exists , deployment_key_empty = validFile (deployment_key_file )
290323
0 commit comments