"""Dependencies for the authenticated Agent queue protocol.""" from typing import Annotated from fastapi import Depends, Header, HTTPException from app.agent_auth.exceptions import ( AgentAuthenticationFailed, AgentAuthenticationUnavailable, AgentCredentialRevoked, AgentIdentityUnauthorized, ) from app.agent_auth.repository import AgentAuthRepository from app.agent_auth.service import AgentAuthService from app.agent_commands.repository import AgentCommandRepository from app.agent_commands.service import AgentCommandService def get_agent_command_service() -> AgentCommandService: return AgentCommandService(AgentCommandRepository()) def get_agent_auth_service() -> AgentAuthService: return AgentAuthService(AgentAuthRepository()) def require_agent_identity( agent_id: Annotated[str | None, Header(alias="X-MinerOps-Agent-ID")] = None, provided_secret: Annotated[ str | None, Header(alias="X-MinerOps-Agent-Secret") ] = None, service: AgentAuthService = Depends(get_agent_auth_service), ) -> str: try: return service.authenticate(agent_id, provided_secret) except AgentAuthenticationUnavailable as exc: raise HTTPException( status_code=503, detail="Agent authentication is not configured", ) from exc except AgentAuthenticationFailed as exc: raise HTTPException(status_code=401, detail=str(exc)) from exc except AgentCredentialRevoked as exc: raise HTTPException(status_code=403, detail=str(exc)) from exc except AgentIdentityUnauthorized as exc: raise HTTPException(status_code=403, detail=str(exc)) from exc