Skip to content

Commit

Permalink
Recover or fail gracefully when not being able to decode stored crede…
Browse files Browse the repository at this point in the history
…ntials
  • Loading branch information
Lykos153 committed Jan 27, 2021
1 parent 3ac62c4 commit 9a5e438
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
23 changes: 15 additions & 8 deletions git_annex_remote_googledrive/google_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


from drivelib import GoogleDrive
from drivelib import Credentials

from .keys import RemoteRoot, Key
from .keys import ExportRemoteRoot, ExportKey
Expand Down Expand Up @@ -85,11 +86,13 @@ def wrapper(self, *args, **kwargs):
prefix = self.annex.getconfig('prefix')
root_id = self.annex.getconfig('root_id')

if self.credentials is None:
raise RemoteError("Stored credentials are invalid. Please re-run `git-annex-remote-googledrive setup` and `git annex enableremote <remotename>`")
root = self._get_root(root_class, self.credentials, prefix, root_id)
if root.id != root_id:
raise RemoteError("ID of root folder changed. Was the repo moved? Please check remote and re-run git annex enableremote")

self.credentials = ''.join(root.json_creds().split())
self.credentials = root.creds()

self.root = root

Expand Down Expand Up @@ -202,14 +205,19 @@ def chunksize(self):
@property
def credentials(self):
if not hasattr(self, '_credentials'):
self._credentials = self.annex.getcreds('credentials')['user']
json_creds = self.annex.getcreds('credentials')['user']
try:
self._credentials = Credentials.from_json(json_creds)
except json.decoder.JSONDecodeError:
self.annex.debug("Error decoding stored credentials: {}".format(json_creds))
self._credentials = None
return self._credentials

@credentials.setter
def credentials(self, creds):
if not self.credentials or json.loads(creds) != json.loads(self.credentials):
if creds != self.credentials:
self._credentials = creds
self.annex.setcreds('credentials', creds, '')
self.annex.setcreds('credentials', ''.join(Credentials.to_json(creds).split()), '')

@send_version_on_error
def initremote(self):
Expand All @@ -230,8 +238,7 @@ def initremote(self):
token_file = othertmp_dir / "git-annex-remote-googledrive.token"

try:
with token_file.open('r') as fp:
credentials = ''.join(fp.read().split())
credentials = Credentials.from_authorized_user_file(token_file)
except Exception as e:
if token_config:
raise RemoteError("Could not read token file {}:".format(token_file), e)
Expand All @@ -256,13 +263,13 @@ def initremote(self):
raise RemoteError("Specified folder has subdirectories. Are you sure 'prefix' or 'id' is set correctly? In case you're migrating from gdrive or rclone, run 'git-annex-remote-googledrive migrate {prefix}' first.".format(prefix=prefix))

self.annex.setconfig('root_id', self.root.id)
self.credentials = ''.join(self.root.json_creds().split())
self.credentials = self.root.creds()

def prepare(self):
self._send_version()

if self.annex.getconfig('mute-api-lockdown-warning') != "true" and \
json.loads(self.credentials)['client_id'] == DEFAULT_CLIENT_ID:
self.credentials.client_id == DEFAULT_CLIENT_ID:

self._info("====== git-annex-remote-googledrive")
self._info("IMPORTANT: Google has started to lockdown their Google Drive API. This might affect access to your Google Drive remotes.")
Expand Down
4 changes: 4 additions & 0 deletions git_annex_remote_googledrive/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from drivelib import NotAuthenticatedError, CheckSumError
from drivelib import ResumableMediaUploadProgress, MediaDownloadProgress
from drivelib import AmbiguousPathError
from drivelib import Credentials

from googleapiclient.errors import HttpError

Expand Down Expand Up @@ -59,6 +60,9 @@ def id(self):
def json_creds(self) -> str:
return self.folder.drive.json_creds()

def creds(self) -> Credentials:
return self.folder.drive.creds

class RemoteRoot(RemoteRootBase):
def __init__(self, rootfolder, uuid: str=None, local_appdir: Union(str, PathLike)=None):
super().__init__(rootfolder, uuid=uuid, local_appdir=local_appdir)
Expand Down

0 comments on commit 9a5e438

Please sign in to comment.