modify: auth插件拆分,将token签发部分交给网关

This commit is contained in:
2026-09-10 16:43:43 +08:00
parent 5ec90a5575
commit c139b0c1d3
23 changed files with 691 additions and 290 deletions
+21
View File
@@ -0,0 +1,21 @@
"""模型导出 — auth_oauth(含插件自有绑定表 + 请求/响应模型)"""
from .pydantic import (
GithubAuthorizeGetResponse,
GithubCallbackPostRequest,
PhoneLoginPostRequest,
PhoneSendCodePostRequest,
PhoneSendCodePostResponse,
)
from chongming_auth import LoginPostResponse
from .identity import IdentityBinding
__all__ = [
"GithubAuthorizeGetResponse",
"GithubCallbackPostRequest",
"LoginPostResponse",
"PhoneLoginPostRequest",
"PhoneSendCodePostRequest",
"PhoneSendCodePostResponse",
"IdentityBinding",
]
+34
View File
@@ -0,0 +1,34 @@
"""身份源绑定表 — 第三方身份(github open_id / 手机号 / ...)与 users 账号的映射。
插件自有表,建在共享 auth 库(与 users 同库不同表):
chongming-auth 拥有的 users schema 保持最小不改动,身份源扩展物归插件。
"""
from datetime import datetime
from uuid import uuid4
from sqlalchemy import UniqueConstraint, func, Column, DateTime
from sqlmodel import Field, SQLModel
def _pk() -> str:
return f"{uuid4()}_{int(datetime.now().timestamp())}"
class IdentityBinding(SQLModel, table=True):
__tablename__ = "identity_bindings" # type: ignore
__table_args__ = (
UniqueConstraint("provider", "provider_uid", name="uq_provider_uid"),
)
id: str = Field(default_factory=_pk, primary_key=True, description="绑定记录ID")
user_id: str = Field(index=True, max_length=255, description="关联的 users.id")
provider: str = Field(index=True, max_length=32, description="身份源: github / phone / ...")
provider_uid: str = Field(max_length=255, description="身份源侧唯一标识(github user id / 手机号)")
created_at: datetime = Field(
sa_column=Column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False,
comment="创建时间",
)
)
+43
View File
@@ -0,0 +1,43 @@
"""auth_oauth 请求/响应模型。
框架契约:所有插件须有 models/pydantic.py(gen_models 的产物位置)——
网关契约校验按 plugins.<type>.models.pydantic → src.plugins.... → workers....
顺序探测,缺失时会以 src.plugins. 前缀二次执行插件包(父包导入链),
插件内自定义 SQLModel 表会在全局元数据中重复定义导致启动失败。
本插件模型由手写(等价 gen_models 产物),端点函数以类型注解直接绑定。
登录响应 LoginPostResponse 直接从 chongming_auth 导入(共享契约)。
"""
from pydantic import BaseModel, Field
class GithubAuthorizeGetResponse(BaseModel):
"""授权跳转 URL 响应"""
result: str = Field(..., description="success/fail")
authorize_url: str = Field(default="", description="GitHub 授权跳转 URL")
state: str = Field(default="", description="防 CSRF state(一次性,300s)")
expires_in: int = Field(default=300, description="state 有效期(秒)")
class GithubCallbackPostRequest(BaseModel):
"""GitHub 回调请求"""
code: str = Field(..., description="GitHub 回跳授权码")
state: str = Field(..., description="authorize 返回的 state")
class PhoneSendCodePostRequest(BaseModel):
"""发送验证码请求"""
phone: str = Field(..., description="手机号")
class PhoneSendCodePostResponse(BaseModel):
"""发送验证码响应"""
result: str = Field(..., description="success/fail")
message: str = Field(..., description="结果消息")
expires_in: int = Field(default=300, description="验证码有效期(秒)")
dev_code: str | None = Field(default=None, description="验证码(仅演示模式返回)")
class PhoneLoginPostRequest(BaseModel):
"""验证码登录请求"""
phone: str = Field(..., description="手机号")
code: str = Field(..., description="短信验证码")