14
14
15
15
import jwt
16
16
import time
17
- from typing import Dict
17
+ from typing import Dict , Optional
18
18
19
19
import planet_auth .logging .auth_logger
20
20
from planet_auth .credential import Credential
@@ -79,45 +79,55 @@ def _load(self):
79
79
80
80
def _refresh (self ):
81
81
if self ._auth_client :
82
+ # TODO: cleanup? We did this before we wrote "update_credential_data()"
83
+ # We maybe should just be using that now. Taking that clean-up slow
84
+ # since it may have interactions with derived code.
85
+ # It seems to me we should be able to collapse the credential.save()
86
+ # calls in this file with the superclass.
82
87
new_credentials = self ._auth_client .refresh (self ._credential .refresh_token ())
83
88
new_credentials .set_path (self ._credential .path ())
84
89
new_credentials .set_storage_provider (self ._credential .storage_provider ())
85
90
new_credentials .save ()
86
91
92
+ # self.update_credential(new_credential=new_credentials)
87
93
self ._credential = new_credentials
88
94
self ._load ()
89
95
90
- def pre_request_hook (self ):
96
+ def _refresh_if_needed (self ):
91
97
# Reload the file before refreshing. Another process might
92
98
# have done it for us, and save us the network call.
93
99
#
94
100
# Also, if refresh tokens are configured to be one time use,
95
- # we want a fresh refresh token. Stale refresh tokens are
96
- # invalid in this case .
101
+ # we want a fresh refresh token. Stale refresh tokens may be
102
+ # invalid depending on server configuration .
97
103
#
98
104
# Also, it's possible that we have a valid refresh token,
99
105
# but not an access token. When that's true, we should
100
106
# try to cash in the refresh token.
101
107
#
102
108
# If everything fails, continue with what we have. Let the API
103
109
# we are calling decide if it's good enough.
104
- if int (time .time ()) > self ._refresh_at :
110
+ now = int (time .time ())
111
+ if now > self ._refresh_at :
105
112
try :
106
113
self ._load ()
107
114
except Exception as e : # pylint: disable=broad-exception-caught
108
115
auth_logger .warning (
109
116
msg = "Error loading auth token. Continuing with old auth token. Load error: " + str (e )
110
117
)
111
- if int ( time . time ()) > self ._refresh_at :
118
+ if now > self ._refresh_at :
112
119
try :
113
120
self ._refresh ()
114
121
except Exception as e : # pylint: disable=broad-exception-caught
115
122
auth_logger .warning (
116
123
msg = "Error refreshing auth token. Continuing with old auth token. Refresh error: " + str (e )
117
124
)
125
+
126
+ def pre_request_hook (self ):
127
+ self ._refresh_if_needed ()
118
128
super ().pre_request_hook ()
119
129
120
- def update_credential (self , new_credential : Credential ):
130
+ def update_credential (self , new_credential : Credential ) -> None :
121
131
if not isinstance (new_credential , FileBackedOidcCredential ):
122
132
raise TypeError (
123
133
f"{ type (self ).__name__ } does not support { type (new_credential )} credentials. Use FileBackedOidcCredential."
@@ -126,7 +136,7 @@ def update_credential(self, new_credential: Credential):
126
136
self ._refresh_at = 0
127
137
# self._load() # Mimic __init__. Don't load, let that happen JIT.
128
138
129
- def update_credential_data (self , new_credential_data : Dict ):
139
+ def update_credential_data (self , new_credential_data : Dict ) -> None :
130
140
# This is more different than update_credential() than it may
131
141
# appear. Inherent in being passed a Credential in update_credential()
132
142
# is that it may not yet be loaded from disk, and so deferring
@@ -137,6 +147,11 @@ def update_credential_data(self, new_credential_data: Dict):
137
147
super ().update_credential_data (new_credential_data = new_credential_data )
138
148
self ._load ()
139
149
150
+ def credential (self , refresh_if_needed : bool = False ) -> Optional [Credential ]:
151
+ if refresh_if_needed :
152
+ self ._refresh_if_needed ()
153
+ return super ().credential ()
154
+
140
155
141
156
class RefreshOrReloginOidcTokenRequestAuthenticator (RefreshingOidcTokenRequestAuthenticator ):
142
157
"""
@@ -171,5 +186,6 @@ def _refresh(self):
171
186
new_credentials .set_storage_provider (self ._credential .storage_provider ())
172
187
new_credentials .save ()
173
188
189
+ # self.update_credential(new_credential=new_credentials)
174
190
self ._credential = new_credentials
175
191
self ._load ()
0 commit comments