captcha

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitGlobalManager

func InitGlobalManager(config *Config)

InitGlobalManager 初始化全局验证码管理器

Types

type ClickCaptcha

type ClickCaptcha struct {
	// contains filtered or unexported fields
}

ClickCaptcha 点击验证码

func NewClickCaptcha

func NewClickCaptcha(width, height, count, tolerance int, expiration time.Duration, store Store) *ClickCaptcha

NewClickCaptcha 创建点击验证码管理器

func (*ClickCaptcha) Generate

func (cc *ClickCaptcha) Generate() (*Result, error)

Generate 生成点击验证码

func (*ClickCaptcha) Verify

func (cc *ClickCaptcha) Verify(id string, userPositions []Point) (bool, error)

Verify 验证点击验证码

func (*ClickCaptcha) VerifyWithoutDelete

func (cc *ClickCaptcha) VerifyWithoutDelete(id string, userPositions []Point) (bool, error)

VerifyWithoutDelete 验证点击验证码但不删除(用于预验证)

type ClickCaptchaData

type ClickCaptchaData struct {
	Image     string  `json:"image"`     // 图片Base64
	Positions []Point `json:"positions"` // 需要点击的位置列表
	Count     int     `json:"count"`     // 需要点击的数量
	Tolerance int     `json:"tolerance"` // 容差(像素)
}

ClickCaptchaData 点击验证码数据

type Config

type Config struct {
	// 图形验证码配置
	ImageWidth  int
	ImageHeight int
	ImageLength int

	// 点击验证码配置
	ClickWidth     int
	ClickHeight    int
	ClickCount     int
	ClickTolerance int

	// 通用配置
	Expiration time.Duration
	Store      Store
}

Config 验证码配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 默认配置

type ImageCaptcha

type ImageCaptcha struct {
	// contains filtered or unexported fields
}

ImageCaptcha 图形验证码

func NewImageCaptcha

func NewImageCaptcha(width, height, length int, expiration time.Duration, store Store) *ImageCaptcha

NewImageCaptcha 创建图形验证码管理器

func (*ImageCaptcha) Generate

func (ic *ImageCaptcha) Generate() (*Result, error)

Generate 生成图形验证码

func (*ImageCaptcha) Verify

func (ic *ImageCaptcha) Verify(id, code string) (bool, error)

Verify 验证图形验证码

func (*ImageCaptcha) VerifyWithoutDelete

func (ic *ImageCaptcha) VerifyWithoutDelete(id, code string) (bool, error)

VerifyWithoutDelete 验证图形验证码但不删除(用于预验证)

type ImageCaptchaData

type ImageCaptchaData struct {
	Image string `json:"image"` // Base64编码的图片
	Code  string `json:"code"`  // 验证码内容(仅用于测试,生产环境不应返回)
}

ImageCaptchaData 图形验证码数据

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager 统一验证码管理器

var GlobalManager *Manager

GlobalManager 全局验证码管理器

func NewManager

func NewManager(config *Config) *Manager

NewManager 创建统一验证码管理器

func (*Manager) Generate

func (m *Manager) Generate(captchaType Type) (*Result, error)

Generate 根据类型生成验证码

func (*Manager) GenerateClick

func (m *Manager) GenerateClick() (*Result, error)

GenerateClick 生成点击验证码

func (*Manager) GenerateImage

func (m *Manager) GenerateImage() (*Result, error)

GenerateImage 生成图形验证码

func (*Manager) Verify

func (m *Manager) Verify(captchaType Type, id string, data interface{}) (bool, error)

Verify 根据类型验证验证码

func (*Manager) VerifyClick

func (m *Manager) VerifyClick(id string, positions []Point) (bool, error)

VerifyClick 验证点击验证码

func (*Manager) VerifyImage

func (m *Manager) VerifyImage(id, code string) (bool, error)

VerifyImage 验证图形验证码

func (*Manager) VerifyImageWithoutDelete

func (m *Manager) VerifyImageWithoutDelete(id, code string) (bool, error)

VerifyImageWithoutDelete 验证图形验证码但不删除

func (*Manager) VerifyWithoutDelete

func (m *Manager) VerifyWithoutDelete(captchaType Type, id string, data interface{}) (bool, error)

VerifyWithoutDelete 根据类型验证验证码但不删除

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore 内存存储实现

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore 创建内存存储

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(id string) error

func (*MemoryStore) Get

func (s *MemoryStore) Get(id string) (interface{}, error)

func (*MemoryStore) Set

func (s *MemoryStore) Set(id string, data interface{}, expires time.Time) error

func (*MemoryStore) VerifyWithFunc

func (s *MemoryStore) VerifyWithFunc(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)

func (*MemoryStore) VerifyWithFuncWithoutDelete

func (s *MemoryStore) VerifyWithFuncWithoutDelete(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)

VerifyWithFuncWithoutDelete 验证但不删除(用于预验证)

type Point

type Point struct {
	X int `json:"x"`
	Y int `json:"y"`
}

Point 坐标点

type Result

type Result struct {
	ID      string                 `json:"id"`      // 验证码ID
	Type    Type                   `json:"type"`    // 验证码类型
	Data    map[string]interface{} `json:"data"`    // 验证码数据(根据类型不同而不同)
	Expires time.Time              `json:"expires"` // 过期时间
}

Result 验证码生成结果

type Store

type Store interface {
	Set(id string, data interface{}, expires time.Time) error
	Get(id string) (interface{}, error)
	Delete(id string) error
	VerifyWithFunc(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)
	VerifyWithFuncWithoutDelete(id string, input interface{}, compareFunc func(stored, input interface{}) bool) (bool, error)
}

Store 验证码存储接口

type Type

type Type string

Type 验证码类型

const (
	TypeImage Type = "image" // 图形验证码
	TypeClick Type = "click" // 点击验证码
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL