"""Base authentication classes for ComfyUI API."""
from abc import ABC, abstractmethod
class ComfyAuth(ABC):
"""Abstract base class for ComfyUI authentication.
This provides a consistent interface for injecting authentication headers
into HTTP requests to ComfyUI, following the SOLID principle of dependency
injection and the Interface Segregation Principle.
Attributes:
base_url: ComfyUI instance base URL (e.g., "http://127.0.0.1:8188")
"""
def __init__(self, base_url: str):
"""Initialize authentication with base URL.
Args:
base_url: ComfyUI instance base URL
"""
self.base_url = base_url.rstrip("/")
@property
@abstractmethod
def auth_header(self) -> dict:
"""Generate authentication headers for API requests.
Returns:
Dictionary of HTTP headers to inject into requests
"""
raise NotImplementedError()
def __repr__(self) -> str:
return f"{self.__class__.__name__}(base_url='{self.base_url}')"
class NoAuth(ComfyAuth):
"""No authentication implementation (current default behavior).
This is the default authentication mode for ComfyUI instances that
don't require authentication. Returns empty auth headers.
Example:
>>> auth = NoAuth("http://127.0.0.1:8188")
>>> auth.auth_header
{}
"""
@property
def auth_header(self) -> dict:
"""Returns empty auth headers (no authentication).
Returns:
Empty dictionary
"""
return {}
class TokenAuth(ComfyAuth):
"""Token-based authentication for ComfyUI (future use).
This implementation supports Bearer token authentication for ComfyUI
instances that require API tokens. The token is injected into the
Authorization header.
Attributes:
token: API token for authentication
Example:
>>> auth = TokenAuth("http://127.0.0.1:8188", token="my-secret-token")
>>> auth.auth_header
{'Authorization': 'Bearer my-secret-token'}
"""
def __init__(self, base_url: str, token: str):
"""Initialize token authentication.
Args:
base_url: ComfyUI instance base URL
token: API token for authentication
"""
super().__init__(base_url)
self.token = token
@property
def auth_header(self) -> dict:
"""Returns Bearer token authorization header.
Returns:
Dictionary with Authorization header
"""
return {"Authorization": f"Bearer {self.token}"}
def __repr__(self) -> str:
# Mask token for security
masked_token = f"{self.token[:4]}...{self.token[-4:]}" if len(self.token) > 8 else "***"
return f"TokenAuth(base_url='{self.base_url}', token='{masked_token}')"