middleware

package
v0.31.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RequireAuth

func RequireAuth(store BotTokenStore, sessionChecker func(w http.ResponseWriter, r *http.Request) bool) func(http.Handler) http.Handler

RequireAuth 组合中间件:先尝试 Bearer,失败则尝试 Session。 - Bearer 通过:注入 BotIdentity,继续 - Bearer 失败且 Session 存在:继续(原 session 中间件行为) - Bearer 失败且 Session 不存在:返回 401 JSON(不重定向)

func RequireBearer

func RequireBearer(store BotTokenStore) func(http.Handler) http.Handler

RequireBearer 创建 Bearer Token 验证中间件。 期望 Authorization 头格式为 "Bearer <token>"。 合法 token 会注入 BotIdentity 到 context;非法 token 返回 401 JSON。

Types

type BotIdentity

type BotIdentity struct {
	TokenKind string // "bind_code" 或 "bearer"(api_token)
	Scope     string // 权限范围,如 "chatops:*" 或 "mcp:read"
	ExpiresAt *time.Time
}

BotIdentity 注入到 context 的用户身份信息。

func GetIdentity

func GetIdentity(ctx context.Context) *BotIdentity

GetIdentity 从 context 读取 BotIdentity。

type BotTokenStore

type BotTokenStore interface {
	// Lookup 按明文 token 查询对应的 bcrypt hash 和其他属性。
	// 返回 (*BotToken, error);token 不存在时返回 (*BotToken, nil)。
	Lookup(ctx context.Context, plainToken string) (*models.BotToken, error)
}

BotTokenStore 定义 Bearer Token 查询接口。 实现由 T13 audit/store 任务完成;此任务使用 mock 实现测试。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL