captcha

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: Apache-2.0 Imports: 10 Imported by: 2

README

package main

import (
    "context"

    "github.com/admpub/cache"

    _ "github.com/coscms/captcha/driver/click"
    _ "github.com/coscms/captcha/driver/rotate"
    _ "github.com/coscms/captcha/driver/slide"
    "github.com/coscms/captcha"
)

func main(){
    store := cache.NewCacher(context.Background(),`memory`,cache.Option{Interval:captcha.MaxAge})
    captcha.Open(`click`,`shape`,store)
}

Documentation

Index

Constants

View Source
const MaxAge = 300 //seconds

Variables

View Source
var (
	// verify
	ErrParameterRequired = errors.New("[captcha]param is empty")
	ErrIllegalKey        = errors.New("[captcha]illegal key")
	ErrInvalidResponse   = errors.New("[captcha]invalid response")

	// make data
	ErrGenerateFailed     = errors.New("[captcha]generate captcha data failed")
	ErrBase64EncodeFailed = errors.New("[captcha]encoding base64 data failed")

	// common
	ErrUnsupported = errors.New("[captcha]unsupported")

	// singleon
	ErrStorerInitFailed  = errors.New("[captcha]storer initialization failed")
	ErrCaptchaInitFailed = errors.New("[captcha]captcha initialization failed")
)

Functions

func JSON

func JSON(w http.ResponseWriter, data interface{}, code ...int)

func Md5

func Md5(b []byte) string

func ParseInt64

func ParseInt64(s string) (int64, error)

func Register

func Register(name string, constructor Constructor)

func RegisterInstance

func RegisterInstance(driverName string, captchaType string, instance Driver)

func Unregister

func Unregister(name string)

func UnregisterInstance

func UnregisterInstance(driverName string, captchaType string)

Types

type APIResponse

type APIResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	*Data   `json:",omitempty"`
}

func (*APIResponse) IsSuccess

func (a *APIResponse) IsSuccess() bool

func (*APIResponse) SetData

func (a *APIResponse) SetData(data *Data) *APIResponse

func (*APIResponse) SetError

func (a *APIResponse) SetError(message string) *APIResponse

func (*APIResponse) SetSuccess

func (a *APIResponse) SetSuccess(message ...string) *APIResponse

type Constructor

type Constructor func(captchaType string, store Storer, options ...Option) (Driver, error)

type Data

type Data struct {
	Key   string `json:"key"`
	Image string `json:"image,omitempty"`
	Thumb string `json:"thumb,omitempty"`
	Tile  *Tile  `json:"tile,omitempty"`
}

type Driver

type Driver interface {
	Init() error
	OptionSetter
	MakeData(ctx context.Context) (*Data, error)
	Verify(ctx context.Context, key string, response string) error
	Storer() Storer
}

func GetInstance

func GetInstance(driverName string, captchaType string) (Driver, error)

func GetInstanceOk added in v0.1.0

func GetInstanceOk(driverName string, captchaType string) (Driver, bool)

func Open

func Open(name string, cType string, store Storer, options ...Option) (Driver, error)

type Option

type Option func(d OptionSetter)

type OptionSetter

type OptionSetter interface {
	SetOption(key string, value interface{})
}

type Storer

type Storer interface {
	Put(ctx context.Context, key string, val interface{}, timeout int64) error
	Get(ctx context.Context, key string, value interface{}) error
	Delete(ctx context.Context, key string) error
}

type Tile

type Tile struct {
	Image   string `json:"image"`
	Width   int    `json:"width"`
	Height  int    `json:"height"`
	OffsetX int    `json:"x"`
	OffsetY int    `json:"y"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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