From 774906e9445fc54caec47f14c252e9dfdf9321dd Mon Sep 17 00:00:00 2001 From: David Harcombe Date: Thu, 7 Sep 2023 14:25:16 +0000 Subject: [PATCH] Updaate to remove legacy style support --- auth/credentials.py | 31 ++++++++++--------------------- pyproject.toml | 2 +- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/auth/credentials.py b/auth/credentials.py index 2ea827d..a4ae185 100644 --- a/auth/credentials.py +++ b/auth/credentials.py @@ -15,8 +15,7 @@ from dataclasses import dataclass from datetime import datetime -import json -from typing import Any, Dict, Type, TypeVar +from typing import Any, Dict, Mapping, Type, TypeVar, Union import pytz from dateutil.relativedelta import relativedelta @@ -24,9 +23,9 @@ from google.oauth2 import credentials as oauth from auth import decorators -from .credentials_helpers import encode_key from .abstract_datastore import AbstractDatastore +from .credentials_helpers import encode_key from .exceptions import CredentialsError @@ -36,7 +35,6 @@ class ProjectCredentials(object): client_secret: str - class Credentials(object): """Credentials. @@ -97,7 +95,7 @@ def token_details(self) -> Dict[str, Any]: return self.datastore.get_document(id=encode_key(self._email)) def store_credentials(self, - creds: oauth.Credentials) -> None: + creds: Union[oauth.Credentials, Mapping[str, Any]]) -> None: """Stores the credentials. This function uses the datastore to store the user credentials for later. @@ -110,8 +108,11 @@ def store_credentials(self, """ if self._email: key = encode_key(self._email) - json_creds = json.loads(creds.to_json()) - self.datastore.update_document(id=key, new_data=json_creds) + + if isinstance(creds, oauth.Credentials): + self.datastore.update_document(id=key, new_data=creds.to_json()) + else: + self.datastore.update_document(id=key, new_data=creds) def _refresh_credentials(self, creds: oauth.Credentials) -> None: """Refreshes the Google OAuth credentials. @@ -139,18 +140,7 @@ def credentials(self) -> oauth.Credentials: expiry = self._to_utc( datetime.now().astimezone(pytz.utc) + relativedelta(minutes=30)) if token := self.token_details: - if token.get('access_token'): - # This handles old-style credential storages. - creds = oauth.Credentials.from_authorized_user_info({ - 'token': token['access_token'], - 'refresh_token': token['refresh_token'], - 'client_id': self.project_credentials.client_id, - 'client_secret': self.project_credentials.client_secret, - }) - - else: - creds = \ - oauth.Credentials.from_authorized_user_info(token) + creds = oauth.Credentials.from_authorized_user_info(token) if creds.expired: creds.expiry = expiry @@ -158,8 +148,7 @@ def credentials(self) -> oauth.Credentials: else: creds = None - raise CredentialsError( - message='credentials not found', email=self._email) + raise CredentialsError(message='credentials not found', email=self._email) return creds diff --git a/pyproject.toml b/pyproject.toml index 7215a91..bdb9ed1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "python-oauth-token-manager" -version = "0.2.7" +version = "0.3.0" authors = [{ name = "David Harcombe", email = "david.harcombe@gmail.com" }] description = "API for managing stored OAuth credentials." readme = "README.md"