tiphys.tools.security

Security tools for the agent.

Allows users to manage pairing and security settings.

 1"""
 2Security tools for the agent.
 3
 4Allows users to manage pairing and security settings.
 5"""
 6
 7from typing import TYPE_CHECKING
 8
 9from tiphys.context import get_context
10from tiphys.tools.base import tool
11
12if TYPE_CHECKING:
13    from tiphys.security.pairing import PairingManager
14
15
16def _get_pairing_manager() -> "PairingManager":
17    """Get the pairing manager from the application context."""
18    # This assumes we'll add pairing_manager to AppContext
19    context = get_context()
20    # For now, we'll try to find it or raise error
21    if not hasattr(context, "pairing_manager") or not context.pairing_manager:
22        raise RuntimeError("Pairing manager is not available in the current context")
23    return context.pairing_manager
24
25
26@tool(name="generate_pairing_code", description="Generate a code to pair a new device or account")
27async def generate_pairing_code() -> str:
28    """
29    Generate a 6-character code that can be used to pair this account.
30    """
31    # This tool is usually called from an ALREADY trusted channel (like WebChat)
32    # to pair a NEW channel (like Telegram).
33    # But for simplicity, we'll just generate it.
34    _get_pairing_manager()
35    # In a real scenario, we need to know WHICH channel/peer we are pairing.
36    # For now, let's just return a placeholder message.
37    return "To pair a new channel, please use the CLI or a trusted admin interface."
38
39
40@tool(name="verify_pairing_code", description="Verify a pairing code to enable DM access")
41async def verify_pairing_code(code: str) -> str:
42    """
43    Submit a pairing code provided by an admin.
44    """
45    pm = _get_pairing_manager()
46    if pm.verify_code(code):
47        return "Pairing successful! You can now use the bot in DMs."
48    return "Invalid or expired pairing code."
@tool(name='generate_pairing_code', description='Generate a code to pair a new device or account')
async def generate_pairing_code() -> str:
27@tool(name="generate_pairing_code", description="Generate a code to pair a new device or account")
28async def generate_pairing_code() -> str:
29    """
30    Generate a 6-character code that can be used to pair this account.
31    """
32    # This tool is usually called from an ALREADY trusted channel (like WebChat)
33    # to pair a NEW channel (like Telegram).
34    # But for simplicity, we'll just generate it.
35    _get_pairing_manager()
36    # In a real scenario, we need to know WHICH channel/peer we are pairing.
37    # For now, let's just return a placeholder message.
38    return "To pair a new channel, please use the CLI or a trusted admin interface."

Generate a 6-character code that can be used to pair this account.

@tool(name='verify_pairing_code', description='Verify a pairing code to enable DM access')
async def verify_pairing_code(code: str) -> str:
41@tool(name="verify_pairing_code", description="Verify a pairing code to enable DM access")
42async def verify_pairing_code(code: str) -> str:
43    """
44    Submit a pairing code provided by an admin.
45    """
46    pm = _get_pairing_manager()
47    if pm.verify_code(code):
48        return "Pairing successful! You can now use the bot in DMs."
49    return "Invalid or expired pairing code."

Submit a pairing code provided by an admin.