-
Notifications
You must be signed in to change notification settings - Fork 344
New OAuth2AuthManager #2244
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
New OAuth2AuthManager #2244
Changes from 10 commits
3ee34b7
8fbec86
5954550
92ad051
38042ca
ab75641
80c3fd5
f15cc90
c32bc9c
9223de0
7dd0165
b690f40
977842b
c860e63
4011015
d7af39e
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,9 +17,12 @@ | |||||||||
|
||||||||||
import base64 | ||||||||||
import importlib | ||||||||||
import threading | ||||||||||
import time | ||||||||||
from abc import ABC, abstractmethod | ||||||||||
from typing import Any, Dict, Optional, Type | ||||||||||
|
||||||||||
import requests | ||||||||||
from requests import HTTPError, PreparedRequest, Session | ||||||||||
from requests.auth import AuthBase | ||||||||||
|
||||||||||
|
@@ -119,6 +122,95 @@ def auth_header(self) -> str: | |||||||||
return f"Bearer {self._token}" | ||||||||||
|
||||||||||
|
||||||||||
class OAuth2TokenProvider: | ||||||||||
"""Thread-safe OAuth2 token provider with token refresh support.""" | ||||||||||
|
||||||||||
client_id: str | ||||||||||
client_secret: str | ||||||||||
token_url: str | ||||||||||
scope: Optional[str] | ||||||||||
refresh_margin: int | ||||||||||
expires_in: Optional[int] | ||||||||||
|
||||||||||
_token: Optional[str] | ||||||||||
_expires_at: int | ||||||||||
_lock: threading.Lock | ||||||||||
|
||||||||||
def __init__( | ||||||||||
self, | ||||||||||
client_id: str, | ||||||||||
client_secret: str, | ||||||||||
token_url: str, | ||||||||||
scope: Optional[str] = None, | ||||||||||
refresh_margin: int = 60, | ||||||||||
expires_in: Optional[int] = None, | ||||||||||
): | ||||||||||
self.client_id = client_id | ||||||||||
self.client_secret = client_secret | ||||||||||
self.token_url = token_url | ||||||||||
self.scope = scope | ||||||||||
self.refresh_margin = refresh_margin | ||||||||||
self.expires_in = expires_in | ||||||||||
|
||||||||||
self._token = None | ||||||||||
self._expires_at = 0 | ||||||||||
self._lock = threading.Lock() | ||||||||||
|
||||||||||
def _refresh_token(self) -> None: | ||||||||||
data = { | ||||||||||
"grant_type": "client_credentials", | ||||||||||
"client_id": self.client_id, | ||||||||||
"client_secret": self.client_secret, | ||||||||||
} | ||||||||||
if self.scope: | ||||||||||
data["scope"] = self.scope | ||||||||||
|
||||||||||
response = requests.post(self.token_url, data=data) | ||||||||||
kevinjqliu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
response.raise_for_status() | ||||||||||
result = response.json() | ||||||||||
|
||||||||||
self._token = result["access_token"] | ||||||||||
expires_in = result.get("expires_in", self.expires_in) | ||||||||||
if expires_in is None: | ||||||||||
raise ValueError( | ||||||||||
"The expiration time of the Token must be provided by the Server in the Access Token Response in `expires_in` field, or by the PyIceberg Client." | ||||||||||
kevinjqliu marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
) | ||||||||||
self._expires_at = time.time() + expires_in - self.refresh_margin | ||||||||||
sungwy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
def get_token(self) -> str: | ||||||||||
with self._lock: | ||||||||||
if not self._token or time.time() >= self._expires_at: | ||||||||||
sungwy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
self._refresh_token() | ||||||||||
if self._token is None: | ||||||||||
raise ValueError("Authorization token is None after refresh") | ||||||||||
return self._token | ||||||||||
|
||||||||||
|
||||||||||
class OAuth2AuthManager(AuthManager): | ||||||||||
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 we have any tests for 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 dont see iceberg-python/mkdocs/docs/configuration.md Lines 368 to 371 in 4cac691
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. we are using 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 researched the IETF RFCs on OAuth closely, and my understanding is that 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. make sense! thanks for looking into it |
||||||||||
"""Auth Manager implementation that supports OAuth2 as defined in IETF RFC6749.""" | ||||||||||
|
||||||||||
def __init__( | ||||||||||
self, | ||||||||||
client_id: str, | ||||||||||
client_secret: str, | ||||||||||
token_url: str, | ||||||||||
scope: Optional[str] = None, | ||||||||||
refresh_margin: int = 60, | ||||||||||
expires_in: Optional[int] = None, | ||||||||||
): | ||||||||||
self.token_provider = OAuth2TokenProvider( | ||||||||||
client_id, | ||||||||||
client_secret, | ||||||||||
token_url, | ||||||||||
scope, | ||||||||||
refresh_margin, | ||||||||||
expires_in, | ||||||||||
) | ||||||||||
|
||||||||||
def auth_header(self) -> str: | ||||||||||
return f"Bearer {self.token_provider.get_token()}" | ||||||||||
|
||||||||||
|
||||||||||
class AuthManagerAdapter(AuthBase): | ||||||||||
"""A `requests.auth.AuthBase` adapter that integrates an `AuthManager` into a `requests.Session` to automatically attach the appropriate Authorization header to every request. | ||||||||||
|
||||||||||
|
@@ -197,3 +289,4 @@ def create(cls, class_or_name: str, config: Dict[str, Any]) -> AuthManager: | |||||||||
AuthManagerFactory.register("noop", NoopAuthManager) | ||||||||||
AuthManagerFactory.register("basic", BasicAuthManager) | ||||||||||
AuthManagerFactory.register("legacyoauth2", LegacyOAuth2AuthManager) | ||||||||||
AuthManagerFactory.register("oauth2", OAuth2AuthManager) | ||||||||||
kevinjqliu marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.