-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathauth.py
More file actions
330 lines (261 loc) · 11.1 KB
/
auth.py
File metadata and controls
330 lines (261 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import base64
import importlib
import logging
import threading
import time
from abc import ABC, abstractmethod
from functools import cached_property
from typing import Any
import requests
from requests import HTTPError, PreparedRequest, Session
from requests.auth import AuthBase
from pyiceberg.catalog.rest.response import TokenResponse, _handle_non_200_response
from pyiceberg.exceptions import OAuthError
AUTH_MANAGER = "auth.manager"
COLON = ":"
logger = logging.getLogger(__name__)
class AuthManager(ABC):
"""
Abstract base class for Authentication Managers used to supply authorization headers to HTTP clients (e.g. requests.Session).
Subclasses must implement the `auth_header` method to return an Authorization header value.
"""
@abstractmethod
def auth_header(self) -> str | None:
"""Return the Authorization header value, or None if not applicable."""
class NoopAuthManager(AuthManager):
"""Auth Manager implementation with no auth."""
def auth_header(self) -> str | None:
return None
class BasicAuthManager(AuthManager):
"""AuthManager implementation that supports basic password auth."""
def __init__(self, username: str, password: str):
credentials = f"{username}:{password}"
self._token = base64.b64encode(credentials.encode()).decode()
def auth_header(self) -> str:
return f"Basic {self._token}"
class LegacyOAuth2AuthManager(AuthManager):
"""Legacy OAuth2 AuthManager implementation.
This class exists for backward compatibility, and will be removed in
PyIceberg 1.0.0 in favor of OAuth2AuthManager.
"""
_session: Session
_auth_url: str | None
_token: str | None
_credential: str | None
_optional_oauth_params: dict[str, str] | None
def __init__(
self,
session: Session,
auth_url: str | None = None,
credential: str | None = None,
initial_token: str | None = None,
optional_oauth_params: dict[str, str] | None = None,
):
self._session = session
self._auth_url = auth_url
self._token = initial_token
self._credential = credential
self._optional_oauth_params = optional_oauth_params
self._refresh_token()
def _fetch_access_token(self, credential: str) -> str:
if COLON in credential:
client_id, client_secret = credential.split(COLON, maxsplit=1)
else:
client_id, client_secret = None, credential
data = {"grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret}
if self._optional_oauth_params:
data.update(self._optional_oauth_params)
if self._auth_url is None:
raise ValueError("Cannot fetch access token from undefined auth_url")
response = self._session.post(
url=self._auth_url, data=data, headers={**self._session.headers, "Content-type": "application/x-www-form-urlencoded"}
)
try:
response.raise_for_status()
except HTTPError as exc:
_handle_non_200_response(exc, {400: OAuthError, 401: OAuthError})
return TokenResponse.model_validate_json(response.text).access_token
def _refresh_token(self) -> None:
if self._credential is not None:
self._token = self._fetch_access_token(self._credential)
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: str | None
refresh_margin: int
expires_in: int | None
_token: str | None
_expires_at: int
_lock: threading.Lock
def __init__(
self,
client_id: str,
client_secret: str,
token_url: str,
scope: str | None = None,
refresh_margin: int = 60,
expires_in: int | None = 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()
@cached_property
def _client_secret_header(self) -> str:
creds = f"{self.client_id}:{self.client_secret}"
creds_bytes = creds.encode("utf-8")
b64_creds = base64.b64encode(creds_bytes).decode("utf-8")
return f"Basic {b64_creds}"
def _refresh_token(self) -> None:
data = {"grant_type": "client_credentials"}
if self.scope:
data["scope"] = self.scope
response = requests.post(self.token_url, data=data, headers={"Authorization": self._client_secret_header})
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."
)
self._expires_at = time.monotonic() + expires_in - self.refresh_margin
def get_token(self) -> str:
with self._lock:
if not self._token or time.monotonic() >= self._expires_at:
self._refresh_token()
if self._token is None:
raise ValueError("Authorization token is None after refresh")
return self._token
class OAuth2AuthManager(AuthManager):
"""Auth Manager implementation that supports OAuth2 as defined in IETF RFC6749."""
def __init__(
self,
client_id: str,
client_secret: str,
token_url: str,
scope: str | None = None,
refresh_margin: int = 60,
expires_in: int | None = 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 GoogleAuthManager(AuthManager):
"""An auth manager that is responsible for handling Google credentials."""
def __init__(self, credentials_path: str | None = None, scopes: list[str] | None = None):
"""
Initialize GoogleAuthManager.
Args:
credentials_path: Optional path to Google credentials JSON file.
scopes: Optional list of OAuth2 scopes.
"""
try:
import google.auth
import google.auth.transport.requests
except ImportError as e:
raise ImportError("Google Auth libraries not found. Please install 'google-auth'.") from e
if credentials_path:
self.credentials, _ = google.auth.load_credentials_from_file(credentials_path, scopes=scopes)
else:
logger.info("Using Google Default Application Credentials")
self.credentials, _ = google.auth.default(scopes=scopes)
self._auth_request = google.auth.transport.requests.Request()
def auth_header(self) -> str:
self.credentials.refresh(self._auth_request)
return f"Bearer {self.credentials.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.
This adapter is useful when working with `requests.Session.auth`
and allows reuse of authentication strategies defined by `AuthManager`.
This AuthManagerAdapter is only intended to be used against the REST Catalog
Server that expects the Authorization Header.
"""
def __init__(self, auth_manager: AuthManager):
"""
Initialize AuthManagerAdapter.
Args:
auth_manager (AuthManager): An instance of an AuthManager subclass.
"""
self.auth_manager = auth_manager
def __call__(self, request: PreparedRequest) -> PreparedRequest:
"""
Modify the outgoing request to include the Authorization header.
Args:
request (requests.PreparedRequest): The HTTP request being prepared.
Returns:
requests.PreparedRequest: The modified request with Authorization header.
"""
if auth_header := self.auth_manager.auth_header():
request.headers["Authorization"] = auth_header
return request
class AuthManagerFactory:
_registry: dict[str, type["AuthManager"]] = {}
@classmethod
def register(cls, name: str, auth_manager_class: type["AuthManager"]) -> None:
"""
Register a string name to a known AuthManager class.
Args:
name (str): unique name like 'oauth2' to register the AuthManager with
auth_manager_class (Type["AuthManager"]): Implementation of AuthManager
Returns:
None
"""
cls._registry[name] = auth_manager_class
@classmethod
def create(cls, class_or_name: str, config: dict[str, Any]) -> AuthManager:
"""
Create an AuthManager by name or fully-qualified class path.
Args:
class_or_name (str): Either a name like 'oauth2' or a full class path like 'my.module.CustomAuthManager'
config (Dict[str, Any]): Configuration passed to the AuthManager constructor
Returns:
AuthManager: An instantiated AuthManager subclass
"""
if class_or_name in cls._registry:
manager_cls = cls._registry[class_or_name]
else:
try:
module_path, class_name = class_or_name.rsplit(".", 1)
module = importlib.import_module(module_path)
manager_cls = getattr(module, class_name)
except Exception as err:
raise ValueError(f"Could not load AuthManager class for '{class_or_name}'") from err
return manager_cls(**config)
AuthManagerFactory.register("noop", NoopAuthManager)
AuthManagerFactory.register("basic", BasicAuthManager)
AuthManagerFactory.register("legacyoauth2", LegacyOAuth2AuthManager)
AuthManagerFactory.register("oauth2", OAuth2AuthManager)
AuthManagerFactory.register("google", GoogleAuthManager)