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) (ts int64, exists bool)
- func (ms *MemoryStorage) GetToken(ctx context.Context, key string, isGetDel ...bool) (ts int64, exists bool)
- func (ms *MemoryStorage) SetChallenge(ctx context.Context, token string, expiresTs int64) error
- func (ms *MemoryStorage) SetToken(ctx context.Context, key string, expiresTs int64) error
- type RedisStorage
- func (s *RedisStorage) Cleanup() error
- func (rs *RedisStorage) GetChallenge(ctx context.Context, token string, isGetDel ...bool) (ts int64, exists bool)
- func (rs *RedisStorage) GetToken(ctx context.Context, key string, isGetDel ...bool) (ts int64, exists bool)
- func (rs *RedisStorage) SetChallenge(ctx context.Context, token string, expiresTs int64) error
- func (rs *RedisStorage) SetToken(ctx context.Context, key string, expiresTs 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") //生成随机串失败 ErrStorageNotDefined = errors.New("storage not defined") //未定义存储实例 )
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 WithLimiterParams ¶ added in v1.1.0
配置限流器参数 (调用 Handlexxx 方法时生效)
- {rps} 每秒通过数. 默认10次/秒. 都置为0时关闭限流
- {burst} 最大突发容量. 默认50次. 都置为0时关闭限流
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() 时的默认存储 默认每隔 5分钟 清理一次过期数据
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) Cleanup ¶
func (s *RedisStorage) Cleanup() error
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} 质询令牌 // - {expiresTs} 过期时刻, 秒级时间戳 SetChallenge(ctx context.Context, token string, expiresTs int64) error // GetChallenge 获取质询令牌过期时间 // - {token} 质询令牌 // - {isGetDel} 是否获取后删除. 可选 GetChallenge(ctx context.Context, token string, isGetDel ...bool) (ts int64, exists bool) // SetToken 设置验证令牌 // - {key} 验证令牌Key // - {expiresTs} 过期时刻, 秒级时间戳 SetToken(ctx context.Context, key string, expiresTs int64) error // GetToken 获取验证令牌过期时间 // - {key} 验证令牌Key // - {isGetDel} 是否获取后删除. 可选 GetToken(ctx context.Context, key string, isGetDel ...bool) (ts 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.