diff --git a/README.md b/README.md index b87902b..59e38b0 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,8 @@ The quick start uses a static `TERMINALS_API_KEY` for both the admin UI and Open For Open WebUI session authentication, set `TERMINALS_OPEN_WEBUI_URL` and use **Session** auth on the Open WebUI terminal connection. Terminals validates user tokens against that instance. Admin policy and lifecycle APIs still require a separate Terminals admin API key; user JWTs are not enough. +Tool discovery at `/openapi.json` and `/p/{policy_id}/openapi.json` also accepts the Terminals admin API key in Session mode. Terminal operations still require a valid user token. + `terminals serve` generates and prints a key if none is configured. That generated key changes when the service process restarts. Running the ASGI app directly without either authentication setting allows unauthenticated access and is intended only for local development. ## Configuration diff --git a/terminals/routers/auth.py b/terminals/routers/auth.py index 4f20a70..56f39c2 100644 --- a/terminals/routers/auth.py +++ b/terminals/routers/auth.py @@ -135,6 +135,16 @@ async def verify_admin_api_key( return None +async def verify_spec_access( + authorization: Optional[str] = Header(None), +) -> Optional[str]: + """Allow the admin API key for tool discovery before a user is involved.""" + scheme, _, token = (authorization or "").partition(" ") + if settings.api_key and scheme.lower() == "bearer" and token == settings.api_key: + return None + return await verify_api_key(authorization) + + async def verify_user_id( verified_id: Optional[str] = Depends(verify_api_key), x_user_id: str = Header(..., alias="X-User-Id"), diff --git a/terminals/routers/proxy.py b/terminals/routers/proxy.py index 17cf9ab..a493bef 100644 --- a/terminals/routers/proxy.py +++ b/terminals/routers/proxy.py @@ -33,7 +33,7 @@ from terminals.utils.context import ( normalize_context_id, ) from terminals.utils.policy_specs import PolicyNotFoundError, resolve_policy_spec -from terminals.routers.auth import validate_token, verify_api_key, verify_user_id +from terminals.routers.auth import validate_token, verify_api_key, verify_spec_access, verify_user_id router = APIRouter() @@ -406,7 +406,7 @@ async def _get_cached_spec( @router.get( "/openapi.json", - dependencies=[Depends(verify_api_key)], + dependencies=[Depends(verify_spec_access)], ) async def get_openapi_spec(request: Request): """Return the open-terminal OpenAPI spec. @@ -457,7 +457,7 @@ async def _resolve_policy_spec(policy_id: str) -> tuple[str, dict | None]: @router.get( "/p/{policy_id}/openapi.json", - dependencies=[Depends(verify_api_key)], + dependencies=[Depends(verify_spec_access)], ) async def policy_openapi_spec( policy_id: str,