Version: 0.9.66.dev.260504
后端: 1. 阶段 2 user/auth 服务边界落地,新增 `cmd/userauth` go-zero zrpc 服务、`services/userauth` 核心实现、gateway user API/zrpc client 与 shared contracts/ports,迁移注册、登录、刷新 token、登出、JWT、黑名单和 token 额度治理 2. gateway 与启动装配切流,`cmd/all` 只保留边缘路由、鉴权和轻量组合,通过 userauth zrpc 访问核心用户能力;拆分 MySQL/Redis 初始化与 AutoMigrate 边界,`userauth` 自迁 `users` 和 token 记账幂等表,`all` 不再迁用户表 3. 清退 Gin 单体旧 user/auth DAO、model、service、router、middleware 和 JWT handler,并同步调整 agent/schedule/cache/outbox 相关调用依赖 4. 补齐 refresh token 防并发重放、MySQL 幂等 token 记账、额度 `>=` 拦截和 RPC 错误映射,避免重复记账与内部错误透出 文档: 1. 新增《学习计划论坛与Token商店PRD》
This commit is contained in:
68
backend/shared/contracts/userauth/types.go
Normal file
68
backend/shared/contracts/userauth/types.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package userauth
|
||||
|
||||
import "time"
|
||||
|
||||
// RegisterRequest 是 user/auth 服务对外暴露的注册契约。
|
||||
// 职责边界:只描述跨进程请求字段,不承载校验、加密或持久化逻辑。
|
||||
type RegisterRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
}
|
||||
|
||||
// RegisterResponse 是注册成功后的稳定响应契约。
|
||||
type RegisterResponse struct {
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
|
||||
// LoginRequest 是登录请求契约。
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// Tokens 保持历史接口 access_token / refresh_token 字段名不变。
|
||||
type Tokens struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
// RefreshTokenRequest 保持历史 old_refresh_token 字段名不变。
|
||||
type RefreshTokenRequest struct {
|
||||
RefreshToken string `json:"old_refresh_token"`
|
||||
}
|
||||
|
||||
// ValidateAccessTokenRequest 是 gateway 调用 user/auth 做边缘鉴权时使用的内部契约。
|
||||
type ValidateAccessTokenRequest struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
// ValidateAccessTokenResponse 返回 gateway 后续轻量组合所需的最小身份信息。
|
||||
// Valid=false 预留给后续“软失败”兼容;当前实现遇到非法 token 会直接返回错误。
|
||||
type ValidateAccessTokenResponse struct {
|
||||
Valid bool `json:"valid"`
|
||||
UserID int `json:"user_id"`
|
||||
TokenType string `json:"token_type"`
|
||||
JTI string `json:"jti"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
// CheckTokenQuotaRequest 是 agent/chat 进入业务前的额度门禁请求。
|
||||
type CheckTokenQuotaRequest struct {
|
||||
UserID int `json:"user_id"`
|
||||
}
|
||||
|
||||
// AdjustTokenUsageRequest 是业务链路回写用户 token 账本的请求。
|
||||
type AdjustTokenUsageRequest struct {
|
||||
EventID string `json:"event_id"`
|
||||
UserID int `json:"user_id"`
|
||||
TokenDelta int `json:"token_delta"`
|
||||
}
|
||||
|
||||
// CheckTokenQuotaResponse 返回额度门禁判断结果。
|
||||
type CheckTokenQuotaResponse struct {
|
||||
Allowed bool `json:"allowed"`
|
||||
TokenLimit int `json:"token_limit"`
|
||||
TokenUsage int `json:"token_usage"`
|
||||
LastResetAt time.Time `json:"last_reset_at"`
|
||||
}
|
||||
46
backend/shared/ports/userauth.go
Normal file
46
backend/shared/ports/userauth.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package ports
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
contracts "github.com/LoveLosita/smartflow/backend/shared/contracts/userauth"
|
||||
)
|
||||
|
||||
// UserCommandClient 是用户入口依赖的 user/auth 命令能力集合。
|
||||
// 职责边界:
|
||||
// 1. 只描述注册、登录、刷新、登出这些入口能力;
|
||||
// 2. 不暴露 DAO、Redis、JWT 签发细节;
|
||||
// 3. 具体通信协议由 adapter 实现,调用方只按 res, err 语义处理。
|
||||
type UserCommandClient interface {
|
||||
Register(ctx context.Context, req contracts.RegisterRequest) (*contracts.RegisterResponse, error)
|
||||
Login(ctx context.Context, req contracts.LoginRequest) (*contracts.Tokens, error)
|
||||
RefreshToken(ctx context.Context, req contracts.RefreshTokenRequest) (*contracts.Tokens, error)
|
||||
Logout(ctx context.Context, accessToken string) error
|
||||
}
|
||||
|
||||
// AccessTokenValidator 是边缘鉴权依赖的最小接口。
|
||||
// 职责边界:只校验 access token 并返回后续业务所需的最小身份信息。
|
||||
type AccessTokenValidator interface {
|
||||
ValidateAccessToken(ctx context.Context, accessToken string) (*contracts.ValidateAccessTokenResponse, error)
|
||||
}
|
||||
|
||||
// TokenQuotaChecker 是 agent/chat 入口做额度门禁时依赖的最小接口。
|
||||
// 职责边界:只判断当前用户是否允许继续消费 token,不负责 token 入账。
|
||||
type TokenQuotaChecker interface {
|
||||
CheckTokenQuota(ctx context.Context, userID int) (*contracts.CheckTokenQuotaResponse, error)
|
||||
}
|
||||
|
||||
// TokenUsageAdjuster 是业务链路回写 token 账本时依赖的最小接口。
|
||||
// 职责边界:只做 token 账本增量调整,不承载鉴权与登录逻辑。
|
||||
type TokenUsageAdjuster interface {
|
||||
AdjustTokenUsage(ctx context.Context, req contracts.AdjustTokenUsageRequest) (*contracts.CheckTokenQuotaResponse, error)
|
||||
}
|
||||
|
||||
// UserAuthClient 组合当前阶段需要的 user/auth 能力。
|
||||
// 职责边界:作为统一装配口径,避免 gateway 和 core service 各自维护一份接口。
|
||||
type UserAuthClient interface {
|
||||
UserCommandClient
|
||||
AccessTokenValidator
|
||||
TokenQuotaChecker
|
||||
TokenUsageAdjuster
|
||||
}
|
||||
Reference in New Issue
Block a user