-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Introduce normalize_oauth2_tokens #4498
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| import logging | ||
| from typing import Optional | ||
| from typing import Tuple | ||
|
|
@@ -97,6 +98,17 @@ def create_oauth2_session( | |
| ) | ||
|
|
||
|
|
||
| @experimental | ||
| def normalize_oauth2_tokens(tokens: object) -> OAuth2Token: | ||
| """Validates and normalizes token payload returned by OAuth libraries.""" | ||
| if not isinstance(tokens, Mapping): | ||
| raise ValueError( | ||
| "OAuth2 token response must be a dict-like object, got " | ||
| f"{type(tokens).__name__}." | ||
| ) | ||
| return tokens # type: ignore[return-value] | ||
|
|
||
|
|
||
| @experimental | ||
| def update_credential_with_tokens( | ||
| auth_credential: AuthCredential, tokens: OAuth2Token | ||
|
|
@@ -107,6 +119,7 @@ def update_credential_with_tokens( | |
| auth_credential: The authentication credential to update. | ||
| tokens: The OAuth2Token object containing new token information. | ||
| """ | ||
| tokens = normalize_oauth2_tokens(tokens) | ||
|
Contributor
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. The |
||
| auth_credential.oauth2.access_token = tokens.get("access_token") | ||
| auth_credential.oauth2.refresh_token = tokens.get("refresh_token") | ||
| auth_credential.oauth2.expires_at = ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve maintainability and avoid code duplication, you can reuse the
token_auth_methodvariable that is already defined within thetryblock. This avoids re-evaluating the same expression in theexceptblock.token_auth_method,