ForcePilot/backend/package/yuxi/channels/adapters/urbit/auth.py
Kris 49ecd949e8 feat(urbit): 实现完整的Urbit聊天适配器模块
新增了从错误定义、客户端实现到功能完整的Urbit适配器全套代码,包括认证、消息处理、目录查询、邀请管理、媒体处理、速率限制等核心功能模块
2026-05-12 00:50:27 +08:00

40 lines
1.1 KiB
Python

from __future__ import annotations
import httpx
from yuxi.channels.exceptions import ChannelAuthenticationError, ChannelConnectionError
from yuxi.utils.logging_config import logger
from .client import UrbitClient
async def authenticate_ship(
client: UrbitClient,
ship_code: str,
) -> str:
try:
r = await client.post("/~/login", json={"password": ship_code})
except httpx.RequestError as e:
raise ChannelConnectionError("Cannot reach Urbit Ship") from e
if r.status_code == 403:
raise ChannelAuthenticationError()
if r.status_code != 200:
raise ChannelAuthenticationError()
cookie = r.cookies.get(client.auth_cookie_name)
if not cookie:
raise ChannelAuthenticationError()
client.set_session_cookie(cookie)
logger.info(f"[Urbit] Authenticated to Ship ~{client.ship_name}")
return cookie
async def refresh_session_if_needed(
client: UrbitClient,
ship_code: str,
) -> str:
logger.info("[Urbit] Session expired, re-authenticating...")
return await authenticate_ship(client, ship_code)