10
10
from oauth2client .client import AccessTokenRefreshError
11
11
from oauth2client .client import OAuth2WebServerFlow
12
12
from oauth2client .client import OOB_CALLBACK_URN
13
+ from oauth2client .contrib .dictionary_storage import DictionaryStorage
13
14
from oauth2client .file import Storage
14
15
from oauth2client .tools import ClientRedirectHandler
15
16
from oauth2client .tools import ClientRedirectServer
@@ -197,9 +198,9 @@ def __init__(
197
198
self .settings = settings or self .DEFAULT_SETTINGS
198
199
ValidateSettings (self .settings )
199
200
200
- self . _storages = self ._InitializeStoragesFromSettings ()
201
- # Only one (`file`) backend is supported now
202
- self ._default_storage = self . _storages [ "file" ]
201
+ storages , default = self ._InitializeStoragesFromSettings ()
202
+ self . _storages = storages
203
+ self ._default_storage = default
203
204
204
205
@property
205
206
def access_token_expired (self ):
@@ -337,7 +338,7 @@ def ServiceAuth(self):
337
338
)
338
339
339
340
def _InitializeStoragesFromSettings (self ):
340
- result = {"file" : None }
341
+ result = {"file" : None , "dictionary" : None }
341
342
backend = self .settings .get ("save_credentials_backend" )
342
343
save_credentials = self .settings .get ("save_credentials" )
343
344
if backend == "file" :
@@ -347,11 +348,16 @@ def _InitializeStoragesFromSettings(self):
347
348
"Please specify credentials file to read"
348
349
)
349
350
result [backend ] = Storage (credentials_file )
351
+ elif backend == "dictionary" :
352
+ result [backend ] = DictionaryStorage (
353
+ self .settings ["save_credentials_dict" ],
354
+ self .settings ["save_credentials_key" ],
355
+ )
350
356
elif save_credentials :
351
357
raise InvalidConfigError (
352
358
"Unknown save_credentials_backend: %s" % backend
353
359
)
354
- return result
360
+ return result , result . get ( backend )
355
361
356
362
def LoadCredentials (self , backend = None ):
357
363
"""Loads credentials or create empty credentials if it doesn't exist.
@@ -366,6 +372,8 @@ def LoadCredentials(self, backend=None):
366
372
raise InvalidConfigError ("Please specify credential backend" )
367
373
if backend == "file" :
368
374
self .LoadCredentialsFile ()
375
+ elif backend == "dictionary" :
376
+ self ._LoadCredentialsDictionary ()
369
377
else :
370
378
raise InvalidConfigError ("Unknown save_credentials_backend" )
371
379
@@ -399,6 +407,10 @@ def LoadCredentialsFile(self, credentials_file=None):
399
407
if self .credentials :
400
408
self .credentials .set_store (self ._default_storage )
401
409
410
+ def _LoadCredentialsDictionary (self ):
411
+ self ._default_storage = self ._storages ["dictionary" ]
412
+ self .credentials = self ._default_storage .get ()
413
+
402
414
def SaveCredentials (self , backend = None ):
403
415
"""Saves credentials according to specified backend.
404
416
@@ -415,6 +427,8 @@ def SaveCredentials(self, backend=None):
415
427
raise InvalidConfigError ("Please specify credential backend" )
416
428
if backend == "file" :
417
429
self .SaveCredentialsFile ()
430
+ elif backend == "dictionary" :
431
+ self ._SaveCredentialsDictionary ()
418
432
else :
419
433
raise InvalidConfigError ("Unknown save_credentials_backend" )
420
434
@@ -446,6 +460,13 @@ def SaveCredentialsFile(self, credentials_file=None):
446
460
"Credentials file cannot be symbolic link"
447
461
)
448
462
463
+ def _SaveCredentialsDictionary (self ):
464
+ if self .credentials is None :
465
+ raise InvalidCredentialsError ("No credentials to save" )
466
+
467
+ storage = self ._storages ["dictionary" ]
468
+ storage .put (self .credentials )
469
+
449
470
def LoadClientConfig (self , backend = None ):
450
471
"""Loads client configuration according to specified backend.
451
472
0 commit comments