@@ -161,11 +161,7 @@ class GoogleAuth(ApiAttributeMixin, object):
161
161
"revoke_uri" ,
162
162
"redirect_uri" ,
163
163
]
164
- SERVICE_CONFIGS_LIST = [
165
- "client_service_email" ,
166
- "client_user_email" ,
167
- "client_pkcs12_file_path" ,
168
- ]
164
+ SERVICE_CONFIGS_LIST = ["client_user_email" ]
169
165
settings = ApiAttribute ("settings" )
170
166
client_config = ApiAttribute ("client_config" )
171
167
flow = ApiAttribute ("flow" )
@@ -296,14 +292,21 @@ def ServiceAuth(self):
296
292
if set (self .SERVICE_CONFIGS_LIST ) - set (self .client_config ):
297
293
self .LoadServiceConfigSettings ()
298
294
scopes = scopes_to_string (self .settings ["oauth_scope" ])
295
+ client_service_json = self .client_config .get ("client_json_file_path" )
296
+ if client_service_json :
297
+ self .credentials = ServiceAccountCredentials .from_json_keyfile_name (
298
+ filename = client_service_json , scopes = scopes
299
+ )
300
+ else :
301
+ service_email = self .client_config ["client_service_email" ]
302
+ file_path = self .client_config ["client_pkcs12_file_path" ]
303
+ self .credentials = ServiceAccountCredentials .from_p12_keyfile (
304
+ service_account_email = service_email ,
305
+ filename = file_path ,
306
+ scopes = scopes ,
307
+ )
308
+
299
309
user_email = self .client_config .get ("client_user_email" )
300
- service_email = self .client_config ["client_service_email" ]
301
- file_path = self .client_config ["client_pkcs12_file_path" ]
302
- self .credentials = ServiceAccountCredentials .from_p12_keyfile (
303
- service_account_email = service_email ,
304
- filename = file_path ,
305
- scopes = scopes ,
306
- )
307
310
if user_email :
308
311
self .credentials = self .credentials .create_delegated (
309
312
sub = user_email
@@ -322,6 +325,8 @@ def LoadCredentials(self, backend=None):
322
325
raise InvalidConfigError ("Please specify credential backend" )
323
326
if backend == "file" :
324
327
self .LoadCredentialsFile ()
328
+ elif backend == "json" :
329
+ self .LoadCredentialsFileJson ()
325
330
else :
326
331
raise InvalidConfigError ("Unknown save_credentials_backend" )
327
332
@@ -348,6 +353,19 @@ def LoadCredentialsFile(self, credentials_file=None):
348
353
"Credentials file cannot be symbolic link"
349
354
)
350
355
356
+ def LoadCredentialsFileJson (self , credentials_file = None ):
357
+ if credentials_file is None :
358
+ credentials_file = self .settings .get ("save_credentials_file" )
359
+ if credentials_file is None :
360
+ raise InvalidConfigError (
361
+ "Please specify credentials file to read"
362
+ )
363
+
364
+ scopes = scopes_to_string (self .settings ["oauth_scope" ])
365
+ self .credentials = ServiceAccountCredentials .from_json_keyfile_name (
366
+ filename = credentials_file , scopes = scopes
367
+ )
368
+
351
369
def SaveCredentials (self , backend = None ):
352
370
"""Saves credentials according to specified backend.
353
371
@@ -471,6 +489,21 @@ def LoadServiceConfigSettings(self):
471
489
"""Loads client configuration from settings file.
472
490
:raises: InvalidConfigError
473
491
"""
492
+ for file_format in ["json" , "pkcs12" ]:
493
+ config = f"client_{ file_format } _file_path"
494
+ value = self .settings ["service_config" ].get (config )
495
+ if value :
496
+ self .client_config [config ] = value
497
+ break
498
+ else :
499
+ raise InvalidConfigError (
500
+ "Either json or pkcs12 file path required "
501
+ "for service authentication"
502
+ )
503
+
504
+ if file_format == "pkcs12" :
505
+ self .SERVICE_CONFIGS_LIST .append ("client_service_email" )
506
+
474
507
for config in self .SERVICE_CONFIGS_LIST :
475
508
try :
476
509
self .client_config [config ] = self .settings ["service_config" ][
@@ -516,7 +549,7 @@ def GetFlow(self):
516
549
self .client_config ["client_id" ],
517
550
self .client_config ["client_secret" ],
518
551
scopes_to_string (self .settings ["oauth_scope" ]),
519
- ** constructor_kwargs
552
+ ** constructor_kwargs ,
520
553
)
521
554
if self .settings .get ("get_refresh_token" ):
522
555
self .flow .params .update (
0 commit comments