1414
1515import jwt
1616import time
17- from typing import Dict
17+ from typing import Dict , Optional
1818
1919import planet_auth .logging .auth_logger
2020from planet_auth .credential import Credential
@@ -79,45 +79,55 @@ def _load(self):
7979
8080 def _refresh (self ):
8181 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.
8287 new_credentials = self ._auth_client .refresh (self ._credential .refresh_token ())
8388 new_credentials .set_path (self ._credential .path ())
8489 new_credentials .set_storage_provider (self ._credential .storage_provider ())
8590 new_credentials .save ()
8691
92+ # self.update_credential(new_credential=new_credentials)
8793 self ._credential = new_credentials
8894 self ._load ()
8995
90- def pre_request_hook (self ):
96+ def _refresh_if_needed (self ):
9197 # Reload the file before refreshing. Another process might
9298 # have done it for us, and save us the network call.
9399 #
94100 # 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 .
97103 #
98104 # Also, it's possible that we have a valid refresh token,
99105 # but not an access token. When that's true, we should
100106 # try to cash in the refresh token.
101107 #
102108 # If everything fails, continue with what we have. Let the API
103109 # 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 :
105112 try :
106113 self ._load ()
107114 except Exception as e : # pylint: disable=broad-exception-caught
108115 auth_logger .warning (
109116 msg = "Error loading auth token. Continuing with old auth token. Load error: " + str (e )
110117 )
111- if int ( time . time ()) > self ._refresh_at :
118+ if now > self ._refresh_at :
112119 try :
113120 self ._refresh ()
114121 except Exception as e : # pylint: disable=broad-exception-caught
115122 auth_logger .warning (
116123 msg = "Error refreshing auth token. Continuing with old auth token. Refresh error: " + str (e )
117124 )
125+
126+ def pre_request_hook (self ):
127+ self ._refresh_if_needed ()
118128 super ().pre_request_hook ()
119129
120- def update_credential (self , new_credential : Credential ):
130+ def update_credential (self , new_credential : Credential ) -> None :
121131 if not isinstance (new_credential , FileBackedOidcCredential ):
122132 raise TypeError (
123133 f"{ type (self ).__name__ } does not support { type (new_credential )} credentials. Use FileBackedOidcCredential."
@@ -126,7 +136,7 @@ def update_credential(self, new_credential: Credential):
126136 self ._refresh_at = 0
127137 # self._load() # Mimic __init__. Don't load, let that happen JIT.
128138
129- def update_credential_data (self , new_credential_data : Dict ):
139+ def update_credential_data (self , new_credential_data : Dict ) -> None :
130140 # This is more different than update_credential() than it may
131141 # appear. Inherent in being passed a Credential in update_credential()
132142 # 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):
137147 super ().update_credential_data (new_credential_data = new_credential_data )
138148 self ._load ()
139149
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+
140155
141156class RefreshOrReloginOidcTokenRequestAuthenticator (RefreshingOidcTokenRequestAuthenticator ):
142157 """
@@ -171,5 +186,6 @@ def _refresh(self):
171186 new_credentials .set_storage_provider (self ._credential .storage_provider ())
172187 new_credentials .save ()
173188
189+ # self.update_credential(new_credential=new_credentials)
174190 self ._credential = new_credentials
175191 self ._load ()
0 commit comments