Skip to content

接口定义

TokenManager

go
type TokenManager interface {
    Create(claims CustomClaims) (string, error)
    Validate(tokenString string) (Claims, bool, error)
    CreateRefresh(claims CustomClaims) (string, error)
    Refresh(refreshTokenString string) (string, error)
    ValidateInto(tokenString string, claims CustomClaims) (CustomClaims, bool, error)
    RefreshInto(refreshTokenString string, claims CustomClaims) (string, error)
    Revoke(tokenString string) error
    IsRevoked(tokenString string) (bool, error)
    ParseUnverified(tokenString string, claims any) error
    Close() error
    IsClosed() bool
}

JWT 令牌操作核心接口。所有实现必须并发安全。默认实现为 *Processor

方法按职责分为三组:

  • 令牌创建CreateCreateRefresh
  • 验证与刷新ValidateValidateIntoRefreshRefreshInto
  • 通用操作RevokeIsRevokedParseUnverifiedCloseIsClosed
interface

方法

方法签名说明
CreateCreate(claims CustomClaims) (string, error)创建访问令牌
ValidateValidate(tokenString string) (Claims, bool, error)验证令牌
CreateRefreshCreateRefresh(claims CustomClaims) (string, error)创建刷新令牌
RefreshRefresh(refreshTokenString string) (string, error)刷新令牌
ValidateIntoValidateInto(tokenString string, claims CustomClaims) (CustomClaims, bool, error)验证到自定义 Claims
RefreshIntoRefreshInto(refreshTokenString string, claims CustomClaims) (string, error)刷新到自定义 Claims
RevokeRevoke(tokenString string) error吊销令牌
IsRevokedIsRevoked(tokenString string) (bool, error)检查是否已吊销
ParseUnverifiedParseUnverified(tokenString string, claims any) error解析但不验证
CloseClose() error释放资源
IsClosedIsClosed() bool是否已关闭

实现类型

类型说明
*Processor默认实现

CustomClaims

go
type CustomClaims interface {
    GetRegisteredClaims() *RegisteredClaims
    Validate() error
}

自定义 Claims 接口。用于 CreateValidateIntoRefreshInto 等方法。

interface

验证契约

Processor 对 *Claims 和其他类型执行不同的验证路径:

类型验证行为
*Claims深度验证:所有字段(长度限制、注入模式、控制字符)
其他类型调用 Validate() + 注册声明字符串清洗(Issuer、Subject、ID、TokenType、Audience)

注意

对于非 *Claims 类型,自定义结构体字段不会被深度验证。实现者须在 Validate() 方法中自行校验所有业务字段。

方法

方法签名说明
GetRegisteredClaimsGetRegisteredClaims() *RegisteredClaims返回标准 JWT 字段
ValidateValidate() error自定义验证逻辑

实现类型

类型说明
*Claims内置 Claims 实现

BlacklistStore

go
type BlacklistStore interface {
    Add(tokenID string, expiresAt time.Time) error
    Contains(tokenID string) (bool, error)
    Close() error
}

黑名单存储后端接口。

interface

方法

方法签名说明
AddAdd(tokenID string, expiresAt time.Time) error添加到黑名单
ContainsContains(tokenID string) (bool, error)检查是否在黑名单中
CloseClose() error释放资源

RateLimitProvider

go
type RateLimitProvider interface {
    Allow(key string) bool
    Reset(key string)
    Close()
}

限流接口。Processor 在创建令牌时调用 Allow(key) 做单次判断。

关于 AllowN

接口本身只定义单次判断的 Allow。批量判断方法 AllowN(key string, n int) bool 是具体类型 *RateLimiter 的扩展方法,不属于此接口。

interface

方法

方法签名说明
AllowAllow(key string) bool检查单次请求是否允许
ResetReset(key string)重置指定 key 的限流状态
CloseClose()释放资源

实现类型

类型说明
*RateLimiter内置令牌桶实现

ClockProvider

go
type ClockProvider interface {
    Now() time.Time
}

时钟接口,用于注入时间(测试场景)。

interface

实现类型

类型说明
SystemClock系统时钟
FixedClock固定时间时钟

RateLimitKeyer

go
type RateLimitKeyer interface {
    RateLimitKey() string
}

可选接口,自定义 Claims 可实现此接口提供限流 key。限流 key 查找优先级:Subject*Claims.UserIDRateLimitKey()

interface