|
17 | 17 |
|
18 | 18 | import base64 |
19 | 19 | import importlib |
20 | | -import threading |
21 | | -import time |
22 | 20 | from abc import ABC, abstractmethod |
23 | 21 | from typing import Any, Dict, Optional, Type |
24 | 22 |
|
25 | | -import requests |
26 | 23 | from requests import HTTPError, PreparedRequest, Session |
27 | 24 | from requests.auth import AuthBase |
28 | 25 |
|
@@ -122,95 +119,6 @@ def auth_header(self) -> str: |
122 | 119 | return f"Bearer {self._token}" |
123 | 120 |
|
124 | 121 |
|
125 | | -class OAuth2TokenProvider: |
126 | | - """Thread-safe OAuth2 token provider with token refresh support.""" |
127 | | - |
128 | | - client_id: str |
129 | | - client_secret: str |
130 | | - token_url: str |
131 | | - scope: Optional[str] |
132 | | - refresh_margin: int |
133 | | - expires_in: Optional[int] |
134 | | - |
135 | | - _token: Optional[str] |
136 | | - _expires_at: int |
137 | | - _lock: threading.Lock |
138 | | - |
139 | | - def __init__( |
140 | | - self, |
141 | | - client_id: str, |
142 | | - client_secret: str, |
143 | | - token_url: str, |
144 | | - scope: Optional[str] = None, |
145 | | - refresh_margin: int = 60, |
146 | | - expires_in: Optional[int] = None, |
147 | | - ): |
148 | | - self.client_id = client_id |
149 | | - self.client_secret = client_secret |
150 | | - self.token_url = token_url |
151 | | - self.scope = scope |
152 | | - self.refresh_margin = refresh_margin |
153 | | - self.expires_in = expires_in |
154 | | - |
155 | | - self._token = None |
156 | | - self._expires_at = 0 |
157 | | - self._lock = threading.Lock() |
158 | | - |
159 | | - def _refresh_token(self) -> None: |
160 | | - data = { |
161 | | - "grant_type": "client_credentials", |
162 | | - "client_id": self.client_id, |
163 | | - "client_secret": self.client_secret, |
164 | | - } |
165 | | - if self.scope: |
166 | | - data["scope"] = self.scope |
167 | | - |
168 | | - response = requests.post(self.token_url, data=data) |
169 | | - response.raise_for_status() |
170 | | - result = response.json() |
171 | | - |
172 | | - self._token = result["access_token"] |
173 | | - expires_in = result.get("expires_in", self.expires_in) |
174 | | - if expires_in is None: |
175 | | - raise ValueError( |
176 | | - "The expiration time of the Token must be provided by the Server in the Access Token Response in `expired_in` field, or by the PyIceberg Client." |
177 | | - ) |
178 | | - self._expires_at = time.time() + expires_in - self.refresh_margin |
179 | | - |
180 | | - def get_token(self) -> str: |
181 | | - with self._lock: |
182 | | - if not self._token or time.time() >= self._expires_at: |
183 | | - self._refresh_token() |
184 | | - if self._token is None: |
185 | | - raise ValueError("Authorization token is None after refresh") |
186 | | - return self._token |
187 | | - |
188 | | - |
189 | | -class OAuth2AuthManager(AuthManager): |
190 | | - """Auth Manager implementation that supports OAuth2 as defined in IETF RFC6749.""" |
191 | | - |
192 | | - def __init__( |
193 | | - self, |
194 | | - client_id: str, |
195 | | - client_secret: str, |
196 | | - token_url: str, |
197 | | - scope: Optional[str] = None, |
198 | | - refresh_margin: int = 60, |
199 | | - expires_in: Optional[int] = None, |
200 | | - ): |
201 | | - self.token_provider = OAuth2TokenProvider( |
202 | | - client_id, |
203 | | - client_secret, |
204 | | - token_url, |
205 | | - scope, |
206 | | - refresh_margin, |
207 | | - expires_in, |
208 | | - ) |
209 | | - |
210 | | - def auth_header(self) -> str: |
211 | | - return f"Bearer {self.token_provider.get_token()}" |
212 | | - |
213 | | - |
214 | 122 | class AuthManagerAdapter(AuthBase): |
215 | 123 | """A `requests.auth.AuthBase` adapter that integrates an `AuthManager` into a `requests.Session` to automatically attach the appropriate Authorization header to every request. |
216 | 124 |
|
@@ -289,4 +197,3 @@ def create(cls, class_or_name: str, config: Dict[str, Any]) -> AuthManager: |
289 | 197 | AuthManagerFactory.register("noop", NoopAuthManager) |
290 | 198 | AuthManagerFactory.register("basic", BasicAuthManager) |
291 | 199 | AuthManagerFactory.register("legacyoauth2", LegacyOAuth2AuthManager) |
292 | | -AuthManagerFactory.register("oauth2", OAuth2AuthManager) |
0 commit comments