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

67 lines
2.3 KiB
Python
Raw Normal View History

import logging
logger = logging.getLogger(__name__)
class IntercomSecurity:
def resolve_dm_policy(self) -> dict:
return {"mode": "open", "allow_from": []}
async def check_allowlist(self, peer_id: str, channel_type: str) -> bool:
return True
def resolve_dm_policy_for_account(self, account: dict) -> dict:
mode = account.get("dm_policy", "open")
return {"mode": mode, "allow_from": account.get("allow_from", [])}
def authorize_dm(self, account: dict, contact_id: str) -> tuple[bool, str]:
policy = account.get("dm_policy", "open")
allow_from = account.get("allow_from", [])
if policy == "disabled":
return False, "DM is disabled"
if policy == "open":
if "*" in allow_from or not allow_from:
return True, ""
return contact_id in allow_from, "not-allowlisted"
if policy == "allowlist":
if not allow_from:
return False, "allowlist-empty"
return contact_id in allow_from, "not-allowlisted"
if policy == "pairing":
return True, "pairing-required"
return False, "unknown-policy"
def apply_config_fixes(self, config: dict) -> dict:
config.setdefault("channels", {})
config["channels"].setdefault("intercom", {})
return config
def collect_warnings(self, config: dict, account_id: str | None = None, account: dict | None = None) -> list[str]:
warnings: list[str] = []
if account:
if not account.get("webhook_secret"):
warnings.append("webhookSecret is not configured. Webhook signature verification will be skipped.")
if account.get("dm_policy") == "open" and not account.get("allow_from"):
warnings.append('dmPolicy is "open" without allowFrom. Consider adding allowFrom=["*"] explicitly.')
if account.get("dm_policy") == "allowlist" and not account.get("allow_from"):
warnings.append('dmPolicy is "allowlist" but allowFrom is empty. No contacts will be allowed.')
return warnings
def collect_audit_findings(
self,
config,
account_id=None,
account=None,
source_config=None,
ordered_account_ids=None,
has_explicit_account_path=False,
) -> list[dict]:
return []