38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
|
|
import logging
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.wechat_mp.types import WeChatAccount
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class WeChatMPSecurity:
|
||
|
|
|
||
|
|
VALID_POLICIES = {"open", "pairing", "allowlist", "disabled"}
|
||
|
|
|
||
|
|
def __init__(self, account: WeChatAccount | None = None):
|
||
|
|
self._account = account
|
||
|
|
if account and account.dm_policy in self.VALID_POLICIES:
|
||
|
|
self._policy = account.dm_policy
|
||
|
|
else:
|
||
|
|
self._policy = "open"
|
||
|
|
self._allowlist: set[str] = set(account.allow_from) if account else set()
|
||
|
|
|
||
|
|
def resolve_dm_policy(self) -> str:
|
||
|
|
return self._policy
|
||
|
|
|
||
|
|
def check_allowlist(self, openid: str, channel_type: str = "wechat-mp") -> bool:
|
||
|
|
if self._policy == "open":
|
||
|
|
return True
|
||
|
|
if self._policy == "disabled":
|
||
|
|
return False
|
||
|
|
if not self._allowlist:
|
||
|
|
logger.warning("allowlist is empty with policy=%s", self._policy)
|
||
|
|
return False
|
||
|
|
return openid in self._allowlist
|
||
|
|
|
||
|
|
def add_to_allowlist(self, openid: str):
|
||
|
|
self._allowlist.add(openid)
|
||
|
|
|
||
|
|
def remove_from_allowlist(self, openid: str):
|
||
|
|
self._allowlist.discard(openid)
|