From bfab2ad5c91c73a20cb61374ffe64c1baa5606a0 Mon Sep 17 00:00:00 2001 From: Andy Date: Mon, 16 Mar 2026 19:08:03 -0600 Subject: [PATCH] fix(serve): use X-Secret-Key header for REST API auth to match pywidevine Standardize on X-Secret-Key across all endpoints so RemoteClient, pywidevine CDM, and api-only mode all use the same auth header. Adds lightweight middleware for --api-only mode without pywidevine dep. --- unshackle/commands/serve.py | 12 +++++++++++- unshackle/core/api/handlers.py | 2 +- unshackle/core/remote_service.py | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/unshackle/commands/serve.py b/unshackle/commands/serve.py index a5860fde..b2c2fe61 100644 --- a/unshackle/commands/serve.py +++ b/unshackle/commands/serve.py @@ -119,13 +119,23 @@ def serve( config.serve["playready_devices"] = [] config.serve["playready_devices"].extend(list(config.directories.prds.glob("*.prd"))) + @web.middleware + async def api_key_authentication(request: web.Request, handler) -> web.Response: + """Authenticate API requests using X-Secret-Key header.""" + secret_key = request.headers.get("X-Secret-Key") + if not secret_key: + return web.json_response({"status": 401, "message": "Secret Key is Empty."}, status=401) + if secret_key not in request.app["config"]["users"]: + return web.json_response({"status": 401, "message": "Secret Key is Invalid."}, status=401) + return await handler(request) + if api_only: log.info("Starting REST API server (pywidevine/pyplayready CDM disabled)") if no_key: app = web.Application(middlewares=[cors_middleware]) app["config"] = {"users": {}} else: - app = web.Application(middlewares=[cors_middleware, pywidevine_serve.authentication]) + app = web.Application(middlewares=[cors_middleware, api_key_authentication]) app["config"] = {"users": {api_secret: {"devices": [], "username": "api_user"}}} app["debug_api"] = debug_api diff --git a/unshackle/core/api/handlers.py b/unshackle/core/api/handlers.py index 2fe90115..fda088a6 100644 --- a/unshackle/core/api/handlers.py +++ b/unshackle/core/api/handlers.py @@ -1367,7 +1367,7 @@ async def session_create_handler(data: Dict[str, Any], request: Optional[web.Req from unshackle.core.config import config as app_config session_id = str(uuid_mod.uuid4()) - api_key = request.headers.get("X-API-Key", "anonymous") if request else "anonymous" + api_key = request.headers.get("X-Secret-Key", "anonymous") if request else "anonymous" api_key_hash = hashlib.sha256(api_key.encode()).hexdigest()[:12] session_cache_tag = f"_sessions/{api_key_hash}/{session_id}/{normalized_service}" diff --git a/unshackle/core/remote_service.py b/unshackle/core/remote_service.py index 6ac9e670..7420eba4 100644 --- a/unshackle/core/remote_service.py +++ b/unshackle/core/remote_service.py @@ -47,7 +47,7 @@ class RemoteClient: if self._session is None: self._session = requests.Session() if self.api_key: - self._session.headers["X-API-Key"] = self.api_key + self._session.headers["X-Secret-Key"] = self.api_key return self._session def _request(self, method: str, endpoint: str, data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: