ForcePilot/backend/package/yuxi/external_systems/integrations/slack/credentials.py
Kris 74709ba2d3 feat: 完成外部系统限界上下文核心代码实现
新增六边形架构核心代码包,包含:
1. 协议适配器层:HTTP/SMTP/IMAP/SSH/GRPC等多协议适配器实现
2. 认证插件体系:基础认证、API密钥、HMAC等多类型认证插件
3. 执行编排框架:工具执行器、上下文构建、运行时治理组件
4. 用例端口与DTO:定义领域服务端口与数据传输对象
5. 厂商集成包框架:支持第三方系统集成扩展
6. 基础设施装配层:实现依赖注入与服务装配

所有代码遵循六边形架构设计原则,实现端口与适配器解耦,支持动态扩展与自动发现。
2026-06-20 22:12:42 +08:00

78 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Slack 厂商专属凭证 Pydantic 模型。
定义两个 ``AuthCredential`` 子类,分别对应 Bot User OAuth Token``xoxb-``)与
App-Level Token``xapp-``)两种静态 Token 认证方式。
``oauth2_authorization_code`` 复用框架级 ``OAuth2AuthCodeCredential``,不在本文件定义。
字段与 ``auth_plugins.py`` 中 ``resolve()`` 构造凭证时读取的 ``auth_config`` 字段对齐:
- ``SlackBotTokenCredential`` ↔ ``SlackBotTokenAuthPlugin``
- ``SlackAppTokenCredential`` ↔ ``SlackAppTokenAuthPlugin``
继承关系说明:
- ``SlackBotTokenCredential`` 继承 ``BearerCredential``,以复用 HTTP 执行器
``_apply_credential`` 中的 ``isinstance(credential, BearerCredential)`` 分派,
自动注入 ``Authorization: Bearer {token}`` 头部(参考 Notion
``NotionIntegrationTokenCredential`` / HubSpot ``PrivateAppTokenCredential`` 模式)。
- ``SlackAppTokenCredential`` 继承 ``AuthCredential``,仅用于 Socket Mode WebSocket
握手,不经过 HTTP 执行器,无需 Bearer 头注入。
"""
from __future__ import annotations
from pydantic import ConfigDict, Field
from yuxi.external_systems.framework.auth_plugins.credentials import (
AuthCredential,
BearerCredential,
)
class SlackBotTokenCredential(BearerCredential):
"""Slack Bot User OAuth Token 凭证。
承载 ``xoxb-`` 前缀的 Bot Token适用于"已通过 Slack App 管理界面手动安装并复制
Bot Token"的场景。``is_token_based=True`` 但不注册刷新策略(静态 Token
与框架级 ``bearer`` 设计一致。
继承 ``BearerCredential`` 以复用 HTTP 执行器 ``_apply_credential`` 中的
``isinstance(credential, BearerCredential)`` 分派,自动注入
``Authorization: Bearer {token}`` 头部(参考 Notion
``NotionIntegrationTokenCredential`` / HubSpot ``PrivateAppTokenCredential`` 模式)。
``scopes`` 字段不参与运行时鉴权Slack 服务端会校验),仅用于客户端调用前的
预检与日志展示。
"""
model_config = ConfigDict(populate_by_name=True, extra="ignore")
auth_type: str = "slack_bot_token"
token: str = Field(..., description="Bot User OAuth Token必须以 xoxb- 开头")
scopes: list[str] = Field(
default_factory=list,
description="Token 已授予的 scope 列表(用于调用前校验)",
)
class SlackAppTokenCredential(AuthCredential):
"""Slack App-Level Token 凭证(仅 Socket Mode 使用)。
承载 ``xapp-`` 前缀的 App-Level Token由 Slack app 设置页生成
Settings > Basic Information > App-Level Tokens必须包含 ``connections:write``
scope仅用于 Socket Mode 建立 WebSocket 连接,不直接调用 Web API。
不继承 ``BearerCredential``App-Level Token 不经过 HTTP 执行器,无需 Bearer 头注入。
若误将 ``slack_app_token`` 配置为 HTTP 工具的认证类型HTTP 执行器
``_apply_credential`` 不会注入任何认证头Slack 将返回 ``not_authed`` 错误,
此为预期行为App-Level Token 不应用于 Web API 调用)。
"""
model_config = ConfigDict(populate_by_name=True, extra="ignore")
auth_type: str = "slack_app_token"
token: str = Field(..., description="App-Level Token必须以 xapp- 开头")
required_scope: str = Field(
default="connections:write",
description="Socket Mode 必备 scope",
)