auth

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 27 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 owner 与 OAuth 的私有状态,不接触产品账号凭据。

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotInitialized     = errors.New("MCP owner is not initialized; run pixiv mcp auth init")
	ErrAlreadyInitialized = errors.New("MCP owner is already initialized; use pixiv mcp auth init --reset")
	ErrInvalidState       = errors.New("MCP state is invalid")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	ClientName   string   `json:"client_name"`
	RedirectURIs []string `json:"redirect_uris"`
	// 只由 owner 同意流程设置,防止匿名注册挤掉已签发授权码的客户端。
	OwnerApproved bool `json:"owner_approved,omitempty"`
}

Client 记录 public client metadata 与本地批准标记;map key 是 client ID,不保存 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 而非原值;已用 refresh hashes 留给重放撤销检查。

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 handler 及 bearer 包装器;canonicalBase 不从请求头推断。 不启动 listener 或读取文件;注册、授权与 token 操作要求 store 已初始化,并在每次请求内读取最新状态。

func (*Handler) RequireBearer

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

RequireBearer 包装 MCP handler;鉴权只影响新请求,不取消已授权请求。

func (*Handler) RequireCanonical

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

RequireCanonical 将入站 Host/Origin 绑定到配置的部署地址,不信任转发头。

func (*Handler) ServeHTTP

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

ServeHTTP 处理 discovery、注册、授权与 token 请求。

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 是唯一持久合同;owner verifier 同时是内存授权会话/code 的 generation。

type Store

type Store struct{ Path string }

Store 只持有路径;每次操作从原子文件读取当前状态,不缓存 verifier。

func (Store) Init

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

Init 提交成功后才返回一次性 secret。reset 保留注册与账号选择,撤销全部 grants。

func (Store) InitializePixivUser

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

InitializePixivUser adopts the initial account only if no selection has committed yet.

func (Store) Read

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

Read 读取当前快照;缺失状态不是可自动放行的空 owner。

func (Store) SelectPixivUser

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

SelectPixivUser 只保存账号 ID;调用方先核验本地账号,不在此复制凭据或修改 CLI default。

func (Store) SelectPixivUserIf

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

SelectPixivUserIf checks local eligibility and commits under the same state lock. eligible must not write MCP state; it receives identity only, never credentials.

Jump to

Keyboard shortcuts

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