Documentation
¶
Index ¶
- Variables
- func BuildStringToSign(method, path, sortedQuery string, body []byte, timestamp int64, nonce string, ...) string
- func GinAuth(verifier *Verifier) gin.HandlerFunc
- func Sign(sk, stringToSign string) string
- func SortQuery(rawQuery string) string
- func SortQueryCached(rawQuery string) string
- type AuthHeaders
- type CachedSKStore
- type Config
- type KeyPair
- type MemoryNonceStore
- type NonceStore
- type RedisNonceStore
- type SKStore
- type Verifier
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidAK = errors.New("akm: AK 不存在") ErrTimestampExpired = errors.New("akm: 时间戳已过期") ErrNonceReplayed = errors.New("akm: Nonce 已被使用(重放攻击)") ErrSignatureMismatch = errors.New("akm: 签名不匹配") )
验签错误类型。
Functions ¶
func BuildStringToSign ¶
func BuildStringToSign(method, path, sortedQuery string, body []byte, timestamp int64, nonce string, extendFields map[string]string) string
BuildStringToSign 按规范拼接待签名字符串。
格式: Method + "\n" + Path + "\n" + SortedQuery + "\n" + Hex(SHA256(Body)) + "\n" + Timestamp + "\n" + Nonce
+ "\n" + ExtendField1Name=ExtendField1Value + ... (按 field_name 字典序)
func GinAuth ¶
func GinAuth(verifier *Verifier) gin.HandlerFunc
GinAuth 返回 Gin 中间件,对每个请求执行 AK/SK 验签。 验证失败时返回 401 并中断请求链。
func SortQuery ¶
SortQuery 将原始 query 字符串按 key 字典序排序后返回。 输入如 "c=3&a=1&b=2",输出 "a=1&b=2&c=3"。 空字符串或仅含 "=" 的 key 会保留。
func SortQueryCached ¶
SortQueryCached 带 LRU 缓存的 SortQuery。对于高频重复 query 参数组合的 API, 缓存命中时零分配,避免每次 strings.Split + sort.Strings + strings.Join。
Types ¶
type AuthHeaders ¶
type AuthHeaders struct {
AK string
Timestamp int64
Nonce string
Signature string
ExtendFields map[string]string // 拓展字段:field_name → field_value,如 {"appcode": "my-app"}
}
AuthHeaders 客户端发送的鉴权头。
type CachedSKStore ¶
type CachedSKStore struct {
// contains filtered or unexported fields
}
CachedSKStore 包装 SKStore,提供本地内存缓存。 缓存命中时零网络开销,TTL 内 SK 变更存在不一致窗口。
func NewCachedSKStore ¶
func NewCachedSKStore(inner SKStore, ttl time.Duration, maxSize int) *CachedSKStore
NewCachedSKStore 创建缓存装饰器。maxSize 限制缓存条目数(0 表示不限制)。
func (*CachedSKStore) Invalidate ¶
func (c *CachedSKStore) Invalidate(ak string)
Invalidate 主动清除指定 AK 的缓存(SK 变更时调用)。
type Config ¶
type Config struct {
TimeWindow time.Duration // 允许的时间偏移,默认 5 分钟
ExtendFields map[string]string // 拓展字段:field_name → header_name,如 {"appcode": "X-AppCode"}
}
Config 验签引擎配置。
type KeyPair ¶
KeyPair AK/SK 密钥对。
func GenerateKeyPair ¶
GenerateKeyPair 生成新的 AK/SK 密钥对。 AK = 20 字符 hex(10 字节随机),SK = 64 字符 hex(32 字节随机)。
type MemoryNonceStore ¶
type MemoryNonceStore struct {
// contains filtered or unexported fields
}
MemoryNonceStore 基于本地内存的 NonceStore 实现。 用于 Redis 不可用时的降级方案。 注意:分布式部署下各实例 Nonce 不共享,但 Nonce 碰撞概率可忽略,且仍受时间窗口约束。
func NewMemoryNonceStore ¶
func NewMemoryNonceStore() *MemoryNonceStore
NewMemoryNonceStore 创建本地内存 Nonce 存储实例,并启动后台 GC 协程。
func (*MemoryNonceStore) CheckAndSet ¶
func (m *MemoryNonceStore) CheckAndSet(ctx context.Context, nonce string, ttl time.Duration) (bool, error)
CheckAndSet 原子检查并设置 nonce。
type NonceStore ¶
type NonceStore interface {
// CheckAndSet 原子操作:如果 nonce 已存在返回 true(重放攻击),否则设置并返回 false。
CheckAndSet(ctx context.Context, nonce string, ttl time.Duration) (exists bool, err error)
}
NonceStore 提供 Nonce 防重放的原子检查与存储能力。
type RedisNonceStore ¶
type RedisNonceStore struct {
// contains filtered or unexported fields
}
RedisNonceStore 基于 Redis 的 NonceStore 实现。 使用 SET key value NX EX ttl 原子命令保证并发安全。
func NewRedisNonceStore ¶
func NewRedisNonceStore(client *redis.Client) *RedisNonceStore
NewRedisNonceStore 创建 Redis Nonce 存储实例。
func (*RedisNonceStore) CheckAndSet ¶
func (r *RedisNonceStore) CheckAndSet(ctx context.Context, nonce string, ttl time.Duration) (bool, error)
CheckAndSet 原子检查并设置 nonce。 若 nonce 已存在(重放)返回 (true, nil);首次出现则设置并返回 (false, nil)。
type Verifier ¶
type Verifier struct {
// contains filtered or unexported fields
}
Verifier AK/SK 请求验签器。
func NewVerifier ¶
func NewVerifier(skStore SKStore, nonceStore NonceStore, cfg Config) *Verifier
NewVerifier 创建验签器实例。