Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrTokenExpired = errors.New("token expired") ErrInvalidToken = errors.New("invalid token") ErrTokenRevoked = errors.New("token revoked") )
公共错误类型,供调用方 errors.Is 使用
Functions ¶
func DefaultTokenDir ¶
func DefaultTokenDir() string
DefaultTokenDir 返回跨平台默认 token 目录 Windows: %APPDATA%\pairproxy Linux: ~/.config/pairproxy
func HashPassword ¶
HashPassword 将明文密码 hash 为 bcrypt 字符串 空密码视为无效输入,返回 error
Types ¶
type Blacklist ¶
type Blacklist struct {
// contains filtered or unexported fields
}
Blacklist 线程安全的 JTI 黑名单,TTL 后自动清理
func (*Blacklist) StartCleanup ¶
func (b *Blacklist) StartCleanup(ctx interface{ Done() <-chan struct{} })
StartCleanup 启动后台清理 goroutine,每 5 分钟运行一次
type JWTClaims ¶
type JWTClaims struct {
UserID string `json:"sub"`
Username string `json:"username"`
GroupID string `json:"group_id"`
Role string `json:"role"` // "user" | "admin"
JTI string `json:"jti"` // 唯一 ID,用于撤销
jwt.RegisteredClaims
}
JWTClaims JWT payload 定义
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager JWT 签发、验证与黑名单管理
func NewManager ¶
NewManager 创建 JWT Manager secret 为签名密钥,不得为空
func (*Manager) IsBlacklisted ¶
IsBlacklisted 检查 JTI 是否在黑名单中
func (*Manager) Parse ¶
Parse 解析并验证 JWT,返回 claims 返回的 error 可通过 errors.Is 区分: ErrTokenExpired / ErrInvalidToken / ErrTokenRevoked
func (*Manager) StartCleanup ¶
func (m *Manager) StartCleanup(ctx interface{ Done() <-chan struct{} })
StartCleanup 启动后台黑名单清理(ctx 取消时停止)
type TokenFile ¶
type TokenFile struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresAt time.Time `json:"expires_at"`
ServerAddr string `json:"server_addr"` // 登录时使用的 s-proxy 地址
Username string `json:"username"` // 登录用户名(展示用)
}
TokenFile 存储在本地的 token 信息(c-proxy 使用)
type TokenStore ¶
type TokenStore struct {
// contains filtered or unexported fields
}
TokenStore 本地 token 文件的读写接口实现
func NewTokenStore ¶
func NewTokenStore(logger *zap.Logger, refreshThreshold time.Duration) *TokenStore
NewTokenStore 创建 TokenStore
func (*TokenStore) Delete ¶
func (s *TokenStore) Delete(dir string) error
Delete 删除本地 token 文件(logout 时调用)
func (*TokenStore) IsValid ¶
func (s *TokenStore) IsValid(tf *TokenFile) bool
IsValid 检查 access_token 是否有效 判断标准:token 非空 且 未达到 refreshThreshold 前的过期时间
func (*TokenStore) Load ¶
func (s *TokenStore) Load(dir string) (*TokenFile, error)
Load 从指定目录加载 token.json 文件不存在时返回 nil, nil(非 error)
func (*TokenStore) NeedsRefresh ¶
func (s *TokenStore) NeedsRefresh(tf *TokenFile) bool
NeedsRefresh 检查 access_token 是否需要刷新(即将过期但尚未过期)