28 lines
827 B
Python
28 lines
827 B
Python
|
|
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 MessengerSecurity:
|
||
|
|
@staticmethod
|
||
|
|
def resolve_dm_policy(account: dict) -> str:
|
||
|
|
policy = account.get("dm_policy", POLICY_PAIRING)
|
||
|
|
if policy not in VALID_POLICIES:
|
||
|
|
logger.warning(f"messenger invalid dm_policy '{policy}', defaulting to {POLICY_PAIRING}")
|
||
|
|
return POLICY_PAIRING
|
||
|
|
return policy
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def check_allowlist(peer_id: str, account: dict) -> bool:
|
||
|
|
allow_from: list[str] = account.get("allow_from", [])
|
||
|
|
if not allow_from:
|
||
|
|
return True
|
||
|
|
return peer_id in allow_from
|