auth

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

func HashPassword(logger *zap.Logger, plain string) (string, error)

HashPassword 将明文密码 hash 为 bcrypt 字符串 空密码视为无效输入,返回 error

func VerifyPassword

func VerifyPassword(logger *zap.Logger, hash, plain string) bool

VerifyPassword 验证明文密码是否与 bcrypt hash 匹配 不匹配返回 false,不返回 error(避免调用方区分 hash 错误与密码错误)

Types

type Blacklist

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

Blacklist 线程安全的 JTI 黑名单,TTL 后自动清理

func NewBlacklist

func NewBlacklist(logger *zap.Logger) *Blacklist

NewBlacklist 创建新的 Blacklist

func (*Blacklist) Add

func (b *Blacklist) Add(jti string, expiry time.Time)

Add 将 JTI 加入黑名单,到期时间为 expiry

func (*Blacklist) IsBlocked

func (b *Blacklist) IsBlocked(jti string) bool

IsBlocked 检查 JTI 是否在黑名单中(自动跳过已过期条目)

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

func NewManager(logger *zap.Logger, secret string) (*Manager, error)

NewManager 创建 JWT Manager secret 为签名密钥,不得为空

func (*Manager) Blacklist

func (m *Manager) Blacklist(jti string, expiry time.Time)

Blacklist 将 JTI 加入黑名单(TTL 后自动清理)

func (*Manager) IsBlacklisted

func (m *Manager) IsBlacklisted(jti string) bool

IsBlacklisted 检查 JTI 是否在黑名单中

func (*Manager) Parse

func (m *Manager) Parse(tokenStr string) (*JWTClaims, error)

Parse 解析并验证 JWT,返回 claims 返回的 error 可通过 errors.Is 区分: ErrTokenExpired / ErrInvalidToken / ErrTokenRevoked

func (*Manager) Sign

func (m *Manager) Sign(claims JWTClaims, ttl time.Duration) (string, error)

Sign 签发 JWT,自动生成唯一 JTI

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 是否需要刷新(即将过期但尚未过期)

func (*TokenStore) Save

func (s *TokenStore) Save(dir string, tf *TokenFile) error

Save 将 token 写入文件,并尽力设置 0600 权限(Windows 忽略权限错误)

Jump to

Keyboard shortcuts

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