middleware

package
v1.0.0-rc.40 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package middleware 放请求主体(路线图 M12):谁发起的请求、有哪些权限范围。

网页与现有 /api/* 只认 session;API 令牌(以后还有远程设备)只能访问 /api/app/v1/*、/mcp 与 qB 兼容入口, 由那几个入口自己解析主体、检查权限范围。

Index

Constants

View Source
const (
	KindSession      = "session"
	KindAPIToken     = "api_token"
	KindRemoteDevice = "remote_device"
	// KindMCP 是经 /mcp 调工具的 API 令牌(工具在进程内调用 App API)
	KindMCP = "mcp"
	// KindAssistant 是内置 AI 助手(经进程内的 MCP 工具调用 App API),ID 是会话属主,例如 web:admin、chatops:3
	KindAssistant = "assistant"
)

主体的种类。

Variables

This section is empty.

Functions

func BearerToken

func BearerToken(r *http.Request) (string, bool)

BearerToken 取 `Authorization: Bearer <令牌>` 里的令牌(scheme 不分大小写)。只认请求头,不认查询串。

func WithPrincipal

func WithPrincipal(ctx context.Context, p *Principal) context.Context

WithPrincipal 把主体放进 context。

Types

type Principal

type Principal struct {
	Kind string
	// ID 是 session 的用户名、令牌编号、设备编号,或者 AI 助手会话的属主
	ID string
	// Name 是令牌或设备的名字
	Name   string
	Scopes []string
}

Principal 是发起请求的主体。session 拥有全部权限范围;令牌与设备只有自己的。

func PrincipalFrom

func PrincipalFrom(ctx context.Context) *Principal

PrincipalFrom 从 context 取主体,没有时返回 nil。

func (*Principal) Has

func (p *Principal) Has(scope string) bool

Has 报告主体有没有这个权限范围(精确匹配,不支持通配)。

Jump to

Keyboard shortcuts

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