Skip to content

Commit 747e201

Browse files
committed
auth: add dictionary storage
Allows not to save anything to disk, which is useful overall but especially useful when working with env vars, which could be sensitive so saving their value to disk is undesirable.
1 parent 3940d7b commit 747e201

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

pydrive2/auth.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from oauth2client.client import AccessTokenRefreshError
1111
from oauth2client.client import OAuth2WebServerFlow
1212
from oauth2client.client import OOB_CALLBACK_URN
13+
from oauth2client.contrib.dictionary_storage import DictionaryStorage
1314
from oauth2client.file import Storage
1415
from oauth2client.tools import ClientRedirectHandler
1516
from oauth2client.tools import ClientRedirectServer
@@ -347,6 +348,11 @@ def _InitializeStoragesFromSettings(self):
347348
"Please specify credentials file to read"
348349
)
349350
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+
)
350356
elif save_credentials:
351357
raise InvalidConfigError(
352358
"Unknown save_credentials_backend: %s" % backend
@@ -366,6 +372,8 @@ def LoadCredentials(self, backend=None):
366372
raise InvalidConfigError("Please specify credential backend")
367373
if backend == "file":
368374
self.LoadCredentialsFile()
375+
elif backend == "dictionary":
376+
self.LoadCredentialsDictionary()
369377
else:
370378
raise InvalidConfigError("Unknown save_credentials_backend")
371379

@@ -399,6 +407,10 @@ def LoadCredentialsFile(self, credentials_file=None):
399407
if self.credentials:
400408
self.credentials.set_store(self._default_storage)
401409

410+
def LoadCredentialsDictionary(self):
411+
self._default_storage = self._storages["dictionary"]
412+
self.credentials = self._default_storage.get()
413+
402414
def SaveCredentials(self, backend=None):
403415
"""Saves credentials according to specified backend.
404416
@@ -415,6 +427,8 @@ def SaveCredentials(self, backend=None):
415427
raise InvalidConfigError("Please specify credential backend")
416428
if backend == "file":
417429
self.SaveCredentialsFile()
430+
elif backend == "dictionary":
431+
self.SaveCredentialsDictionary()
418432
else:
419433
raise InvalidConfigError("Unknown save_credentials_backend")
420434

@@ -446,6 +460,13 @@ def SaveCredentialsFile(self, credentials_file=None):
446460
"Credentials file cannot be symbolic link"
447461
)
448462

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+
449470
def LoadClientConfig(self, backend=None):
450471
"""Loads client configuration according to specified backend.
451472

0 commit comments

Comments
 (0)