Skip to content

Commit 3971c4a

Browse files
Updaate to remove legacy style support (#2)
1 parent ccef5b6 commit 3971c4a

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

auth/credentials.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515

1616
from dataclasses import dataclass
1717
from datetime import datetime
18-
import json
19-
from typing import Any, Dict, Type, TypeVar
18+
from typing import Any, Dict, Mapping, Type, TypeVar, Union
2019

2120
import pytz
2221
from dateutil.relativedelta import relativedelta
2322
from google.auth.transport import requests
2423
from google.oauth2 import credentials as oauth
2524

2625
from auth import decorators
27-
from .credentials_helpers import encode_key
2826

2927
from .abstract_datastore import AbstractDatastore
28+
from .credentials_helpers import encode_key
3029
from .exceptions import CredentialsError
3130

3231

@@ -36,7 +35,6 @@ class ProjectCredentials(object):
3635
client_secret: str
3736

3837

39-
4038
class Credentials(object):
4139
"""Credentials.
4240
@@ -97,7 +95,7 @@ def token_details(self) -> Dict[str, Any]:
9795
return self.datastore.get_document(id=encode_key(self._email))
9896

9997
def store_credentials(self,
100-
creds: oauth.Credentials) -> None:
98+
creds: Union[oauth.Credentials, Mapping[str, Any]]) -> None:
10199
"""Stores the credentials.
102100
103101
This function uses the datastore to store the user credentials for later.
@@ -110,8 +108,11 @@ def store_credentials(self,
110108
"""
111109
if self._email:
112110
key = encode_key(self._email)
113-
json_creds = json.loads(creds.to_json())
114-
self.datastore.update_document(id=key, new_data=json_creds)
111+
112+
if isinstance(creds, oauth.Credentials):
113+
self.datastore.update_document(id=key, new_data=creds.to_json())
114+
else:
115+
self.datastore.update_document(id=key, new_data=creds)
115116

116117
def _refresh_credentials(self, creds: oauth.Credentials) -> None:
117118
"""Refreshes the Google OAuth credentials.
@@ -139,27 +140,15 @@ def credentials(self) -> oauth.Credentials:
139140
expiry = self._to_utc(
140141
datetime.now().astimezone(pytz.utc) + relativedelta(minutes=30))
141142
if token := self.token_details:
142-
if token.get('access_token'):
143-
# This handles old-style credential storages.
144-
creds = oauth.Credentials.from_authorized_user_info({
145-
'token': token['access_token'],
146-
'refresh_token': token['refresh_token'],
147-
'client_id': self.project_credentials.client_id,
148-
'client_secret': self.project_credentials.client_secret,
149-
})
150-
151-
else:
152-
creds = \
153-
oauth.Credentials.from_authorized_user_info(token)
143+
creds = oauth.Credentials.from_authorized_user_info(token)
154144

155145
if creds.expired:
156146
creds.expiry = expiry
157147
self._refresh_credentials(creds=creds)
158148

159149
else:
160150
creds = None
161-
raise CredentialsError(
162-
message='credentials not found', email=self._email)
151+
raise CredentialsError(message='credentials not found', email=self._email)
163152

164153
return creds
165154

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
55

66
[project]
77
name = "python-oauth-token-manager"
8-
version = "0.2.7"
8+
version = "0.3.0"
99
authors = [{ name = "David Harcombe", email = "[email protected]" }]
1010
description = "API for managing stored OAuth credentials."
1111
readme = "README.md"

0 commit comments

Comments
 (0)