Authentication
Reference for magick_mind.auth.
magick_mind.auth.email_password
Email/password authentication provider.
compute_token_expiry
def compute_token_expiry(expires_in: int,
refresh_expires_in: int,
current_time: float,
buffer_seconds: float = 10.0) -> tuple[float, float]Compute token expiry timestamps with safety buffer.
Pure function — no side effects, easily testable.
Returns:
(access_expires_at, refresh_expires_at)
EmailPasswordAuth
class EmailPasswordAuth(AuthProvider)Email/password authentication using the Magick Mind /v1/auth/login endpoint.
Automatically handles token refresh when the access token expires.
Example:
auth = EmailPasswordAuth( email="user@example.com", password="your_password", base_url="https://api.magickmind.ai" )
Login happens automatically on first request
headers = await auth.get_headers_async()
EmailPasswordAuth.__init__
def __init__(email: str, password: str, base_url: str, timeout: float = 30.0)Initialize email/password authentication.
Arguments:
email- User email addresspassword- User passwordbase_url- Base URL of the Magick Mind APItimeout- Request timeout in seconds
EmailPasswordAuth.is_authenticated
def is_authenticated() -> boolCheck if currently authenticated with a valid token.
EmailPasswordAuth.get_token_async
async def get_token_async() -> strGet raw access token asynchronously, refreshing if needed.
EmailPasswordAuth.refresh_if_needed_async
async def refresh_if_needed_async() -> NoneAsync version of refresh_if_needed.
EmailPasswordAuth.get_headers_async
async def get_headers_async() -> Dict[str, str]Async version of get_headers.
magick_mind.auth.base
Base authentication provider interface.
AuthProvider
class AuthProvider(ABC)Base class for authentication providers.
AuthProvider.get_headers_async
@abstractmethod
async def get_headers_async() -> Dict[str, str]Get authentication headers for API requests (async).
Returns:
Dictionary of HTTP headers to include in requests
AuthProvider.refresh_if_needed_async
@abstractmethod
async def refresh_if_needed_async() -> NoneRefresh authentication credentials if needed (async). Implementations should handle login and token refresh logic.
AuthProvider.is_authenticated
@abstractmethod
def is_authenticated() -> boolCheck if the provider is currently authenticated.
Returns:
True if authenticated, False otherwise
AuthProvider.get_token_async
@abstractmethod
async def get_token_async() -> strGet the raw access token asynchronously. Should handle refresh if needed.
Returns:
Raw access token string