新增 Microsoft Teams 渠道扩展,支持在 Yuxi 平台中集成 Microsoft Teams 协作平台。 包含以下功能模块: - sdk: Bot Framework SDK 封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - auth: JWT 认证 - jwks: JWKS 密钥管理 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - state: 状态管理 - runtime: 运行时管理 - actions: 动作处理 - adaptive_card: 自适应卡片 - task_modules: 任务模块 - message_extension: 消息扩展 - proactive: Proactive Messaging - graph: Microsoft Graph API 集成 - graph_teams: Teams 操作 - graph_members: 成员管理 - graph_messages: 消息获取 - graph_thread: 线程管理 - graph_users: 用户管理 - graph_upload: 文件上传 - files: 文件处理 - file_consent: 文件授权 - conversations: 会话存储 - mentions: @提及处理 - threading: 线程管理 - reactions: 表情反应 - polls: 投票功能 - meetings: 会议集成 - feedback: 反馈处理 - sso: 单点登录 - deep_links: 深层链接 - incoming_webhook: 入站 Webhook - localization: 本地化 - user_agent: 用户代理 - sent_message_cache: 消息缓存 - types: 类型定义
168 lines
4.7 KiB
Python
168 lines
4.7 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import secrets
|
|
import time
|
|
from dataclasses import dataclass, field
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
SSO_STATE_TTL_SECONDS = 300
|
|
|
|
|
|
@dataclass
|
|
class SSOTokenState:
|
|
state: str
|
|
token: str = ""
|
|
created_at: float = field(default_factory=time.monotonic)
|
|
exchanged: bool = False
|
|
user_id: str = ""
|
|
tenant_id: str = ""
|
|
error: str = ""
|
|
|
|
@property
|
|
def is_expired(self) -> bool:
|
|
return time.monotonic() - self.created_at > SSO_STATE_TTL_SECONDS
|
|
|
|
|
|
class SSOTokenStore:
|
|
def __init__(self):
|
|
self._states: dict[str, SSOTokenState] = {}
|
|
|
|
def create_state(self, tenant_id: str = "", user_id: str = "") -> str:
|
|
state = secrets.token_hex(16)
|
|
self._states[state] = SSOTokenState(state=state, tenant_id=tenant_id, user_id=user_id)
|
|
self._cleanup_expired()
|
|
return state
|
|
|
|
def verify_state(self, state: str) -> SSOTokenState | None:
|
|
stored = self._states.get(state)
|
|
if not stored:
|
|
return None
|
|
if stored.is_expired:
|
|
self._states.pop(state, None)
|
|
return None
|
|
return stored
|
|
|
|
def exchange_token(self, state: str, token: str) -> bool:
|
|
stored = self.verify_state(state)
|
|
if not stored:
|
|
return False
|
|
stored.token = token
|
|
stored.exchanged = True
|
|
return True
|
|
|
|
def revoke_state(self, state: str) -> None:
|
|
self._states.pop(state, None)
|
|
|
|
def list_active_states(self) -> list[dict]:
|
|
self._cleanup_expired()
|
|
return [
|
|
{
|
|
"state": s.state,
|
|
"exchanged": s.exchanged,
|
|
"user_id": s.user_id,
|
|
"tenant_id": s.tenant_id,
|
|
}
|
|
for s in self._states.values()
|
|
]
|
|
|
|
def _cleanup_expired(self) -> None:
|
|
expired = [s for s, st in self._states.items() if st.is_expired]
|
|
for s in expired:
|
|
self._states.pop(s, None)
|
|
|
|
|
|
async def exchange_token_with_microsoft(
|
|
tenant_id: str,
|
|
client_id: str,
|
|
client_secret: str,
|
|
code: str,
|
|
redirect_uri: str,
|
|
) -> dict | None:
|
|
try:
|
|
import httpx
|
|
|
|
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
|
|
body = {
|
|
"client_id": client_id,
|
|
"client_secret": client_secret,
|
|
"code": code,
|
|
"redirect_uri": redirect_uri,
|
|
"grant_type": "authorization_code",
|
|
}
|
|
async with httpx.AsyncClient(timeout=30.0) as client:
|
|
resp = await client.post(token_url, data=body)
|
|
resp.raise_for_status()
|
|
return resp.json()
|
|
except Exception as e:
|
|
logger.warning("Token exchange failed: %s", e)
|
|
return None
|
|
|
|
|
|
async def exchange_token_on_behalf_of(
|
|
tenant_id: str,
|
|
client_id: str,
|
|
client_secret: str,
|
|
user_token: str,
|
|
scope: str,
|
|
) -> dict | None:
|
|
try:
|
|
import httpx
|
|
|
|
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
|
|
body = {
|
|
"client_id": client_id,
|
|
"client_secret": client_secret,
|
|
"assertion": user_token,
|
|
"scope": scope,
|
|
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
"requested_token_use": "on_behalf_of",
|
|
}
|
|
async with httpx.AsyncClient(timeout=30.0) as client:
|
|
resp = await client.post(token_url, data=body)
|
|
resp.raise_for_status()
|
|
return resp.json()
|
|
except Exception as e:
|
|
logger.warning("OBO token exchange failed: %s", e)
|
|
return None
|
|
|
|
|
|
def build_sso_card(connection_name: str, state: str, text: str = "") -> dict:
|
|
card = {
|
|
"type": "AdaptiveCard",
|
|
"version": "1.4",
|
|
"body": [
|
|
{
|
|
"type": "TextBlock",
|
|
"text": text or "🔐 登录认证",
|
|
"weight": "Bolder",
|
|
"size": "Medium",
|
|
},
|
|
{
|
|
"type": "TextBlock",
|
|
"text": "请点击下方按钮完成 Microsoft 365 身份认证",
|
|
"wrap": True,
|
|
},
|
|
],
|
|
"actions": [
|
|
{
|
|
"type": "Action.Submit",
|
|
"title": "🔑 登录 Microsoft 365",
|
|
"data": {
|
|
"action": "sso_signin",
|
|
"state": state,
|
|
"connection_name": connection_name,
|
|
},
|
|
},
|
|
],
|
|
}
|
|
return card
|
|
|
|
|
|
def build_sso_attachment(connection_name: str, state: str, text: str = "") -> dict:
|
|
from .adaptive_card import build_attachment
|
|
|
|
card = build_sso_card(connection_name, state, text)
|
|
return build_attachment(card)
|