captcha

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2019 License: MIT, MIT, MIT Imports: 8 Imported by: 0

README

#captcha 验证码组件

Documentation

Index

Constants

View Source
const HeaderCaptchaEnabled = "X-Captcha-Enabled"

HeaderCaptchaEnabled header which contians if captcha is enabeld. If catpcha enabled,value "enabled" will be passed. Otherwise no header is passed.

View Source
const HeaderCaptchaName = "X-Captcha-Name"

HeaderCaptchaName header which contains the captcha name which used.

View Source
const HeaderReset = "X-Reset-Captcha"

HeaderReset header which should be passed in to reset captcha.

Variables

This section is empty.

Functions

func Factories

func Factories() []string

Factories returns a sorted list of the names of the registered factories.

func Register

func Register(name string, f Factory)

Register makes a driver creator available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.

func UnregisterAll

func UnregisterAll()

UnregisterAll unregister all drivers.

Types

type Captcha

type Captcha struct {

	//Session captcha session store.
	SessionStore *session.Store
	//Enabled if captcha is enabled.
	Enabled bool
	//AddrWhiteList ip addr white list.Ip start with value in list doesn't need captcha.
	AddrWhiteList []string
	//DisabledScenes scenes which doesn't neec captcha.
	DisabledScenes map[string]bool
	//EnabledChecker function which check if captcha is necessarily.
	EnabledChecker func(captcha *Captcha, scene string, r *http.Request) (bool, error)
	// contains filtered or unexported fields
}

Captcha captcha struct.

func New

func New(s *session.Store) *Captcha

New create a new empty captcha instance with given session store.

func (*Captcha) CaptchaAction

func (c *Captcha) CaptchaAction(scene string) func(w http.ResponseWriter, r *http.Request)

CaptchaAction action which afford capcha. Return captcha config json or empty object json if doesn't need captcha. If reset header is passed in,captcha will be reseted if supported.

func (*Captcha) EnabledCheck

func (c *Captcha) EnabledCheck(scene string, r *http.Request) (bool, error)

EnabledCheck check if http request in given scene need captcha. Return true if captcha is necessarily,and any error if raised.

func (*Captcha) Verifier

func (c *Captcha) Verifier(r *http.Request, scene string) Verifier

Verifier create verifier with given http request and scene.

func (*Captcha) Verify

func (c *Captcha) Verify(r *http.Request, scene string, token string) (bool, error)

Verify verify if token is validated with given http rquest and scene. return verify result and any error raised.

type Config

type Config struct {
	Enabled        bool
	Driver         string
	DisabledScenes map[string]bool
	AddrWhiteList  []string
	Config         cache.ConfigMap
}

Config captcha config struct.

func (*Config) ApplyTo

func (c *Config) ApplyTo(captcha *Captcha) error

ApplyTo apply config to captcha.

type Driver

type Driver interface {
	//Name return driver name.
	Name() string
	//MustCaptcha execute captcha to given http request and response and scene or reset value.
	//Panic if any error rasied.
	MustCaptcha(s *session.Store, w http.ResponseWriter, r *http.Request, scene string, reset bool)
	//Verify verify if token is validated with given http rquest and scene.
	//return verify result and any error raised.
	Verify(s *session.Store, r *http.Request, scene string, token string) (bool, error)
}

Driver captcha driver interface.

func NewDriver

func NewDriver(name string, conf cache.Config, prefix string) (Driver, error)

NewDriver create new driver with given name,config and prefix. Return driver created and any error if raised.

type Factory

type Factory func(conf cache.Config, prefix string) (Driver, error)

Factory driver createor with given config and prefix. Return driver and any error raised.

type Verifier

type Verifier func(token string) (bool, error)

Verifier verifier interface.

Jump to

Keyboard shortcuts

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