From 779e01a57faf89cb4e92cfd68463b3e0d5284d63 Mon Sep 17 00:00:00 2001 From: mhdzumair Date: Wed, 11 Dec 2024 15:15:35 +0530 Subject: [PATCH] Make `API_PASSWORD` optional for app that uses mediaflow as lib package Updated `api_password` to be optional in configurations and logic, allowing API access to function without it if not set. Adjusted related checks and encryption handling to accommodate this change while maintaining security features when a password is provided. Updated README to reflect the change. --- README.md | 2 +- mediaflow_proxy/configs.py | 2 +- mediaflow_proxy/main.py | 3 +++ mediaflow_proxy/utils/crypto_utils.py | 4 ++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 67b04ee..dfe675c 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ MediaFlow Proxy is a powerful and flexible solution for proxifying various types Set the following environment variables: -- `API_PASSWORD`: Required. Protects against unauthorized access and API network abuses. +- `API_PASSWORD`: Optional. Protects against unauthorized access and API network abuses. - `ENABLE_STREAMING_PROGRESS`: Optional. Enable streaming progress logging. Default is `false`. ### Transport Configuration diff --git a/mediaflow_proxy/configs.py b/mediaflow_proxy/configs.py index 83eb95e..7d24dba 100644 --- a/mediaflow_proxy/configs.py +++ b/mediaflow_proxy/configs.py @@ -51,7 +51,7 @@ class TransportConfig(BaseSettings): class Settings(BaseSettings): - api_password: str # The password for accessing the API endpoints. + api_password: str | None = None # The password for protecting the API endpoints. log_level: str = "INFO" # The logging level to use. transport_config: TransportConfig = Field(default_factory=TransportConfig) # Configuration for httpx transport. enable_streaming_progress: bool = False # Whether to enable streaming progress tracking. diff --git a/mediaflow_proxy/main.py b/mediaflow_proxy/main.py index e2c0e3a..9b45b81 100644 --- a/mediaflow_proxy/main.py +++ b/mediaflow_proxy/main.py @@ -38,6 +38,9 @@ async def verify_api_key(api_key: str = Security(api_password_query), api_key_al Raises: HTTPException: If the API key is invalid. """ + if not settings.api_password: + return + if api_key == settings.api_password or api_key_alt == settings.api_password: return diff --git a/mediaflow_proxy/utils/crypto_utils.py b/mediaflow_proxy/utils/crypto_utils.py index b20e1dd..056df3b 100644 --- a/mediaflow_proxy/utils/crypto_utils.py +++ b/mediaflow_proxy/utils/crypto_utils.py @@ -61,7 +61,7 @@ class EncryptionMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): encrypted_token = request.query_params.get("token") - if encrypted_token: + if encrypted_token and self.encryption_handler: try: client_ip = self.get_client_ip(request) decrypted_data = self.encryption_handler.decrypt_data(encrypted_token, client_ip) @@ -107,4 +107,4 @@ class EncryptionMiddleware(BaseHTTPMiddleware): return request.client.host if request.client else "127.0.0.1" -encryption_handler = EncryptionHandler(settings.api_password) +encryption_handler = EncryptionHandler(settings.api_password) if settings.api_password else None