Skip to content
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

refactor: use google.auth TokenState for credentials validity #1089

Merged
merged 4 commits into from
May 24, 2024

Conversation

jackwotherspoon
Copy link
Collaborator

@jackwotherspoon jackwotherspoon commented May 24, 2024

In the google.auth package, it is now recommended to use .token_state
over .valid for checking validity of credentials.

Token state gives more information such as if token is stale, expired,
invalid etc.

.valid is now marked as deprecated (code):

@property
def valid(self):
    """Checks the validity of the credentials.

    This is True if the credentials have a :attr:`token` and the token
    is not :attr:`expired`.

    .. deprecated:: v2.24.0
      Prefer checking :attr:`token_state` instead.
    """
    return self.token is not None and not self.expired

Closes #1048

@jackwotherspoon jackwotherspoon self-assigned this May 24, 2024
@jackwotherspoon jackwotherspoon requested a review from a team as a code owner May 24, 2024 13:30
Comment on lines +236 to +239
# before making Cloud SQL Admin API calls, refresh creds
if not self._client._credentials.token_state == TokenState.FRESH:
self._client._credentials.refresh(requests.Request())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved checking of credentials up a level into the refresh operation and out of the individual API calls, this way we are checking once instead of twice.

@jackwotherspoon jackwotherspoon merged commit 9fbe87a into main May 24, 2024
15 checks passed
@jackwotherspoon jackwotherspoon deleted the token-state branch May 24, 2024 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replacecredentials.valid check for credentials.token_state
2 participants