Help us shape the future of Go! Take the 2026 Go Developer Survey

auth

package
v0.32.41 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New("invalid token")
	ErrExpiredToken = errors.New("token has expired")
	ErrTokenRevoked = errors.New("token has been revoked")
)

Functions

func GenerateAdminToken

func GenerateAdminToken(userID uint, username string) (string, error)

GenerateAdminToken 生成管理员 token

func GenerateToken

func GenerateToken(userID uint, username, role string) (string, error)

GenerateToken 生成 JWT token

func IsTokenRevoked

func IsTokenRevoked(ctx context.Context, token string) bool

IsTokenRevoked 查询 token 是否已注销。 Redis 查询失败时回退内存兜底(与 RevokeToken 的降级写配对,故障窗口内 写入内存的吊销仍可被查到)。

func RefreshToken

func RefreshToken(ctx context.Context, tokenString string) (string, error)

RefreshToken 刷新 token。已注销(黑名单)的 token 拒绝刷新, 堵住"登出后旧 token 仍可换新"的撤销绕过(设计文档 §9.6)。

轮换语义(2026-10-05 审计 P1 修复):签发新 token 的同时把旧 token 按剩余 有效期加入黑名单——否则被盗 token 可与真用户并行使用到自然过期,且每次 刷新都无法止损。

func RevokeToken

func RevokeToken(ctx context.Context, token string, remaining time.Duration)

RevokeToken 注销 token(至其自然过期前一直拦截)。 Redis 故障时降级写内存兜底(2026-10-05 审计 P2:此前 Set 错误被吞、查询 fail-open,Redis 抖动窗口内已注销 token 静默恢复可用)。

func SessionExpiry

func SessionExpiry() time.Duration

SessionExpiry 当前会话时长(测试/诊断用)。

func SetBlacklistRedis

func SetBlacklistRedis(rdb *redis.Client)

SetBlacklistRedis 注入 Redis(bootstrap 调用;nil = 纯内存模式)。 nil 归一化:typed-nil 接口会骗过内存模式守卫(!= nil 判真)。

func SetEpochLoader

func SetEpochLoader(fn func(userID uint) int)

SetEpochLoader 注入纪元加载器(bootstrap 调用一次;nil 关闭)。

func SetJWTSecret

func SetJWTSecret(secret string)

SetJWTSecret 设置 JWT secret(写锁,从配置注入)

func SetSessionExpiry

func SetSessionExpiry(d time.Duration)

SetSessionExpiry 设置会话时长;<=0 视为非法,保留原值。 仅影响新签发的 token,已签发会话到自然过期。

Types

type Claims

type Claims struct {
	UserID   uint   `json:"user_id"`
	Username string `json:"username"`
	Role     string `json:"role"`
	// Epoch 会话纪元快照:签发时用户的 session_epoch。改密/封禁/降权会使
	// DB 纪元 +1,纪元不匹配的旧 token 即时失效(认证中间件回查比对)。
	Epoch int `json:"sess_epoch,omitempty"`
	jwt.RegisteredClaims
}

Claims JWT claims

func ParseToken

func ParseToken(tokenString string) (*Claims, error)

ParseToken 解析 JWT token

Jump to

Keyboard shortcuts

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