-
Notifications
You must be signed in to change notification settings - Fork 3k
token refresh offset #12136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
token refresh offset #12136
Changes from 28 commits
dc1a9b2
bd7ae61
6a58f5c
2a94b3d
51fbed8
9ed71f1
618c226
e6ab928
e235ad2
6d51768
beb9027
08fa31e
2bec84f
66fe30b
b2b4b43
4eb71a0
e060268
0d9e63e
9ba3910
10499c4
d93fdba
78518cd
82ad53a
429ca27
99e8921
a57f7fa
e08692f
101fa22
a38294a
173e26d
533ab8c
b6e1cc7
04dad9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,15 @@ def get_token(self, *scopes, **kwargs): | |
self._authorization_code = None # auth codes are single-use | ||
return token | ||
|
||
token = self._client.get_cached_access_token(scopes) or self._redeem_refresh_token(scopes, **kwargs) | ||
token = self._client.get_cached_access_token(scopes) | ||
if not token: | ||
token = self._redeem_refresh_token(scopes, **kwargs) | ||
elif self._client.should_refresh(token): | ||
try: | ||
self._redeem_refresh_token(scopes, **kwargs) | ||
except Exception: # pylint: disable=broad-except | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be logging refreshes which fail here? Is this already done in _redeem_refresh_token? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good question. I am leaning towards not logging it because:
|
||
|
||
if not token: | ||
raise ClientAuthenticationError( | ||
message="No authorization code, cached access token, or refresh token available." | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,11 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument | |
token = self._client.get_cached_access_token(scopes, query={"client_id": self._client_id}) | ||
if not token: | ||
token = self._client.obtain_token_by_client_certificate(scopes, self._certificate, **kwargs) | ||
elif self._client.should_refresh(token): | ||
try: | ||
self._client.obtain_token_by_client_certificate(scopes, self._certificate, **kwargs) | ||
except Exception: # pylint: disable=broad-except | ||
pass | ||
Comment on lines
49
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic: if not token:
# get new token
elif should_refrsesh:
try:
# get new token
except Exception:
# swallow seems to be present in most if not all the credentials. Perhaps it could be moved into a base or mixin, and have the implementation just provide a callback or an override for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. But different credentials have different ways to refresh/redeem tokens. So I have not found a clean way to do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of something like this: class CredentialBase(ABC):
def __init__(self, **kwargs):
self._client = AadClient(...)
def _get_token_impl(*scopes, **kwargs):
if not scopes:
raise ValueError('"get_token" requires at least one scope')
token = self._client.get_cached_access_token(scopes)
if not token:
token = self._request_token(scopes, **kwargs)
elif self._client.should_refresh(token):
try:
self._request_token(scopes, **kwargs)
except Exception: # pylint:disable=broad-except
pass
return token
@abc.abstractmethod
def _request_token(self, *scopes, **kwargs):
pass
class Credential(CredentialBase):
def get_token(*scopes, **kwargs):
"""relevant user-facing docstring"""
return self._get_token_impl(*scopes, **kwargs)
def _request_token(*scopes, **kwargs):
"""get a new token according to this credential's personal idiom"""
... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean we make a shared credential base? I would like to have it into a separate issue/PR as code refactoring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactoring always has a lower priority than new features. Merging this code is an open-ended commitment to maintaining it as is, so it's worth investigating a better organization now. The one I sketched may have its own problems (e.g. multiple inheritance would require some care) but it seems workable. What do you think? Have you tried something similar already? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think when we do refactoring by adding a shared class for all credentials, we can do further than only this. But I don't want to rush it right before a release. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return token | ||
|
||
def _get_auth_client(self, tenant_id, client_id, **kwargs): | ||
|
Uh oh!
There was an error while loading. Please reload this page.