Documentation ¶
Index ¶
- Variables
- func AddToContext(ctx context.Context, keyValuePair ...interface{}) (context.Context, error)
- func ErrInvalidRequest(err error) render.Renderer
- func ErrRender(err error) render.Renderer
- type Bank
- type Captcha
- type CaptchaResponse
- type Challenge
- type Challenger
- type CheckRequest
- type CtxKey
- type ErrResponse
- type Generator
- type Manager
- func (m *Manager) CaptchaCtx(next http.Handler) http.Handler
- func (m *Manager) CheckCaptcha(w http.ResponseWriter, r *http.Request)
- func (m *Manager) GC(unit time.Duration, errChan chan<- error, cancel <-chan struct{})
- func (m *Manager) Gen(ctx context.Context) (c *Captcha, err error)
- func (m *Manager) NewCaptcha(w http.ResponseWriter, r *http.Request)
- func (m *Manager) Refresh(captchaID uint32) (*Captcha, error)
- func (m *Manager) RefreshCaptcha(w http.ResponseWriter, r *http.Request)
- func (m *Manager) Router(ctx context.Context) http.Handler
- type MathSymbol
- type OKResponse
- type Option
- func WithExpiry(expiry time.Duration) Option
- func WithLifetime(lifetime time.Duration) Option
- func WithMath(symbols *Symbols) Option
- func WithMountPoint(v string) Option
- func WithNoGzip() Option
- func WithQuestionBank(bank *Bank) Option
- func WithSources(s Source) Option
- func WithStorage(storageURL string) Option
- type Source
- type Storer
- type Symbols
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoSources sources not set, select one of Random, Math or QuestionBank. ErrNoSources = errors.New("sources not set, select one of Random, Math or QuestionBank") // ErrLangEmpty lang is empty. ErrLangEmpty = errors.New("lang is empty") )
var ( ErrNotFound = &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."} ErrInternalServerError = &ErrResponse{HTTPStatusCode: 500, StatusText: "Server error."} ErrIncorrectAnswer = &ErrResponse{HTTPStatusCode: 403, StatusText: "Incorrect answer."} )
var ( // DefaultManager default settings. DefaultManager = &Manager{ Languages: defaultLangs, Sources: Math | Random, Math: NewSymbols(defaultLangs...), Bank: NewBank(defaultLangs...), defaultExpiry: 10 * time.Minute, lifetimeAfterPassed: 2 * time.Minute, Store: DefaultStore, } )
Functions ¶
func AddToContext ¶
AddToContext is a helper function that adds the values in keyValuePair to ctx. Keys are even indexes (0 is considered even), values are odd indexes.
func ErrInvalidRequest ¶
Types ¶
type Bank ¶
Bank contains arrays a map[string][]*Captcha, under each language. The captchas only contain questions and answers, their Audio and image fields will not be populated until it is requested.
type Captcha ¶
type Captcha struct { // ID of this Captcha. ID uint32 `json:"id,omitempty"` // Img url to image file on server. Image string `json:"image-url,omitempty"` // Passed status of this captcha. Passed bool `json:"passed"` ClientID string `json:"client-id"` // Lang language. Lang string `json:"language,omitempty"` // Audio url to audio file on server. Audio string `json:"audio-url,omitempty"` // Question the question posed in the captcha. Question string `json:"question,omitempty"` // Ans slice of acceptable answers. Answers []string `json:"answers,omitempty"` // Expiry when this Captcha is invalid and needs to be refreshed. Expiry time.Time `json:"expiry,omitempty"` }
type CaptchaResponse ¶
type CaptchaResponse struct {
*Captcha
}
func NewCaptchaResponse ¶
func NewCaptchaResponse(c *Captcha) *CaptchaResponse
func (*CaptchaResponse) Render ¶
func (c *CaptchaResponse) Render(w http.ResponseWriter, r *http.Request) error
type Challenger ¶
type Challenger interface { // Gen generates a Challenge based on parameters in Gen. Gen(ctx context.Context) (*Captcha, error) Refresh(captchaID uint32) (*Captcha, error) Status(captchaID uint32) bool // GC garbage collects expired captchas. Needs to run in a goroutine to clean // expired captchas. It call the store's GC method every unit. GC(unit time.Duration, err chan<- error, cancel <-chan struct{}) }
type CheckRequest ¶
type CheckRequest struct {
Answer string `json:"challenge-response"`
}
type ErrResponse ¶
type ErrResponse struct { Err error `json:"-"` // low-level runtime error HTTPStatusCode int `json:"-"` // http response status code StatusText string `json:"status"` // user-level status message ErrorText string `json:"error,omitempty"` // application-level error message, for debugging }
func (*ErrResponse) Render ¶
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error
type Manager ¶
type Manager struct { Challenger // Sources sources the manager will use for challenges. Sources Source // Languages the manager will accept and generate challenges in. Languages []string // Bank question bank to use. Bank *Bank // Math set of math symbols to use. Math *Symbols Store Storer FileStorage gostorage.Driver // contains filtered or unexported fields }
func NewManager ¶
NewManager returns a default manager modified with opts. Panics on error.
func (*Manager) CheckCaptcha ¶
func (m *Manager) CheckCaptcha(w http.ResponseWriter, r *http.Request)
func (*Manager) GC ¶
GC needs to be run in a goroutine to clean expired captcahs. It'll start a time.Ticker every unit and periodically call the store GC method.
func (*Manager) NewCaptcha ¶
func (m *Manager) NewCaptcha(w http.ResponseWriter, r *http.Request)
func (*Manager) RefreshCaptcha ¶
func (m *Manager) RefreshCaptcha(w http.ResponseWriter, r *http.Request)
type MathSymbol ¶
func (*MathSymbol) HumanOrSymbol ¶
func (m *MathSymbol) HumanOrSymbol() string
HumanOrSymbol 50/50 chance of either.
func (*MathSymbol) String ¶
func (m *MathSymbol) String() string
String always return the symbol be parsed.
type OKResponse ¶
type OKResponse struct {
Status string `json:"Status"`
}
func (*OKResponse) Render ¶
func (ok *OKResponse) Render(w http.ResponseWriter, r *http.Request) error
type Option ¶
type Option func(*Manager)
func WithExpiry ¶
WithExpiry sets captcha expiration to expiry.
func WithLifetime ¶
WithLifetime sets lifetime of captchas, once passed, to lifetime.
func WithMath ¶
WithMath appends Math to the sources and replaces the default math symbols with the passed symbols.
func WithNoGzip ¶
func WithNoGzip() Option
WithNoGzip serve the static assets without gzipping them.
func WithQuestionBank ¶
func WithStorage ¶
WithStorage sets the Store to the storageURL. See https://github.com/djangulo/go-storage for viable connection strings. Panics on error.
type Storer ¶
type Storer interface { Create(c *Captcha) error Get(id uint32) (*Captcha, error) Update(id uint32, c *Captcha) error Delete(id uint32) error // GC garbage collects expired captchas. Needs to run in a goroutine to clean // expired captchas. GC() error }
Storer interface for persistent storage.
type Symbols ¶
type Symbols struct {
Values map[string][]*MathSymbol
}
Symbols holds MathSymbol definitions under each language. e.g. Symbols.Values["en"].
func NewSymbols ¶
NewSymbols initializes a *Symbols with langs.