15
15
16
16
from dataclasses import dataclass
17
17
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
20
19
21
20
import pytz
22
21
from dateutil .relativedelta import relativedelta
23
22
from google .auth .transport import requests
24
23
from google .oauth2 import credentials as oauth
25
24
26
25
from auth import decorators
27
- from .credentials_helpers import encode_key
28
26
29
27
from .abstract_datastore import AbstractDatastore
28
+ from .credentials_helpers import encode_key
30
29
from .exceptions import CredentialsError
31
30
32
31
@@ -36,7 +35,6 @@ class ProjectCredentials(object):
36
35
client_secret : str
37
36
38
37
39
-
40
38
class Credentials (object ):
41
39
"""Credentials.
42
40
@@ -97,7 +95,7 @@ def token_details(self) -> Dict[str, Any]:
97
95
return self .datastore .get_document (id = encode_key (self ._email ))
98
96
99
97
def store_credentials (self ,
100
- creds : oauth .Credentials ) -> None :
98
+ creds : Union [ oauth .Credentials , Mapping [ str , Any ]] ) -> None :
101
99
"""Stores the credentials.
102
100
103
101
This function uses the datastore to store the user credentials for later.
@@ -110,8 +108,11 @@ def store_credentials(self,
110
108
"""
111
109
if self ._email :
112
110
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 )
115
116
116
117
def _refresh_credentials (self , creds : oauth .Credentials ) -> None :
117
118
"""Refreshes the Google OAuth credentials.
@@ -139,27 +140,15 @@ def credentials(self) -> oauth.Credentials:
139
140
expiry = self ._to_utc (
140
141
datetime .now ().astimezone (pytz .utc ) + relativedelta (minutes = 30 ))
141
142
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 )
154
144
155
145
if creds .expired :
156
146
creds .expiry = expiry
157
147
self ._refresh_credentials (creds = creds )
158
148
159
149
else :
160
150
creds = None
161
- raise CredentialsError (
162
- message = 'credentials not found' , email = self ._email )
151
+ raise CredentialsError (message = 'credentials not found' , email = self ._email )
163
152
164
153
return creds
165
154
0 commit comments