Documentation
¶
Overview ¶
Package apitoken 管理 API 令牌(路线图 M12):给 App、MCP 与 qB 兼容入口用,网页登录仍然只认 session。
令牌是 `ptt_<id>_<secret>`,secret 是 32 字节随机数的 base64url。库里只存 secret 的 SHA-256:按 id 找到那一行, 再用常量时间比较散列。高熵的随机令牌不需要 bcrypt,每个请求也不用做一次慢散列。 格式不对、id 不存在、secret 不对、过期、已撤销一律返回同一个 ErrUnauthorized,不给枚举的线索。
Index ¶
- Constants
- Variables
- func ValidScope(s string) bool
- type CreateInput
- type Store
- func (s *Store) Create(ctx context.Context, in CreateInput, by string) (Token, string, error)
- func (s *Store) Get(ctx context.Context, id uint) (Token, error)
- func (s *Store) List(ctx context.Context) ([]Token, error)
- func (s *Store) Revoke(ctx context.Context, id uint) error
- func (s *Store) Verify(ctx context.Context, plain string) (Token, error)
- type Token
Constants ¶
View Source
const ( ScopeAppRead = "app:read" ScopeAppWrite = "app:write" ScopeMCPRead = "mcp:read" ScopeMCPWrite = "mcp:write" ScopeQbitCompat = "qbit:compat" )
权限范围:精确匹配,不支持通配。
Variables ¶
View Source
var ( ErrInvalid = errors.New("参数不对") ErrNotFound = errors.New("没有这个令牌") )
View Source
var Scopes = []string{ScopeAppRead, ScopeAppWrite, ScopeMCPRead, ScopeMCPWrite, ScopeQbitCompat}
Scopes 是所有权限范围,也是令牌里 scope 的排列顺序。
Functions ¶
Types ¶
type CreateInput ¶
type CreateInput struct {
Name string `json:"name"`
Scopes []string `json:"scopes"`
ExpiresInDays int `json:"expires_in_days"`
}
CreateInput 是新建令牌的内容。ExpiresInDays 为 0 时不过期。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store 存取令牌。
type Token ¶
type Token struct {
ID uint `json:"id"`
Name string `json:"name"`
Scopes []string `json:"scopes"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
CreatedBy string `json:"created_by"`
CreatedAt time.Time `json:"created_at"`
}
Token 是一个令牌(不含明文与散列)。
Click to show internal directories.
Click to hide internal directories.