From 54c72018860d5a0e004a37c7773d22bc3e2612b0 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 5 Mar 2024 11:57:37 +0100 Subject: [PATCH] Handle the old python3-google-auth in Debian (1.5.x) Implement our own to_json() method and adjust the code handling the refresh of the credentials: Old versions of the library did not read expiry or token from the file, needing you to refresh the token, but we did not trigger refresh because the token was never considered expired, hence check whether we have a token, and , if not, also consider it expired. Additionally, make the credential storage atomic such that we do not accidentally write an empty credential file on errors. --- lieer/remote.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lieer/remote.py b/lieer/remote.py index f36bfc3..57ee057 100644 --- a/lieer/remote.py +++ b/lieer/remote.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import json import os import time @@ -506,8 +507,23 @@ def __store_credentials__(self, path, credentials): """ Store valid credentials in json format """ - with open(path, "w") as storage: - storage.write(credentials.to_json()) + with open(path + ".new", "w") as storage: + try: + storage.write(credentials.to_json()) + except AttributeError: + storage.write( + json.dumps( + { + "token": credentials.token, + "refresh_token": credentials.refresh_token, + "token_uri": credentials.token_uri, + "client_id": credentials.client_id, + "client_secret": credentials.client_secret, + "scopes": credentials.scopes, + } + ) + ) + os.rename(path + ".new", path) def __get_credentials__(self): """ @@ -528,7 +544,11 @@ def __get_credentials__(self): ) if not credentials or not credentials.valid: - if credentials and credentials.expired and credentials.refresh_token: + if ( + credentials + and (credentials.expired or not credentials.token) + and credentials.refresh_token + ): credentials.refresh(Request()) elif self.CLIENT_SECRET_FILE is not None: