43 lines
1.7 KiB
Python
43 lines
1.7 KiB
Python
from yuxi.channel.extensions.alipay.types import AlipayAccount, AlipayDmPolicy
|
|
|
|
|
|
class AlipaySecurity:
|
|
def __init__(self):
|
|
self._allow_from: list[str] = []
|
|
|
|
def resolve_dm_policy(self, account: AlipayAccount | None = None) -> dict:
|
|
if not account:
|
|
return {"mode": "disabled", "allow_from": []}
|
|
return {
|
|
"mode": account.dm_policy.value,
|
|
"allow_from": account.allow_from or [],
|
|
}
|
|
|
|
def check_allowlist(self, peer_id: str, channel_type: str = "direct", account: AlipayAccount | None = None) -> bool:
|
|
if not account:
|
|
return False
|
|
if account.dm_policy == AlipayDmPolicy.OPEN:
|
|
return True
|
|
if account.dm_policy == AlipayDmPolicy.DISABLED:
|
|
return False
|
|
if account.dm_policy == AlipayDmPolicy.ALLOWLIST:
|
|
return peer_id in (account.allow_from or [])
|
|
if account.dm_policy == AlipayDmPolicy.PAIRING:
|
|
return peer_id in self._allow_from
|
|
return False
|
|
|
|
def add_to_allowlist(self, peer_id: str) -> None:
|
|
if peer_id not in self._allow_from:
|
|
self._allow_from.append(peer_id)
|
|
|
|
def collect_warnings(
|
|
self, config: dict | None = None, account_id: str | None = None, account: AlipayAccount | None = None
|
|
) -> list[str]:
|
|
warnings = []
|
|
if account:
|
|
if account.dm_policy == AlipayDmPolicy.OPEN:
|
|
warnings.append("DM 策略为 'open',所有关注者均可发送消息")
|
|
if account.dm_policy == AlipayDmPolicy.OPEN and not account.allow_from:
|
|
warnings.append("DM 策略为 'open' 且无白名单限制")
|
|
return warnings
|