ForcePilot/backend/package/yuxi/channel/extensions/workplace/security.py

46 lines
1.3 KiB
Python
Raw Normal View History

import logging
logger = logging.getLogger(__name__)
POLICY_OPEN = "open"
POLICY_PAIRING = "pairing"
POLICY_ALLOWLIST = "allowlist"
POLICY_DISABLED = "disabled"
VALID_POLICIES = {POLICY_OPEN, POLICY_PAIRING, POLICY_ALLOWLIST, POLICY_DISABLED}
class WorkplaceSecurity:
DM_POLICIES = frozenset(["open", "pairing", "allowlist", "disabled"])
@staticmethod
def resolve_dm_policy() -> dict:
return {"mode": "pairing", "allow_from": []}
@staticmethod
async def check_allowlist(peer_id: str, channel_type: str) -> bool:
from yuxi.channel.extensions.workplace.config import WorkplaceConfigAdapter
adapter = WorkplaceConfigAdapter()
account = await adapter.resolve_account("default")
policy = account.get("dm_policy", POLICY_PAIRING)
if policy == POLICY_OPEN:
return True
if policy == POLICY_DISABLED:
logger.info("Workplace DM disabled, rejecting peer_id=%s", peer_id)
return False
allow_from = account.get("allow_from", [])
if not allow_from:
if policy == POLICY_ALLOWLIST:
return False
return True
if peer_id in allow_from:
return True
logger.info("Workplace allowlist check failed for peer_id=%s", peer_id)
return False