|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import base64 |
| 19 | +from abc import ABC, abstractmethod |
| 20 | +from typing import Optional |
| 21 | + |
| 22 | +from requests import PreparedRequest |
| 23 | +from requests.auth import AuthBase |
| 24 | + |
| 25 | + |
| 26 | +class AuthManager(ABC): |
| 27 | + """ |
| 28 | + Abstract base class for Authentication Managers used to supply authorization headers to HTTP clients (e.g. requests.Session). |
| 29 | +
|
| 30 | + Subclasses must implement the `auth_header` method to return an Authorization header value. |
| 31 | + """ |
| 32 | + |
| 33 | + @abstractmethod |
| 34 | + def auth_header(self) -> Optional[str]: |
| 35 | + """Return the Authorization header value, or None if not applicable.""" |
| 36 | + |
| 37 | + |
| 38 | +class NoopAuthManager(AuthManager): |
| 39 | + def auth_header(self) -> Optional[str]: |
| 40 | + return None |
| 41 | + |
| 42 | + |
| 43 | +class BasicAuthManager(AuthManager): |
| 44 | + def __init__(self, username: str, password: str): |
| 45 | + credentials = f"{username}:{password}" |
| 46 | + self._token = base64.b64encode(credentials.encode()).decode() |
| 47 | + |
| 48 | + def auth_header(self) -> str: |
| 49 | + return f"Basic {self._token}" |
| 50 | + |
| 51 | + |
| 52 | +class AuthManagerAdapter(AuthBase): |
| 53 | + """A `requests.auth.AuthBase` adapter that integrates an `AuthManager` into a `requests.Session` to automatically attach the appropriate Authorization header to every request. |
| 54 | +
|
| 55 | + This adapter is useful when working with `requests.Session.auth` |
| 56 | + and allows reuse of authentication strategies defined by `AuthManager`. |
| 57 | + This AuthManagerAdapter is only intended to be used against the REST Catalog |
| 58 | + Server that expects the Authorization Header. |
| 59 | + """ |
| 60 | + |
| 61 | + def __init__(self, auth_manager: AuthManager): |
| 62 | + """ |
| 63 | + Initialize AuthManagerAdapter. |
| 64 | +
|
| 65 | + Args: |
| 66 | + auth_manager (AuthManager): An instance of an AuthManager subclass. |
| 67 | + """ |
| 68 | + self.auth_manager = auth_manager |
| 69 | + |
| 70 | + def __call__(self, request: PreparedRequest) -> PreparedRequest: |
| 71 | + """ |
| 72 | + Modify the outgoing request to include the Authorization header. |
| 73 | +
|
| 74 | + Args: |
| 75 | + request (requests.PreparedRequest): The HTTP request being prepared. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + requests.PreparedRequest: The modified request with Authorization header. |
| 79 | + """ |
| 80 | + if auth_header := self.auth_manager.auth_header(): |
| 81 | + request.headers["Authorization"] = auth_header |
| 82 | + return request |
0 commit comments