feat: add platform access policy
这个提交存在于:
父节点
d3d0327eaa
当前提交
d60788d6c9
共有 4 个文件被更改,包括 185 次插入 和 0 次删除
|
|
@ -13,6 +13,15 @@
|
||||||
"obvious_hint": true,
|
"obvious_hint": true,
|
||||||
"default": ""
|
"default": ""
|
||||||
},
|
},
|
||||||
|
"allowed_platforms": {
|
||||||
|
"description": "允许使用插件的平台",
|
||||||
|
"type": "list",
|
||||||
|
"hint": "默认支持 aiocqhttp 和 telegram",
|
||||||
|
"default": [
|
||||||
|
"aiocqhttp",
|
||||||
|
"telegram"
|
||||||
|
]
|
||||||
|
},
|
||||||
"private_user_ids": {
|
"private_user_ids": {
|
||||||
"description": "允许操作的私聊用户",
|
"description": "允许操作的私聊用户",
|
||||||
"type": "list",
|
"type": "list",
|
||||||
|
|
|
||||||
94
access_policy.py
普通文件
94
access_policy.py
普通文件
|
|
@ -0,0 +1,94 @@
|
||||||
|
"""Platform-aware access policy for MoviePilot commands."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any, Mapping
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, slots=True)
|
||||||
|
class PlatformIdentity:
|
||||||
|
"""Normalized identity extracted from an AstrBot message event."""
|
||||||
|
|
||||||
|
platform_name: str
|
||||||
|
platform_id: str
|
||||||
|
user_id: str
|
||||||
|
group_id: str
|
||||||
|
session_origin: str
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_private(self) -> bool:
|
||||||
|
return not self.group_id
|
||||||
|
|
||||||
|
|
||||||
|
def _string(value: Any) -> str:
|
||||||
|
return str(value).strip() if value is not None else ""
|
||||||
|
|
||||||
|
|
||||||
|
def identity_from_event(event: Any) -> PlatformIdentity:
|
||||||
|
"""Extract a platform-neutral identity from an AstrMessageEvent-like object."""
|
||||||
|
return PlatformIdentity(
|
||||||
|
platform_name=_string(event.get_platform_name()),
|
||||||
|
platform_id=_string(event.get_platform_id()),
|
||||||
|
user_id=_string(event.get_sender_id()),
|
||||||
|
group_id=_string(event.get_group_id()),
|
||||||
|
session_origin=_string(event.unified_msg_origin),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _normalized_ids(values: Any) -> set[str]:
|
||||||
|
if not isinstance(values, (list, tuple, set)):
|
||||||
|
return set()
|
||||||
|
return {_string(value) for value in values if _string(value)}
|
||||||
|
|
||||||
|
|
||||||
|
class AccessPolicy:
|
||||||
|
"""Whitelist policy for private and group MoviePilot operations."""
|
||||||
|
|
||||||
|
def __init__(self, config: Mapping[str, Any]) -> None:
|
||||||
|
self._allowed_platforms = _normalized_ids(
|
||||||
|
config.get("allowed_platforms", ["aiocqhttp", "telegram"])
|
||||||
|
)
|
||||||
|
self._private_user_ids = _normalized_ids(config.get("private_user_ids", []))
|
||||||
|
self._group_permissions = self._parse_group_permissions(
|
||||||
|
config.get("group_permissions", [])
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_group_permissions(value: Any) -> dict[str, set[str]]:
|
||||||
|
permissions: dict[str, set[str]] = {}
|
||||||
|
if isinstance(value, Mapping):
|
||||||
|
for group_id, user_ids in value.items():
|
||||||
|
normalized_group = _string(group_id)
|
||||||
|
if normalized_group:
|
||||||
|
permissions[normalized_group] = _normalized_ids(user_ids)
|
||||||
|
return permissions
|
||||||
|
|
||||||
|
if not isinstance(value, (list, tuple)):
|
||||||
|
return permissions
|
||||||
|
for item in value:
|
||||||
|
if not isinstance(item, Mapping):
|
||||||
|
continue
|
||||||
|
group_id = _string(item.get("group_id"))
|
||||||
|
if group_id:
|
||||||
|
permissions[group_id] = _normalized_ids(item.get("user_ids", []))
|
||||||
|
return permissions
|
||||||
|
|
||||||
|
def is_allowed(self, event: Any) -> bool:
|
||||||
|
"""Return whether an event may operate MoviePilot."""
|
||||||
|
identity = identity_from_event(event)
|
||||||
|
if not identity.platform_name or identity.platform_name not in self._allowed_platforms:
|
||||||
|
return False
|
||||||
|
if not identity.user_id:
|
||||||
|
return False
|
||||||
|
if identity.is_private:
|
||||||
|
return identity.user_id in self._private_user_ids
|
||||||
|
return identity.group_id in self._group_permissions and identity.user_id in self._group_permissions[identity.group_id]
|
||||||
|
|
||||||
|
def is_allowed_identity(self, identity: PlatformIdentity) -> bool:
|
||||||
|
"""Return whether a normalized identity may operate MoviePilot."""
|
||||||
|
if identity.platform_name not in self._allowed_platforms or not identity.user_id:
|
||||||
|
return False
|
||||||
|
if identity.is_private:
|
||||||
|
return identity.user_id in self._private_user_ids
|
||||||
|
return identity.user_id in self._group_permissions.get(identity.group_id, set())
|
||||||
5
main.py
5
main.py
|
|
@ -4,6 +4,8 @@ from astrbot.api import logger, star
|
||||||
from astrbot.api.event import AstrMessageEvent, filter
|
from astrbot.api.event import AstrMessageEvent, filter
|
||||||
from astrbot.api.star import AstrBotConfig
|
from astrbot.api.star import AstrBotConfig
|
||||||
|
|
||||||
|
from access_policy import AccessPolicy
|
||||||
|
|
||||||
|
|
||||||
@star.register(
|
@star.register(
|
||||||
"astrbot_plugin_moviepilot",
|
"astrbot_plugin_moviepilot",
|
||||||
|
|
@ -17,11 +19,14 @@ class MoviePilotPlugin(star.Star):
|
||||||
def __init__(self, context: star.Context, config: AstrBotConfig) -> None:
|
def __init__(self, context: star.Context, config: AstrBotConfig) -> None:
|
||||||
super().__init__(context)
|
super().__init__(context)
|
||||||
self.config = config
|
self.config = config
|
||||||
|
self.access_policy = AccessPolicy(config)
|
||||||
logger.info("MoviePilot 插件已加载")
|
logger.info("MoviePilot 插件已加载")
|
||||||
|
|
||||||
@filter.command("mp")
|
@filter.command("mp")
|
||||||
async def moviepilot_help(self, event: AstrMessageEvent) -> None:
|
async def moviepilot_help(self, event: AstrMessageEvent) -> None:
|
||||||
"""显示 MoviePilot 插件命令入口。"""
|
"""显示 MoviePilot 插件命令入口。"""
|
||||||
|
if not self.access_policy.is_allowed(event):
|
||||||
|
return
|
||||||
yield event.plain_result(
|
yield event.plain_result(
|
||||||
"MoviePilot 插件已加载。当前开发中,后续将支持 /mp search、/mp resources、"
|
"MoviePilot 插件已加载。当前开发中,后续将支持 /mp search、/mp resources、"
|
||||||
"/mp subscribe 和 /mp download。"
|
"/mp subscribe 和 /mp download。"
|
||||||
|
|
|
||||||
77
tests/test_access_policy.py
普通文件
77
tests/test_access_policy.py
普通文件
|
|
@ -0,0 +1,77 @@
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from access_policy import AccessPolicy, PlatformIdentity, identity_from_event
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class FakeEvent:
|
||||||
|
platform_name: str = "aiocqhttp"
|
||||||
|
platform_id: str = "bot-1"
|
||||||
|
user_id: str = "100"
|
||||||
|
group_id: str = ""
|
||||||
|
unified_msg_origin: str = "aiocqhttp:friend:100"
|
||||||
|
|
||||||
|
def get_platform_name(self) -> str:
|
||||||
|
return self.platform_name
|
||||||
|
|
||||||
|
def get_platform_id(self) -> str:
|
||||||
|
return self.platform_id
|
||||||
|
|
||||||
|
def get_sender_id(self) -> str:
|
||||||
|
return self.user_id
|
||||||
|
|
||||||
|
def get_group_id(self) -> str:
|
||||||
|
return self.group_id
|
||||||
|
|
||||||
|
|
||||||
|
def policy() -> AccessPolicy:
|
||||||
|
return AccessPolicy(
|
||||||
|
{
|
||||||
|
"allowed_platforms": ["aiocqhttp", "telegram"],
|
||||||
|
"private_user_ids": ["100"],
|
||||||
|
"group_permissions": [
|
||||||
|
{"group_id": "200", "user_ids": ["100", "101"]}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_identity_uses_astrbot_event_fields() -> None:
|
||||||
|
identity = identity_from_event(FakeEvent())
|
||||||
|
|
||||||
|
assert identity == PlatformIdentity(
|
||||||
|
platform_name="aiocqhttp",
|
||||||
|
platform_id="bot-1",
|
||||||
|
user_id="100",
|
||||||
|
group_id="",
|
||||||
|
session_origin="aiocqhttp:friend:100",
|
||||||
|
)
|
||||||
|
assert identity.is_private
|
||||||
|
|
||||||
|
|
||||||
|
def test_private_user_whitelist() -> None:
|
||||||
|
access_policy = policy()
|
||||||
|
assert access_policy.is_allowed(FakeEvent(user_id="100"))
|
||||||
|
assert not access_policy.is_allowed(FakeEvent(user_id="999"))
|
||||||
|
|
||||||
|
|
||||||
|
def test_group_requires_group_and_user_whitelist() -> None:
|
||||||
|
access_policy = policy()
|
||||||
|
assert access_policy.is_allowed(FakeEvent(user_id="100", group_id="200"))
|
||||||
|
assert access_policy.is_allowed(FakeEvent(user_id="101", group_id="200"))
|
||||||
|
assert not access_policy.is_allowed(FakeEvent(user_id="999", group_id="200"))
|
||||||
|
assert not access_policy.is_allowed(FakeEvent(user_id="100", group_id="999"))
|
||||||
|
|
||||||
|
|
||||||
|
def test_platform_and_missing_identity_are_rejected() -> None:
|
||||||
|
access_policy = policy()
|
||||||
|
assert not access_policy.is_allowed(FakeEvent(platform_name="discord"))
|
||||||
|
assert not access_policy.is_allowed(FakeEvent(user_id=""))
|
||||||
|
|
||||||
|
|
||||||
|
def test_mapping_group_config_is_supported() -> None:
|
||||||
|
access_policy = AccessPolicy(
|
||||||
|
{"private_user_ids": [], "group_permissions": {"200": ["100"]}}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert access_policy.is_allowed(FakeEvent(user_id="100", group_id="200"))
|
||||||
正在加载…
添加表格
在新工单中引用