"""
Trusty Sign - Credentials Service
Manages digital signature credentials for users
NOW USES: TrustyVault MCP tools (centralized credential management)
REPLACED: Direct database access (oauth_server.database)
"""
import logging
import os
import httpx
from typing import Optional
from dataclasses import dataclass
logger = logging.getLogger(__name__)
# TrustyVault MCP server URL
TRUSTYVAULT_MCP_URL = os.getenv(
'TRUSTYVAULT_MCP_URL',
'http://localhost:8100/mcp/execute'
)
@dataclass
class FirmaCredentials:
"""Digital signature credentials"""
username: str
password: str
pin: str
class CredentialsService:
"""
Service for managing digital signature credentials
NOW USES: TrustyVault MCP vault_get_credentials / vault_set_credentials
"""
async def _call_mcp(self, tool_name: str, params: dict) -> dict:
"""Call TrustyVault MCP tool via HTTP."""
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
TRUSTYVAULT_MCP_URL,
json={"tool": tool_name, "params": params}
)
response.raise_for_status()
# Parse SSE response (simplified)
lines = response.text.strip().split('\n')
for line in lines:
if line.startswith('data: '):
import json
return json.loads(line[6:])
raise ValueError("No data in MCP response")
except Exception as e:
logger.error(f"MCP call failed ({tool_name}): {e}")
return {"success": False, "error": str(e)}