managarten/services/mana-tts/app/auth.py
Till JS 996ec81a0e refactor(shared-python): extract shared auth package from mana-stt and mana-tts
Create packages/shared-python/manacore_auth/ with:
- auth.py: API key validation, rate limiting, local + external auth
- external_auth.py: mana-core-auth remote validation with caching
- create_auth_dependency(scope): factory for per-service auth deps

Migrated services:
- mana-stt: auth.py now wraps shared auth with scope="stt" (272→42 LOC)
- mana-tts: auth.py now wraps shared auth with scope="tts" (272→42 LOC)

The only difference between services was the scope parameter ("stt" vs "tts").
Both external_auth.py files were 100% identical and are now thin re-exports.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 14:09:32 +02:00

39 lines
955 B
Python

"""
API Key Authentication for ManaCore TTS Service.
Delegates to shared manacore_auth package.
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..", "..", "packages", "shared-python"))
from manacore_auth import (
APIKey,
AuthResult,
RateLimitInfo,
verify_api_key as _verify_api_key,
get_api_key_stats,
reload_api_keys,
api_key_header,
create_auth_dependency,
)
from manacore_auth.external_auth import (
ExternalValidationResult,
is_external_auth_enabled,
validate_api_key_external,
)
from typing import Optional
from fastapi import Security, Request
# TTS-specific auth dependency
verify_tts_key = create_auth_dependency("tts")
async def verify_api_key(
request: Request,
api_key: Optional[str] = Security(api_key_header),
) -> AuthResult:
"""Verify API key with TTS scope."""
return await _verify_api_key(request, scope="tts", api_key=api_key)