Documentation
¶
Index ¶
- Variables
- type Cap
- func (c *Cap) CreateChallenge(ctx context.Context) (*ChallengeData, error)
- func (c *Cap) HandleCreateChallenge() http.HandlerFunc
- func (c *Cap) HandleRedeemChallenge() http.HandlerFunc
- func (c *Cap) HandleValidateToken() http.HandlerFunc
- func (c *Cap) RedeemChallenge(ctx context.Context, token string, solutions []int64) (*TokenData, error)
- func (c *Cap) ValidateToken(ctx context.Context, token string) bool
- type CapOption
- type ChallengeData
- type ChallengeItem
- type MemoryStorage
- func (ms *MemoryStorage) Cleanup() error
- func (ms *MemoryStorage) GetChallenge(ctx context.Context, token string, isGetDel ...bool) (mts int64, exists bool)
- func (ms *MemoryStorage) GetToken(ctx context.Context, key string, isGetDel ...bool) (mts int64, exists bool)
- func (ms *MemoryStorage) SetChallenge(ctx context.Context, token string, expiresMTs int64) error
- func (ms *MemoryStorage) SetToken(ctx context.Context, key string, expiresMTs int64) error
- type RedisStorage
- func (s *RedisStorage) Cleanup() error
- func (rs *RedisStorage) GetChallenge(ctx context.Context, token string, isGetDel ...bool) (mts int64, exists bool)
- func (rs *RedisStorage) GetToken(ctx context.Context, key string, isGetDel ...bool) (mts int64, exists bool)
- func (rs *RedisStorage) SetChallenge(ctx context.Context, token string, expiresMTs int64) error
- func (rs *RedisStorage) SetToken(ctx context.Context, key string, expiresMTs int64) error
- type RedisStorageConfig
- type Storage
- type TokenData
- type VerificationParams
- type VerificationResult
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrInvalidChallenge = errors.New("invalid challenge body") //非法质询参数 ErrChallengeExpired = errors.New("challenge expired") //质询令牌过期 ErrInvalidSolutions = errors.New("invalid solutions") //非法解方案组参数 ErrGenerateFailed = errors.New("generate random string failed") //生成随机串失败 ErrStorageFailed = errors.New("storage operation failed") //存储操作失败 )
Functions ¶
This section is empty.
Types ¶
type Cap ¶
type Cap struct {
// contains filtered or unexported fields
}
func (*Cap) CreateChallenge ¶
func (c *Cap) CreateChallenge(ctx context.Context) (*ChallengeData, error)
CreateChallenge 创建质询数据
func (*Cap) HandleCreateChallenge ¶ added in v1.1.0
func (c *Cap) HandleCreateChallenge() http.HandlerFunc
HandleCreateChallenge 创建质询 Http Handle 封装
func (*Cap) HandleRedeemChallenge ¶ added in v1.1.0
func (c *Cap) HandleRedeemChallenge() http.HandlerFunc
HandleRedeemChallenge 工作量证明兑换验证令牌 Http Handle 封装
func (*Cap) HandleValidateToken ¶ added in v1.1.0
func (c *Cap) HandleValidateToken() http.HandlerFunc
HandleValidateToken 检查验证令牌 Http Handle 封装
type CapOption ¶
type CapOption func(c *Cap)
func WithChallenge ¶
配置质询数量/大小/难度
- {count} 生成组数量. 默认50
- {size} 每组大小. 默认32
- {difficulty} 难度级别. 默认4
注:
1.质询数量过大会占用内存磁盘空间并拖慢系统响应, 理论上限 2^31-1 或 2^63-1, 这里取上限 1_000 2.质询大小会影响连续内存的分配与通信开销, 理论上限约 2^64 (SHA-256输入上限), 这里取上限 128 (<1KB) 3.质询难度上限受制于目标哈希空间、数值表示范围等, 这里取上限 16
func WithLimiterParams ¶ added in v1.1.0
配置限流器参数 (调用 Handlexxx 方法时生效)
- {rps} 每秒通过数. 默认10次/秒.
- {burst} 最大突发容量. 默认50次.
注: rps 和 burst 都必须 > 0 才会启用限流. burst 建议设置为 rps 的 2-10 倍, 以处理突发请求
type ChallengeData ¶
type ChallengeData struct {
Challenge ChallengeItem `json:"challenge"`
Expires int64 `json:"expires"` //过期时间,毫秒级时间戳
Token string `json:"token"` //质询令牌
}
质询数据内容
type ChallengeItem ¶
type MemoryStorage ¶
type MemoryStorage struct {
// contains filtered or unexported fields
}
内存存储实现 (gocap.New 时的默认选项)
func NewMemoryStorage ¶
func NewMemoryStorage(cleanup ...int64) *MemoryStorage
创建内存存储实例
- {cleanup} 自动清理过期数据的定时间隔, 单位秒, 默认每 5 分钟
func (*MemoryStorage) Cleanup ¶
func (ms *MemoryStorage) Cleanup() error
func (*MemoryStorage) GetChallenge ¶
func (*MemoryStorage) SetChallenge ¶
type RedisStorage ¶
type RedisStorage struct {
// contains filtered or unexported fields
}
Redis 存储实现
func NewRedisStorage ¶
func NewRedisStorage(conf *RedisStorageConfig) (*RedisStorage, error)
创建 Redis 存储实例
- {conf} 配置项
func (*RedisStorage) GetChallenge ¶
func (*RedisStorage) SetChallenge ¶
type RedisStorageConfig ¶
type RedisStorageConfig struct {
RedisAddr string `json:"redis_addr"` //required. host:port
RedisUser string `json:"redis_user"` //optional.
RedisPass string `json:"redis_pass"` //optional.
RedisDb int `json:"redis_db"` //optional.
PrefixChallenge string `json:"prefix_challenge"` //质询数据缓存前缀
PrefixToken string `json:"prefix_token"` //验证令牌数据缓存前缀
}
type Storage ¶
type Storage interface {
// SetChallenge 设置质询令牌
// - {token} 质询令牌
// - {expiresMTs} 过期时刻,毫秒级时间戳
SetChallenge(ctx context.Context, token string, expiresMTs int64) error
// GetChallenge 获取质询令牌过期时间
// - {token} 质询令牌
// - {isGetDel} 是否获取后删除. 可选
//
// @return {mts} 毫秒级时间戳
// @return {exists} 是否存在
GetChallenge(ctx context.Context, token string, isGetDel ...bool) (mts int64, exists bool)
// SetToken 设置验证令牌
// - {key} 验证令牌Key
// - {expiresMTs} 过期时刻,毫秒级时间戳
SetToken(ctx context.Context, key string, expiresMTs int64) error
// GetToken 获取验证令牌过期时间
// - {key} 验证令牌Key
// - {isGetDel} 是否获取后删除. 可选
//
// @return {mts} 毫秒级时间戳
// @return {exists} 是否存在
GetToken(ctx context.Context, key string, isGetDel ...bool) (mts int64, exists bool)
// Cleanup 清理过期数据
Cleanup() error
}
type TokenData ¶
type TokenData struct {
Expires int64 `json:"expires,omitzero"` //过期时间,毫秒级时间戳
Token string `json:"token,omitzero"` //验证令牌
}
验证令牌内容
type VerificationParams ¶ added in v1.1.0
type VerificationParams struct {
Token string `json:"token"` //质询令牌
Solutions []int64 `json:"solutions,omitzero"` //质询解方案组
}
前端组件传入的参数
注: 用于 RedeemChallenge() 或 ValidateToken()
type VerificationResult ¶ added in v1.1.0
type VerificationResult struct {
*TokenData
Success bool `json:"success"`
Message string `json:"message,omitzero"`
}
返回前端组件的结果
注: 来自 RedeemChallenge() 或 ValidateToken()
Click to show internal directories.
Click to hide internal directories.