Documentation
¶
Overview ¶
Package auth 为 MCP HTTP 端点提供单 owner 的 OAuth 授权与私有的持久状态。
包的组成:NewHandler 组装 discovery、动态客户端注册、授权与 token 路由, Handler.RequireBearer 校验访问令牌,Handler.RequireCanonical 把请求绑定到配置的 部署地址;Store 保存客户端、grant 与选中的 Pixiv 账号 ID。授权流程的状态机在 authorize.go,token 兑换与轮换在 token.go。这里只保存 MCP owner 自身的凭据摘要, Pixiv 账号凭据仍由账号存储持有。
Index ¶
- Variables
- func ParseBaseURL(canonicalBase string) (*url.URL, error)
- type Client
- type Grant
- type Handler
- type State
- type Store
- func (s Store) Init(ctx context.Context, reset bool) (string, error)
- func (s Store) InitializePixivUser(ctx context.Context, userID int64) (int64, error)
- func (s Store) Read(ctx context.Context) (State, error)
- func (s Store) SelectPixivUser(ctx context.Context, userID int64) error
- func (s Store) SelectPixivUserIf(ctx context.Context, userID int64, eligible func(int64) (bool, error)) (bool, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotInitialized 表示状态文件尚不存在,调用方需要先执行 pixiv mcp auth init。 ErrNotInitialized = errors.New("MCP owner is not initialized; run pixiv mcp auth init") // ErrAlreadyInitialized 表示状态文件已存在而本次初始化没有要求 reset。 ErrAlreadyInitialized = errors.New("MCP owner is already initialized; use pixiv mcp auth init --reset") // ErrInvalidState 表示状态文件内容不满足 Read 校验的结构。 ErrInvalidState = errors.New("MCP state is invalid") )
Functions ¶
Types ¶
type Client ¶
type Client struct {
ClientName string `json:"client_name"`
RedirectURIs []string `json:"redirect_uris"`
// OwnerApproved 只由 owner 同意流程设置,避免匿名注册把已签发授权码的客户端挤掉。
OwnerApproved bool `json:"owner_approved,omitempty"`
}
Client 保存一个已注册客户端的公开元数据;map key 即 client ID,也没有 client secret。
type Grant ¶
type Grant struct {
ClientID string `json:"client_id"`
Resource string `json:"resource"`
Scope string `json:"scope"`
AccessTokens map[string]time.Time `json:"access_tokens"`
RefreshHash string `json:"refresh_hash"`
UsedRefreshHashes []string `json:"used_refresh_hashes"`
Revoked bool `json:"revoked"`
}
Grant 保存 token 的 verifier 而不保存原值;UsedRefreshHashes 记录已用过的 refresh token 摘要,重放时据此撤销整条 grant。
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler 持有 OAuth 路由与 canonical resource 的 bearer 边界,listener 由调用方管理。
func NewHandler ¶
NewHandler 组装 discovery、DCR、authorize、token 路由和 bearer 包装器。 canonicalBase 只来自显式配置,不从请求头推断;函数本身不启动 listener。store 必须 已初始化,注册、授权与 token 操作都在请求内读取最新状态。
func (*Handler) RequireBearer ¶
RequireBearer 只对新请求校验访问令牌;已进入处理的请求不会因令牌过期被中断。
func (*Handler) RequireCanonical ¶
RequireCanonical 要求请求的 Host 等于配置的部署地址;Origin 缺省时只校验 Host, 提供时也必须匹配。转发头不参与判断。
type State ¶
type State struct {
Version int `json:"version"`
OwnerVerifier string `json:"owner_verifier"`
Clients map[string]Client `json:"clients"`
Grants map[string]Grant `json:"grants"`
SelectedPixivUserID int64 `json:"selected_pixiv_user_id"`
}
State 持久化 auth 的全部状态。OwnerVerifier 同时充当内存中授权会话与 code 的 generation,reset 后旧会话自然失效。
type Store ¶
type Store struct{ Path string }
Store 只保存状态文件路径,每次操作都从文件读最新快照,不缓存 verifier 或 grant。
func (Store) Init ¶
Init 在状态落盘后才返回一次性 owner secret。reset 保留客户端注册与账号选择, 撤销全部 grant,并记录已有 grant 的客户端曾获批准。
func (Store) InitializePixivUser ¶
InitializePixivUser 只在尚无账号选择时采用 initial 账号,并返回最终选中的账号 ID。
func (Store) SelectPixivUser ¶
SelectPixivUser 只写入账号 ID;调用方须先确认本地账号存在,这里不复制凭据,也不改 CLI 的默认账号。