auth

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 28 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

View Source
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

func ParseBaseURL added in v1.3.0

func ParseBaseURL(canonicalBase string) (*url.URL, error)

ParseBaseURL 返回去掉末尾斜杠的公开 MCP base URL,并拒绝会使 issuer 与 discovery 路径不一致的配置。客户端会归一化 dot segment,所以这类路径必须在配置阶段就挡下。

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。

func (Grant) Usable added in v1.3.0

func (g Grant) Usable(resource string) bool

Usable 报告 grant 未撤销、资源匹配且 scope 为 mcp;令牌是否过期由调用方另查。

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler 持有 OAuth 路由与 canonical resource 的 bearer 边界,listener 由调用方管理。

func NewHandler

func NewHandler(canonicalBase string, store Store) (*Handler, error)

NewHandler 组装 discovery、DCR、authorize、token 路由和 bearer 包装器。 canonicalBase 只来自显式配置,不从请求头推断;函数本身不启动 listener。store 必须 已初始化,注册、授权与 token 操作都在请求内读取最新状态。

func (*Handler) RequireBearer

func (h *Handler) RequireBearer(next http.Handler) http.Handler

RequireBearer 只对新请求校验访问令牌;已进入处理的请求不会因令牌过期被中断。

func (*Handler) RequireCanonical

func (h *Handler) RequireCanonical(next http.Handler) http.Handler

RequireCanonical 要求请求的 Host 等于配置的部署地址;Origin 缺省时只校验 Host, 提供时也必须匹配。转发头不参与判断。

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 分发 discovery、注册、授权与 token 请求;其他路径返回 404。

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

func (s Store) Init(ctx context.Context, reset bool) (string, error)

Init 在状态落盘后才返回一次性 owner secret。reset 保留客户端注册与账号选择, 撤销全部 grant,并记录已有 grant 的客户端曾获批准。

func (Store) InitializePixivUser

func (s Store) InitializePixivUser(ctx context.Context, userID int64) (int64, error)

InitializePixivUser 只在尚无账号选择时采用 initial 账号,并返回最终选中的账号 ID。

func (Store) Read

func (s Store) Read(ctx context.Context) (State, error)

Read 返回当前快照。状态文件不存在时返回 ErrNotInitialized,不会当作空 owner 放行。

func (Store) SelectPixivUser

func (s Store) SelectPixivUser(ctx context.Context, userID int64) error

SelectPixivUser 只写入账号 ID;调用方须先确认本地账号存在,这里不复制凭据,也不改 CLI 的默认账号。

func (Store) SelectPixivUserIf

func (s Store) SelectPixivUserIf(ctx context.Context, userID int64, eligible func(int64) (bool, error)) (bool, error)

SelectPixivUserIf 在同一把状态文件锁内检查 eligible 并提交选择,避免用过期信息覆盖 显式选择。eligible 只能读,拿到账号身份而不会拿到凭据。

Jump to

Keyboard shortcuts

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