JWT 工具
ling-base common/jwtutil 模块文档
在线 Playground
在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。
另见专题文档:JWT 安全文档。以下为
common/jwtutil包 README 全文。
jwtutil
Reusable JWT authentication layer built on common/crypto, adding token pairs, refresh flows, revocation, and HTTP helpers.
Features
- Token pairs: short-lived access token + long-lived refresh token
- Refresh flow: issue new access token from a valid refresh token
- Revocation / blacklist via pluggable
TokenStore - Claims builder with roles and permissions
- HTTP helpers: bearer token extraction, context-based claim retrieval
Key types
Auth-- the main JWT auth managerConfig-- manager configuration (secret/keys, TTLs, store, leeway)Claims-- decoded JWT claims with auth-specific fields (roles, permissions)TokenPair-- access + refresh token responseTokenStore-- revocation interface (Revoke,IsRevoked,MarkUsed)MemoryTokenStore-- in-process token store implementationTokenType--TokenTypeAccess,TokenTypeRefresh
Key functions
New(cfg)-- create an Auth managerAuth.Login(subject, opts...)-- issue a token pairAuth.Verify(token)-- verify and return claimsAuth.Refresh(refreshToken)-- issue new pair from refresh tokenAuth.Revoke(ctx, token)-- revoke a tokenExtractBearerToken(header)-- extract token from Authorization headerNewMemoryTokenStore()-- in-memory revocation store
Quick start
import "github.com/LingByte/ling-base/common/jwtutil"
auth, err := jwtutil.New(jwtutil.Config{
Secret: []byte("my-32-byte-secret-1234567890123456"),
Issuer: "my-app",
AccessTTL: 15 * time.Minute,
RefreshTTL: 7 * 24 * time.Hour,
})
if err != nil {
log.Fatal(err)
}
pair, err := auth.Login("user-123", jwtutil.Roles("admin"))
claims, err := auth.Verify(pair.AccessToken)License
MIT